Reload status after uploading an add-on to AMO

This commit is contained in:
Piotr Zalewa 2011-09-14 23:38:22 +01:00
Родитель 09c740811a
Коммит 84f07d2e84
5 изменённых файлов: 86 добавлений и 12 удалений

Просмотреть файл

@ -2,5 +2,7 @@ from django.conf.urls.defaults import url, patterns
urlpatterns = patterns('amo.views',
url(r'^upload_to_amo/(?P<pk>\d+)/', 'upload_to_amo', name='amo_upload'),
url(r'^addon_details_from_amo/(?P<pk>\d+)/', 'get_addon_details_from_amo',
name='amo_get_addon_details'),
url(r'^addon_details/(?P<pk>\d+)/', 'get_addon_details',
name='amo_get_addon_details'))
name='get_addon_status'))

Просмотреть файл

@ -43,7 +43,7 @@ def upload_to_amo(request, pk):
return HttpResponse('{"delayed": true}')
def get_addon_details(request, pk):
def get_addon_details_from_amo(request, pk):
""" Finds latest revision uploaded to AMO and pulls metadata from AMO
using `generic AMO API <https://developer.mozilla.org/en/addons.mozilla.org_%28AMO%29_API_Developers%27_Guide/The_generic_AMO_API>`_
@ -67,3 +67,24 @@ def get_addon_details(request, pk):
super(PackageRevision, revision).save()
return HttpResponse(simplejson.dumps(amo_meta))
#mimetype="application/json")
def get_addon_details(request, pk):
"""Provide currently stored AMO Status (without contacting to AMO)
:attr: pk (int) :class:`~jetpack.models.PackageRevision` primary key
:returns: add-on metadata or empty dict in JSON format
"""
# get PackageRevision
revision = get_object_or_404(PackageRevision, pk=pk)
# check if Package is synced with the AMO and last update was successful
if (not revision.package.amo_id):
return HttpResponse('{}')# mimetype="application/json")
amo_meta = {'status': revision.get_status_name(),
'status_code': revision.amo_status,
'version': revision.amo_version_name,
'pk': revision.pk,
'uploaded': revision.amo_status != STATUS_UPLOAD_FAILED}
return HttpResponse(simplejson.dumps(amo_meta))
#mimetype="application/json")

Просмотреть файл

@ -158,6 +158,11 @@ class PackageRevision(BaseModel):
"""
return reverse('amo_get_addon_details', args=[self.pk])
def get_status_url(self):
""":returns: (string) url to pull get_addon_details view
"""
return reverse('get_addon_status', args=[self.pk])
def get_status_name(self):
""":returns: (string) the name of the AMO status or None
"""

Просмотреть файл

@ -3,7 +3,8 @@
<div class='UI_AMO_Info'
data-revision_id="{{ item.latest_uploaded.pk }}"
data-uploaded="{% if item.amo_id and item.latest_uploaded and item.latest_uploaded.amo_status != -2 %}1{% endif %}"
data-pull_info_url="{{ item.latest_uploaded.get_amo_status_url }}">
data-pull_info_url="{{ item.latest_uploaded.get_amo_status_url }}"
data-get_addon_info_url="{{ item.latest.get_status_url }}">
<h2>AMO Status</h2>
<p>version:
<span class='amo-latest_version'>{{ item.latest_uploaded.amo_version_name|default:"---"}}</span>
@ -18,13 +19,13 @@
{% if item.latest.is_uploaded %}
<li class="UI_AMO_Version_Uploaded">upload new version</li>
{% else %}
<li class="UI_AMO_Upload_New_Version"
<li class="UI_AMO_Upload_New_Version upload_link"
data-upload_url="{{ item.latest.get_upload_to_amo_url }}"><a>upload new version</a></li>
{% endif %}
{# <li class="UI_AMO_View_History" data-url=""><a>view history</a></li> #}
<li class="UI_AMO_View_On_AMO"><a href="" target="">view on AMO</a></li>
{#<li class="UI_AMO_View_On_AMO"><a href="" target="">view on AMO</a></li>#}
{% else %}
<li class="UI_AMO_Upload_New_Addon"
<li class="UI_AMO_Upload_New_Addon upload_link"
data-upload_url="{{ item.latest.get_upload_to_amo_url }}"><a>upload to AMO</a></li>
{% endif %}
</ul>

Просмотреть файл

@ -14,11 +14,9 @@ FlightDeck = Class.refactor(FlightDeck, {
})
})
}
$$('.UI_AMO_Upload_New_Version a').addEvent('click', this.uploadToAMO);
$$('.UI_AMO_Upload_New_Addon a').addEvent('click', this.uploadToAMO);
$$('.UI_AMO_Info').each(function(status_el) {
this.getStatusFromAMO(status_el);
}, this);
$$('.UI_AMO_Upload_New_Version a').addEvent('click', this.uploadToAMO.bind(this));
$$('.UI_AMO_Upload_New_Addon a').addEvent('click', this.uploadToAMO.bind(this));
$$('.UI_AMO_Info').each(this.getStatusFromAMO, this);
},
/*
@ -52,7 +50,11 @@ FlightDeck = Class.refactor(FlightDeck, {
'<a href="' + settings.amooauth_protocol +
'://' + settings.amooauth_domain +
'/en-US/developers/addons" target="amo_dashboard">AMO dashboard</a>');
}
this.getStatus.delay(5000, this, el.getParent('.UI_AMO_Info'));
}.bind(this),
addOnFailure: function() {
this.getStatus.delay(500, this, el.getParent('.UI_AMO_Info'));
}.bind(this)
}).send();
},
@ -81,6 +83,35 @@ FlightDeck = Class.refactor(FlightDeck, {
}).send();
},
/*
* Method: getStatus
* pull Add-o status and update data on the page
*/
getStatus: function(status_el) {
var pk = status_el.get('data-revision_id');
new Request.JSON({
url: status_el.get('data-get_addon_info_url'),
useSpinner: true,
spinnerTarget: status_el.getElements('h2')[0],
spinnerOptions: {
img: {
'class': 'spinner-img spinner-16'
},
maskBorder: false
},
onSuccess: function(response) {
this.updateStatus(status_el, response);
if (!status_el.get('data-uploaded')) {
status_el.set('data-uploaded', 1)
}
// repeat every 10s if still no answer from AMO was
// saved
if (response.status_code && response.status_code == -1) {
this.getStatus.delay(10000, this, status_el);
}
}.bind(this)
}).send();
},
/*
* Method: updateStatus
* update data on the page
@ -91,5 +122,19 @@ FlightDeck = Class.refactor(FlightDeck, {
};
if (data.status) update('.amo-review_status', data.status);
if (data.version) update('.amo-latest_version', data.version);
if (data.pk) status_el.set('data-revision_id', data.pk) ;
if (data.hasOwnProperty('uploaded')) {
status_el.set('data-uploaded', data.uploaded);
if (data.uploaded) {
// remove ability to upload
var li_anchor = $$('.upload_link')[0],
anchor = li_anchor.getElement('a');
li_anchor.set('text', anchor.get('text'));
anchor.destroy();
li_anchor.removeClass('UI_AMO_Version_Uploaded').removeClass('UI_AMO_Version_Uploaded');
li_anchor.addClass('UI_AMO_Version_Uploaded');
li_anchor.highlight();
}
}
}
});