Skip to content

Commit

Permalink
push to live functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohanator9000 committed Apr 18, 2018
1 parent b3967a3 commit c3336b1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
27 changes: 25 additions & 2 deletions assets/js/components/management/PackageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@
<div class="col">
<h2>
{{ packageData.slug || $route.params.slug }}
<b-button class="ml-2" size="sm" variant="secondary" :disabled="isReallyFetching" @click="fetchGdrive">
<b-button class="ml-2" size="sm" variant="secondary" :disabled="isReallyFetching || isPublishing" @click="fetchGdrive">
<span v-if="isReallyFetching">
Fetching...
</span>
<span v-else-if="isPublishing">
Publishing...
</span>
<span v-else>
Update from GDrive
</span>
</b-button>
<b-button class="ml-2" size="sm" variant="danger" :disabled="isReallyFetching || isPublishing" @click="publishToNode">
<span v-if="isReallyFetching">
Fetching...
</span>
<span v-else-if="isPublishing">
Publishing...
</span>
<span v-else>
Publish
</span>
</b-button>
</h2>
</div>
</div>
</div>
<div class="col">
<div class="row">
Expand Down Expand Up @@ -95,6 +109,7 @@ export default {
return {
packageData: {},
isFetching: false,
isPublishing: false,
isLoading: true,
}
},
Expand All @@ -117,6 +132,14 @@ export default {
this.packageData = res.data;
})
},
publishToNode: function() {
this.isPublishing = true;
axios.post("/api/packages/" + this.$route.params.pset + "/" + this.$route.params.slug + "/push")
.catch((error) => {
alert("Publishing Failed!");
});
this.isPublishing = false;
},
}
}
</script>
1 change: 1 addition & 0 deletions kerckhoff/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,6 @@
S3_ASSETS_UPLOAD_BUCKET = env('S3_ASSETS_UPLOAD_BUCKET')
REPOSITORY_FOLDER_ID = env("REPOSITORY_FOLDER_ID")
CF_ZONE = env('CF_ZONE')
LIVE_PUSH_SERVER = env('LIVE_PUSH_SERVER')

LOGIN_REDIRECT_URL = '/manage/'
19 changes: 12 additions & 7 deletions packages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import imghdr
import CloudFlare
import re
import requests

S3_BUCKET = settings.S3_ASSETS_UPLOAD_BUCKET
s3 = boto3.client('s3', 'us-west-2', config=Config(s3={'addressing_style': 'path'}))
Expand Down Expand Up @@ -114,6 +115,10 @@ def setup_and_save(self, user, pset_slug):
self.save()
return self

def push_to_live(self):
res = requests.post(settings.LIVE_PUSH_SERVER + "/update", json={'id': self.package_set.slug + '/' + self.slug})
return res.ok

# TODO - put this in a workqueue
def fetch_from_gdrive(self, user):
self.processing = True
Expand Down Expand Up @@ -200,9 +205,9 @@ def transfer_to_s3(session, package):
"hash": image_hash,
"s3_fields": response
}

return package



def add_to_repo_folder(session, package):
Expand All @@ -224,7 +229,7 @@ def get_file(session, file_id, download=False):
def list_folder(session, package):
text = "### No article document was found in this package!\n"
images = []

payload = {
"q": "'%s' in parents" % package.drive_folder_id,
"maxResults": 1000
Expand All @@ -244,7 +249,7 @@ def list_folder(session, package):
if article[0]['mimeType'] != "application/vnd.google-apps.document":
req = get_file(session, article[0]['id'], download=True)
text = req.content.decode('utf-8')
else:
else:
data = session.get(PREFIX + "/v2/files/" + article[0]['id'] + "/export", params={"mimeType": "text/plain"})
text = data.content.decode('utf-8')
# fix indentation for yaml
Expand Down Expand Up @@ -272,14 +277,14 @@ def create_package(session, package, existing=False):
session.post(PREFIX + "/v2/files", json=file_payload)

return (folder_resource['alternateLink'], folder_resource['id'])


# https://github.com/pennersr/django-allauth/issues/420
def get_oauth2_session(user):
""" Create OAuth2 session which autoupdates the access token if it has expired """

refresh_token_url = "https://accounts.google.com/o/oauth2/token"

social_token = SocialToken.objects.get(account__user=user, account__provider='google')

def token_updater(token):
Expand All @@ -304,5 +309,5 @@ def token_updater(token):
'expires_in': expires_in # Important otherwise the token update doesn't get triggered.
}

return OAuth2Session(client_id, token=token, auto_refresh_kwargs=extra,
return OAuth2Session(client_id, token=token, auto_refresh_kwargs=extra,
auto_refresh_url=refresh_token_url, token_updater=token_updater)
3 changes: 2 additions & 1 deletion packages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
path('', views.list_psets, name='list_package_sets'),
path('<str:pset_slug>', views.list_or_create, name='list_or_create'),
path('<str:pset_slug>/<str:id>/fetch', views.update_package, name='package_update'),
path('<str:pset_slug>/<str:id>/', views.show_one, name='get')
path('<str:pset_slug>/<str:id>/', views.show_one, name='get'),
path('<str:pset_slug>/<str:id>/push', views.push_to_live, name="push_to_live")
]
11 changes: 10 additions & 1 deletion packages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ def show_one(request, pset_slug, id):
def update_package(request, pset_slug, id):
package = Package.objects.get(package_set__slug=pset_slug, slug=id)
res = package.fetch_from_gdrive(request.user)
return JsonResponse(model_to_dict(res))
return JsonResponse(model_to_dict(res))

@require_POST
def push_to_live(request, pset_slug, id):
package = Package.objects.get(package_set__slug=pset_slug, slug=id)
res = package.push_to_live()
if res:
return HttpResponse(status=200)
else:
return HttpResponse(status=400)

0 comments on commit c3336b1

Please sign in to comment.