diff --git a/apps/zadmin/tests/test_views.py b/apps/zadmin/tests/test_views.py index 54dcb1a3acd..226e67db78b 100644 --- a/apps/zadmin/tests/test_views.py +++ b/apps/zadmin/tests/test_views.py @@ -24,7 +24,7 @@ from versions.models import Version from zadmin.forms import NotifyForm from zadmin.models import ValidationJob, ValidationResult, EmailPreviewTopic -from zadmin.views import completed_versions_dirty +from zadmin.views import completed_versions_dirty, find_files from zadmin import tasks @@ -641,6 +641,69 @@ def test_max_version_override(self, validate): eq_(validate.call_args[1]['overrides'], {"targetapp_maxVersion": {amo.FIREFOX.guid: '3.7a4'}}) + def create_version(self, addon, statuses): + version = Version.objects.create(addon=addon) + for status in statuses: + file = File.objects.create(status=status, version=version) + return version + + def test_getting_status(self): + version = self.create_version(self.addon, [amo.STATUS_PUBLIC, + amo.STATUS_NOMINATED]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(version.files.all()[0].pk, ids[0]) + + def test_getting_status_lite_and_nominated(self): + version = self.create_version(self.addon, + [amo.STATUS_PUBLIC, + amo.STATUS_LITE_AND_NOMINATED]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(version.files.all()[0].pk, ids[0]) + + def test_getting_latest_public(self): + old_version = self.create_version(self.addon, [amo.STATUS_PUBLIC]) + new_version = self.create_version(self.addon, [amo.STATUS_NULL]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(old_version.files.all()[0].pk, ids[0]) + + def test_getting_latest_public_order(self): + old_version = self.create_version(self.addon, [amo.STATUS_PURGATORY]) + new_version = self.create_version(self.addon, [amo.STATUS_PUBLIC]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(new_version.files.all()[0].pk, ids[0]) + + def test_getting_w_pending(self): + old_version = self.create_version(self.addon, [amo.STATUS_PUBLIC]) + new_version = self.create_version(self.addon, [amo.STATUS_PENDING]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 2) + eq_([old_version.files.all()[0].pk, new_version.files.all()[0].pk], + ids) + + def test_multiple_files(self): + version = self.create_version(self.addon, [amo.STATUS_LISTED, + amo.STATUS_LISTED, + amo.STATUS_LISTED]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 3) + + def test_multiple_public(self): + old_version = self.create_version(self.addon, [amo.STATUS_PUBLIC]) + new_version = self.create_version(self.addon, [amo.STATUS_PUBLIC]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(new_version.files.all()[0].pk, ids[0]) + + def test_multiple_addons(self): + addon = Addon.objects.create(type=amo.ADDON_EXTENSION) + version = self.create_version(addon, [amo.STATUS_PURGATORY]) + ids = find_files(amo.FIREFOX, self.curr_max) + eq_(len(ids), 1) + eq_(self.version.files.all()[0].pk, ids[0]) def test_settings(): # Are you there, settings page? diff --git a/apps/zadmin/views.py b/apps/zadmin/views.py index 0d90f3eb71b..760cfa8d8a8 100644 --- a/apps/zadmin/views.py +++ b/apps/zadmin/views.py @@ -164,6 +164,32 @@ def validation(request, form=None): 'validation_jobs': jobs}) +def find_files(application, curr_max_version): + # TODO(someone): optimise this, this is going to be horribly slow. + # Can we just get addons with transforms and use current version? + # Or this should be chunked into the task. + statuses = [amo.STATUS_PUBLIC, amo.STATUS_LISTED] + statuses_pending = list(amo.UNREVIEWED_STATUSES) + [amo.STATUS_PENDING] + ids = [] + addons = (Addon.objects.filter(status__in=statuses, + disabled_by_user=False, + versions__files__status__in=statuses, + versions__apps__application=application.id, + versions__apps__max=curr_max_version.id) + .no_transforms()) + for addon in addons: + latest = (addon.versions.filter(files__status__in=statuses) + .order_by('-id')[0]) + ids.extend([l.pk for l in latest.files.filter(status__in=statuses)]) + pending = (addon.versions.filter(files__status__in=statuses_pending, + id__gt=latest.id)) + for version in pending: + ids.extend(version.files.filter(status__in=statuses_pending) + .values_list('id', flat=True)) + + return ids + + @admin.site.admin_view @transaction.commit_manually def start_validation(request): @@ -173,28 +199,10 @@ def start_validation(request): job = form.save(commit=False) job.creator = get_user() job.save() - - sql = """ - select files.id - from files - join versions v on v.id=files.version_id - join addons a on a.id=v.addon_id - join applications_versions av on av.version_id=v.id - where - av.application_id = %(application_id)s - and av.max = %(curr_max_version)s - and NOT a.inactive - and a.status NOT IN %(inactive_statuses)s - and files.status NOT IN %(inactive_statuses)s""" - cursor = connection.cursor() - cursor.execute(sql, {'application_id': job.application.id, - 'curr_max_version': job.curr_max_version.id, - 'inactive_statuses': [amo.STATUS_DISABLED, - amo.STATUS_NULL]}) results = [] - for row in cursor: + for id in find_files(job.application, job.curr_max_version): res = ValidationResult.objects.create(validation_job=job, - file_id=row[0]) + file_id=id) results.append(res.id) transaction.commit() except: