Skip to content

Commit 1ccab53

Browse files
Make github/bitbucket repo syncing async in the frontend
We are using a ajax request to the API to trigger the update and then monitor the progress. When the sync job has finished, we update the page. Also removing the synchronous sync code from the urls and view logic.
1 parent fd8ae06 commit 1ccab53

File tree

6 files changed

+99
-35
lines changed

6 files changed

+99
-35
lines changed

media/css/core.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ input[type="hidden"] { display: none; }
9696
input[type="checkbox"], input[type="radio"] { display: inline; }
9797
label { display: block; margin-bottom: 4px; font-weight: bold; color: #444; }
9898

99+
input[type="submit"].inline, input[type="button"].inline, button.inline, .button.inline { display: inline; }
100+
99101
h2 > span.link-help,
100102
h3 > span.link-help,
101103
label > span.link-help {

media/javascript/rtd-import.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,72 @@
3131

3232
repo.val(type);
3333
});
34+
35+
$('[data-sync-repositories]').each(function () {
36+
var $button = $(this);
37+
var target = $(this).attr('data-target');
38+
39+
$button.on('click', function () {
40+
var url = $button.attr('data-sync-repositories');
41+
$.ajax({
42+
method: 'POST',
43+
url: url,
44+
success: function (data) {
45+
$button.attr('disabled', true);
46+
watchProgress(data.url);
47+
},
48+
error: function () {
49+
onError();
50+
}
51+
});
52+
$('.sync-repositories').addClass('hide');
53+
$('.sync-repositories-progress').removeClass('hide');
54+
});
55+
56+
function watchProgress(url) {
57+
setTimeout(function () {
58+
$.ajax({
59+
method: 'GET',
60+
url: url,
61+
success: function (data) {
62+
if (data.finished) {
63+
if (data.success) {
64+
onSuccess();
65+
} else {
66+
onError();
67+
}
68+
} else {
69+
watchProgress(url);
70+
}
71+
},
72+
error: function () {
73+
watchProgress(url);
74+
}
75+
});
76+
}, 2000);
77+
}
78+
79+
function onSuccess(url) {
80+
$.ajax({
81+
method: 'GET',
82+
url: window.location.href,
83+
success: function (data) {
84+
var $newContent = $(data).find(target);
85+
$('body').find(target).replaceWith($newContent);
86+
$('.sync-repositories').addClass('hide');
87+
$('.sync-repositories-progress').addClass('hide');
88+
$('.sync-repositories-success').removeClass('hide');
89+
},
90+
error: onError
91+
});
92+
}
93+
94+
function onError() {
95+
$('.sync-repositories').addClass('hide');
96+
$('.sync-repositories-progress').addClass('hide');
97+
$('.sync-repositories-error').removeClass('hide');
98+
}
99+
});
34100
});
35101

36102
})();

readthedocs/projects/urls/private.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,12 @@
2626

2727
url(r'^import/github/$',
2828
'projects.views.private.project_import_github',
29-
{'sync': False},
3029
name='projects_import_github'),
3130

32-
url(r'^import/github/sync/$',
33-
'projects.views.private.project_import_github',
34-
{'sync': True},
35-
name='projects_sync_github'),
36-
3731
url(r'^import/bitbucket/$',
3832
'projects.views.private.project_import_bitbucket',
39-
{'sync': False},
4033
name='projects_import_bitbucket'),
4134

42-
url(r'^import/bitbucket/sync/$',
43-
'projects.views.private.project_import_bitbucket',
44-
{'sync': True},
45-
name='projects_sync_bitbucket'),
46-
4735
url(r'^(?P<project_slug>[-\w]+)/$',
4836
'projects.views.private.project_manage',
4937
name='projects_manage'),

readthedocs/projects/views/private.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,10 @@ def project_redirects_delete(request, project_slug):
592592

593593

594594
@login_required
595-
def project_import_github(request, sync=False):
595+
def project_import_github(request):
596596
'''Show form that prefills import form with data from GitHub'''
597-
github_connected = oauth_utils.import_github(user=request.user, sync=sync)
597+
github_connected = oauth_utils.import_github(
598+
user=request.user, sync=False)
598599
repos = GithubProject.objects.filter(users__in=[request.user])
599600

600601
# Find existing projects that match a repo url
@@ -615,17 +616,17 @@ def project_import_github(request, sync=False):
615616
{
616617
'repos': repos,
617618
'github_connected': github_connected,
618-
'sync': sync,
619619
},
620620
context_instance=RequestContext(request)
621621
)
622622

623623

624624
@login_required
625-
def project_import_bitbucket(request, sync=False):
625+
def project_import_bitbucket(request):
626626
'''Show form that prefills import form with data from BitBucket'''
627627

