Forráskód Böngészése

[notebook] Include Github oAuth flow in the UI

Enrico Berti 10 éve
szülő
commit
2c18d0c

+ 12 - 7
desktop/libs/notebook/src/notebook/api.py

@@ -335,19 +335,24 @@ def github_authorize(request):
     return JsonResponse(response)
   else:
     auth_url = GithubClient.get_authorization_url()
+    request.session['github_callback_redirect'] = request.GET.get('currentURL')
+    request.session['github_callback_fetch'] = request.GET.get('fetchURL')
+    response = {
+      'status': -1,
+      'auth_url':auth_url
+    }
+    if (request.is_ajax()):
+      return JsonResponse(response)
+
     return HttpResponseRedirect(auth_url)
 
 
 @api_error_handler
 def github_callback(request):
-  response = {'status': -1}
-
+  redirect_base = request.session['github_callback_redirect'] + "&github_status="
   if 'code' in request.GET:
     session_code = request.GET.get('code')
     request.session['github_access_token'] = GithubClient.get_access_token(session_code)
-    response['status'] = 0
-    response['message'] = _('User successfully authenticated to GitHub.')
+    return HttpResponseRedirect(redirect_base + "0&github_fetch=" + request.session['github_callback_fetch'])
   else:
-    response['message'] = _('Could not decode file content to JSON.')
-
-  return JsonResponse(response)
+    return HttpResponseRedirect(redirect_base + "-1&github_fetch=" + request.session['github_callback_fetch'])

+ 31 - 2
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -253,7 +253,7 @@ ${ require.config() }
   </div>
   <div class="modal-footer">
     <a href="#" class="btn" data-dismiss="modal">${_('Close')}</a>
-    <a href="#" class="btn btn-primary disable-feedback" data-dismiss="modal" data-bind="click: importGithub">${_('Import')}</a>
+    <a id ="importGithubBtn" href="#" class="btn btn-primary disable-feedback" data-bind="click: authorizeGithub">${_('Import')}</a>
   </div>
 </div>
 
@@ -991,10 +991,28 @@ ${ require.config() }
 
 <script type="text/javascript" charset="utf-8">
 
+  function authorizeGithub() {
+    if ($("#importGithubUrl").val().trim() != "") {
+      $(".fa-github").addClass("fa-spinner fa-spin");
+      $("#importGithubBtn").attr("disabled", "disabled");
+      $.getJSON("/notebook/api/github/authorize?currentURL=" + location.pathname + (location.search != "" ? location.search : "?github=true") + "&fetchURL=" + $("#importGithubUrl").val(), function (data) {
+        if (data.status == 0) {
+          $(".fa-github").removeClass("fa-spinner fa-spin");
+          $("#importGithubBtn").removeAttr("disabled");
+          $("#importGithubModal").modal("hide");
+          importGithub();
+        }
+        else {
+          location.href = data.auth_url;
+        }
+      });
+    }
+  }
+
   function importGithub() {
     $(".hoverText").html("<i class='fa fa-spinner fa-spin'></i>");
     showHoverMsg();
-    $.get("api/github/fetch?url=" + $("#importGithubUrl").val(), function(data){
+    $.get("api/github/fetch?url=" + $("#importGithubUrl").val().trim(), function(data){
       if (data && data.content){
         if ($.isArray(data.content)) { // it's a Hue Notebook
           window.importExternalNotebook(JSON.parse(data.content[0].fields.data));
@@ -1726,6 +1744,17 @@ ${ require.config() }
       ko.applyBindings(viewModel);
       viewModel.init();
 
+      if (location.getParameter("github_status") != "") {
+        if (location.getParameter("github_status") == "0") {
+          $.jHueNotify.info("${ _('User successfully authenticated to GitHub.') }");
+          $("#importGithubUrl").val(location.getParameter("github_fetch"));
+          importGithub();
+        }
+        else {
+          $.jHueNotify.error("${ _('Could not decode Github file content to JSON.') }");
+        }
+      }
+
       window.hueDebug = {
         viewModel: viewModel,
         ko: ko