628-
bitbucket_connected = oauth_utils.import_bitbucket(user=request.user, sync=sync)
628+
bitbucket_connected = oauth_utils.import_bitbucket(
629+
user=request.user, sync=False)
629630
repos = BitbucketProject.objects.filter(users__in=[request.user])
630631

631632
# Find existing projects that match a repo url
@@ -646,7 +647,6 @@ def project_import_bitbucket(request, sync=False):
646647
{
647648
'repos': repos,
648649
'bitbucket_connected': bitbucket_connected,
649-
'sync': sync,
650650
},
651651
context_instance=RequestContext(request)
652652
)

readthedocs/templates/projects/project_import_bitbucket.html

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{% endblock %}
1010

1111
{% block extra_scripts %}
12+
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/django-csrf.js"></script>
1213
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/rtd-import.js"></script>
1314
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/jquery.markitup.pack.js"></script>
1415
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/sets/sphinx/editor.js"></script>
@@ -23,17 +24,20 @@
2324

2425
{% block content %}
2526
{% if bitbucket_connected %}
26-
{% if not sync %}
27-
<p class="empty">
28-
{% url 'projects_sync_bitbucket' as sync_bitbucket_url %}
29-
{% blocktrans %}Showing your BitBucket projects. <a href="{{ sync_bitbucket_url }}">Sync your BitBucket projects</a> to update them.{% endblocktrans %}
27+
<p class="empty sync-repositories">
28+
{% url 'api_sync_bitbucket_repositories' as sync_url %}
29+
{% blocktrans with button_attrs=' class="inline" data-sync-repositories="'|add:sync_url|add:'" data-target=".respository-list"'|safe %}Showing your BitBucket projects. <button {{ button_attrs }}>Sync your BitBucket projects</button> to update them.{% endblocktrans %}
3030
</p>
31-
{% else %}
32-
<p class="empty">
31+
<p class="empty sync-repositories-progress hide">
32+
{% blocktrans %}Syncing ...{% endblocktrans %}
33+
</p>
34+
<p class="empty sync-repositories-success hide">
3335
{% blocktrans %}BitBucket projects are now up to date.{% endblocktrans %}
3436
</p>
35-
{% endif %}
36-
<div class="module">
37+
<p class="empty sync-repositories-error hide">
38+
{% blocktrans %}Your BitBucket projects could not be synced. Please try again later.{% endblocktrans %}
39+
</p>
40+
<div class="module repository-list">
3741
<div class="module-wrapper">
3842
<div class="module-list">
3943
<div class="module-list-wrapper">
@@ -82,7 +86,7 @@
8286
</div>
8387
{% else %}
8488
{% url 'socialaccount_connections' as social_url %}
85-
{% blocktrans %}You don't have any BitBucket repositories connected.
89+
{% blocktrans %}You don't have any BitBucket repositories connected.
8690
Go to your <a href="{{ social_url }}">Social Accounts</a> to set one up.
8791
{% endblocktrans %}
8892
{% endif %}

readthedocs/templates/projects/project_import_github.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{% endblock %}
1010

1111
{% block extra_scripts %}
12+
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/django-csrf.js"></script>
1213
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/rtd-import.js"></script>
1314
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/jquery.markitup.pack.js"></script>
1415
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/sets/sphinx/editor.js"></script>
@@ -23,17 +24,20 @@
2324

2425
{% block content %}
2526
{% if github_connected %}
26-
{% if not sync %}
27-
<p class="empty">
28-
{% url 'projects_sync_github' as sync_github_url %}
29-
{% blocktrans %}Showing your GitHub projects. <a href="{{ sync_github_url }}">Sync your GitHub projects</a> to update them.{% endblocktrans %}
27+
<p class="empty sync-repositories">
28+
{% url 'api_sync_github_repositories' as sync_url %}
29+
{% blocktrans with button_attrs=' class="inline" data-sync-repositories="'|add:sync_url|add:'" data-target=".respository-list"'|safe %}Showing your GitHub projects. <button {{ button_attrs }}>Sync your GitHub projects</button> to update them.{% endblocktrans %}
3030
</p>
31-
{% else %}
32-
<p class="empty">
31+
<p class="empty sync-repositories-progress hide">
32+
{% blocktrans %}Syncing ...{% endblocktrans %}
33+
</p>
34+
<p class="empty sync-repositories-success hide">
3335
{% blocktrans %}GitHub projects are now up to date.{% endblocktrans %}
3436
</p>
35-
{% endif %}
36-
<div class="module">
37+
<p class="empty sync-repositories-error hide">
38+
{% blocktrans %}Your GitHub projects could not be synced. Please try again later.{% endblocktrans %}
39+
</p>
40+
<div class="module repository-list">
3741
<div class="module-wrapper">
3842
<div class="module-list">
3943
<div class="module-list-wrapper">

0 commit comments

Comments
 (0)