2010-02-25 20:00:03 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-03-09 04:11:26 +04:00
|
|
|
import datetime
|
2013-07-02 22:16:09 +04:00
|
|
|
import json
|
2010-11-12 19:51:52 +03:00
|
|
|
import os
|
2010-04-14 02:00:03 +04:00
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
import django.dispatch
|
2010-11-12 19:51:52 +03:00
|
|
|
from django.conf import settings
|
2012-09-19 21:48:26 +04:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2012-02-17 05:14:10 +04:00
|
|
|
from django.core.files.storage import default_storage as storage
|
2009-10-23 02:38:11 +04:00
|
|
|
from django.db import models
|
|
|
|
|
2010-02-04 08:13:44 +03:00
|
|
|
import caching.base
|
2013-02-15 04:20:10 +04:00
|
|
|
import commonware.log
|
|
|
|
import jinja2
|
2010-02-04 08:13:44 +03:00
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
import addons.query
|
2010-11-04 23:22:57 +03:00
|
|
|
import amo
|
2010-01-29 04:59:26 +03:00
|
|
|
import amo.models
|
2011-01-12 23:03:39 +03:00
|
|
|
import amo.utils
|
2010-11-04 23:22:57 +03:00
|
|
|
from amo.urlresolvers import reverse
|
2010-01-23 03:52:41 +03:00
|
|
|
from applications.models import Application, AppVersion
|
2010-11-20 00:27:56 +03:00
|
|
|
from files import utils
|
2012-03-24 01:37:25 +04:00
|
|
|
from files.models import File, Platform, cleanup_file
|
2012-04-02 22:05:32 +04:00
|
|
|
from tower import ugettext as _
|
2013-03-06 00:29:33 +04:00
|
|
|
from translations.fields import (LinkifiedField, PurifiedField, save_signal,
|
|
|
|
TranslatedField)
|
2010-02-04 08:13:44 +03:00
|
|
|
from users.models import UserProfile
|
2013-05-07 02:08:40 +04:00
|
|
|
from versions.tasks import update_supported_locales_single
|
2009-10-23 02:38:11 +04:00
|
|
|
|
2012-04-03 03:22:26 +04:00
|
|
|
from .compare import version_dict, version_int
|
2010-02-25 21:46:19 +03:00
|
|
|
|
2010-11-12 20:42:36 +03:00
|
|
|
log = commonware.log.getLogger('z.versions')
|
|
|
|
|
2009-10-23 02:38:11 +04:00
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
class VersionManager(amo.models.ManagerBase):
|
|
|
|
|
|
|
|
def __init__(self, include_deleted=False):
|
|
|
|
amo.models.ManagerBase.__init__(self)
|
|
|
|
self.include_deleted = include_deleted
|
|
|
|
|
|
|
|
def get_query_set(self):
|
|
|
|
qs = super(VersionManager, self).get_query_set()
|
|
|
|
qs = qs._clone(klass=addons.query.IndexQuerySet)
|
|
|
|
if not self.include_deleted:
|
|
|
|
qs = qs.exclude(deleted=True)
|
|
|
|
return qs.transform(Version.transformer)
|
|
|
|
|
|
|
|
|
2010-01-29 04:59:26 +03:00
|
|
|
class Version(amo.models.ModelBase):
|
2010-04-14 02:00:03 +04:00
|
|
|
addon = models.ForeignKey('addons.Addon', related_name='versions')
|
2010-01-20 20:45:05 +03:00
|
|
|
license = models.ForeignKey('License', null=True)
|
2010-02-23 22:47:16 +03:00
|
|
|
releasenotes = PurifiedField()
|
2010-03-06 04:37:13 +03:00
|
|
|
approvalnotes = models.TextField(default='', null=True)
|
2010-11-20 06:18:56 +03:00
|
|
|
version = models.CharField(max_length=255, default='0.1')
|
2011-02-23 02:10:45 +03:00
|
|
|
version_int = models.BigIntegerField(null=True, editable=False)
|
2009-10-23 02:38:11 +04:00
|
|
|
|
2011-03-08 00:19:23 +03:00
|
|
|
nomination = models.DateTimeField(null=True)
|
2011-03-03 01:52:57 +03:00
|
|
|
reviewed = models.DateTimeField(null=True)
|
|
|
|
|
2011-07-29 04:28:39 +04:00
|
|
|
has_info_request = models.BooleanField(default=False)
|
|
|
|
has_editor_comment = models.BooleanField(default=False)
|
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
deleted = models.BooleanField(default=False)
|
|
|
|
|
2013-05-01 03:46:24 +04:00
|
|
|
supported_locales = models.CharField(max_length=255)
|
|
|
|
|
2013-06-06 20:13:03 +04:00
|
|
|
_developer_name = models.CharField(max_length=255, default='',
|
|
|
|
editable=False)
|
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
objects = VersionManager()
|
|
|
|
with_deleted = VersionManager(include_deleted=True)
|
|
|
|
|
2010-01-29 04:59:26 +03:00
|
|
|
class Meta(amo.models.ModelBase.Meta):
|
2009-10-23 02:38:11 +04:00
|
|
|
db_table = 'versions'
|
2010-03-04 00:30:00 +03:00
|
|
|
ordering = ['-created', '-modified']
|
2010-02-02 23:22:24 +03:00
|
|
|
|
2010-02-25 21:46:19 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Version, self).__init__(*args, **kwargs)
|
2012-04-03 03:22:26 +04:00
|
|
|
self.__dict__.update(version_dict(self.version or ''))
|
2010-02-25 21:46:19 +03:00
|
|
|
|
2010-02-18 02:56:17 +03:00
|
|
|
def __unicode__(self):
|
2010-12-16 23:01:15 +03:00
|
|
|
return jinja2.escape(self.version)
|
2010-02-18 02:56:17 +03:00
|
|
|
|
2011-02-23 02:10:45 +03:00
|
|
|
def save(self, *args, **kw):
|
|
|
|
if not self.version_int and self.version:
|
2012-04-03 03:22:26 +04:00
|
|
|
v_int = version_int(self.version)
|
2011-02-23 02:10:45 +03:00
|
|
|
# Magic number warning, this is the maximum size
|
|
|
|
# of a big int in MySQL to prevent version_int overflow, for
|
|
|
|
# people who have rather crazy version numbers.
|
|
|
|
# http://dev.mysql.com/doc/refman/5.5/en/numeric-types.html
|
2012-04-03 03:22:26 +04:00
|
|
|
if v_int < 9223372036854775807:
|
|
|
|
self.version_int = v_int
|
2011-02-23 02:10:45 +03:00
|
|
|
else:
|
|
|
|
log.error('No version_int written for version %s, %s' %
|
|
|
|
(self.pk, self.version))
|
2013-06-07 22:02:45 +04:00
|
|
|
creating = not self.id
|
|
|
|
super(Version, self).save(*args, **kw)
|
|
|
|
if creating:
|
2013-07-02 22:16:09 +04:00
|
|
|
# To avoid circular import.
|
2013-06-07 22:02:45 +04:00
|
|
|
from mkt.webapps.models import AppFeatures
|
|
|
|
if self.addon.type == amo.ADDON_WEBAPP:
|
|
|
|
AppFeatures.objects.create(version=self)
|
|
|
|
return self
|
2011-02-23 02:10:45 +03:00
|
|
|
|
2010-11-12 20:42:36 +03:00
|
|
|
@classmethod
|
2011-06-07 23:47:56 +04:00
|
|
|
def from_upload(cls, upload, addon, platforms, send_signal=True):
|
2011-08-03 02:09:20 +04:00
|
|
|
data = utils.parse_addon(upload, addon)
|
2010-11-16 04:47:15 +03:00
|
|
|
try:
|
|
|
|
license = addon.versions.latest().license_id
|
|
|
|
except Version.DoesNotExist:
|
|
|
|
license = None
|
2013-06-06 20:13:03 +04:00
|
|
|
max_len = cls._meta.get_field_by_name('_developer_name')[0].max_length
|
|
|
|
developer = data.get('developer_name', '')[:max_len]
|
2010-11-16 04:47:15 +03:00
|
|
|
v = cls.objects.create(addon=addon, version=data['version'],
|
2013-06-06 20:13:03 +04:00
|
|
|
license_id=license, _developer_name=developer)
|
2011-05-21 01:50:17 +04:00
|
|
|
log.info('New version: %r (%s) from %r' % (v, v.id, upload))
|
2013-07-02 22:16:09 +04:00
|
|
|
|
2010-11-12 20:42:36 +03:00
|
|
|
AV = ApplicationsVersions
|
2010-12-09 02:18:30 +03:00
|
|
|
for app in data.get('apps', []):
|
2010-11-12 20:42:36 +03:00
|
|
|
AV(version=v, min=app.min, max=app.max,
|
|
|
|
application_id=app.id).save()
|
2012-06-02 05:45:56 +04:00
|
|
|
if addon.type in [amo.ADDON_SEARCH, amo.ADDON_WEBAPP]:
|
2012-08-01 02:28:50 +04:00
|
|
|
# Search extensions and webapps are always for all platforms.
|
2011-01-05 03:37:26 +03:00
|
|
|
platforms = [Platform.objects.get(id=amo.PLATFORM_ALL.id)]
|
2011-03-24 21:48:57 +03:00
|
|
|
else:
|
2011-04-15 00:26:08 +04:00
|
|
|
platforms = cls._make_safe_platform_files(platforms)
|
2011-03-24 21:48:57 +03:00
|
|
|
|
2013-07-02 22:16:09 +04:00
|
|
|
if addon.is_webapp():
|
|
|
|
from mkt.webapps.models import AppManifest
|
|
|
|
|
|
|
|
# Create AppManifest if we're a Webapp.
|
|
|
|
# Note: This must happen before we call `File.from_upload`.
|
|
|
|
manifest = utils.WebAppParser().get_json_data(upload)
|
|
|
|
AppManifest.objects.create(
|
|
|
|
version=v, manifest=json.dumps(manifest))
|
|
|
|
|
2011-01-04 21:13:24 +03:00
|
|
|
for platform in platforms:
|
2013-06-01 00:46:02 +04:00
|
|
|
File.from_upload(upload, v, platform, parse_data=data)
|
2013-05-01 03:46:24 +04:00
|
|
|
|
2013-07-02 22:16:09 +04:00
|
|
|
if addon.is_webapp():
|
|
|
|
# Update supported locales from manifest.
|
|
|
|
# Note: This needs to happen after we call `File.from_upload`.
|
2013-05-07 02:08:40 +04:00
|
|
|
update_supported_locales_single.apply_async(
|
|
|
|
args=[addon.id], kwargs={'latest': True},
|
|
|
|
eta=datetime.datetime.now() +
|
|
|
|
datetime.timedelta(seconds=settings.NFS_LAG_DELAY))
|
|
|
|
|
2011-03-22 03:27:48 +03:00
|
|
|
v.disable_old_files()
|
2012-08-01 02:28:50 +04:00
|
|
|
# After the upload has been copied to all platforms, remove the upload.
|
2012-02-17 05:14:10 +04:00
|
|
|
storage.delete(upload.path)
|
2011-06-07 23:47:56 +04:00
|
|
|
if send_signal:
|
|
|
|
version_uploaded.send(sender=v)
|
2012-10-13 02:47:39 +04:00
|
|
|
|
2013-08-01 18:18:17 +04:00
|
|
|
# If packaged app and app is blocked, put in escalation queue.
|
|
|
|
if (addon.is_webapp() and addon.is_packaged and
|
|
|
|
addon.status == amo.STATUS_BLOCKED):
|
|
|
|
# To avoid circular import.
|
|
|
|
from editors.models import EscalationQueue
|
|
|
|
EscalationQueue.objects.create(addon=addon)
|
2012-10-13 02:47:39 +04:00
|
|
|
|
2010-11-12 20:42:36 +03:00
|
|
|
return v
|
|
|
|
|
2011-04-15 00:26:08 +04:00
|
|
|
@classmethod
|
|
|
|
def _make_safe_platform_files(cls, platforms):
|
|
|
|
"""Make file platform translations until all download pages
|
|
|
|
support desktop ALL + mobile ALL. See bug 646268.
|
|
|
|
"""
|
|
|
|
pl_set = set([p.id for p in platforms])
|
|
|
|
|
|
|
|
if pl_set == set([amo.PLATFORM_ALL_MOBILE.id, amo.PLATFORM_ALL.id]):
|
|
|
|
# Make it really ALL:
|
|
|
|
return [Platform.objects.get(id=amo.PLATFORM_ALL.id)]
|
|
|
|
|
|
|
|
has_mobile = any(p in amo.MOBILE_PLATFORMS for p in pl_set)
|
|
|
|
has_desktop = any(p in amo.DESKTOP_PLATFORMS for p in pl_set)
|
|
|
|
has_all = any(p in (amo.PLATFORM_ALL_MOBILE.id,
|
|
|
|
amo.PLATFORM_ALL.id) for p in pl_set)
|
|
|
|
is_mixed = has_mobile and has_desktop
|
|
|
|
if (is_mixed and has_all) or has_mobile:
|
|
|
|
# Mixing desktop and mobile w/ ALL is not safe;
|
|
|
|
# we have to split the files into exact platforms.
|
|
|
|
# Additionally, it is not safe to use all-mobile.
|
|
|
|
new_plats = []
|
|
|
|
for p in platforms:
|
|
|
|
if p.id == amo.PLATFORM_ALL_MOBILE.id:
|
|
|
|
new_plats.extend(list(Platform.objects
|
|
|
|
.filter(id__in=amo.MOBILE_PLATFORMS)
|
|
|
|
.exclude(id=amo.PLATFORM_ALL_MOBILE.id)))
|
|
|
|
elif p.id == amo.PLATFORM_ALL.id:
|
|
|
|
new_plats.extend(list(Platform.objects
|
|
|
|
.filter(id__in=amo.DESKTOP_PLATFORMS)
|
|
|
|
.exclude(id=amo.PLATFORM_ALL.id)))
|
|
|
|
else:
|
|
|
|
new_plats.append(p)
|
|
|
|
return new_plats
|
|
|
|
|
|
|
|
# Platforms are safe as is
|
|
|
|
return platforms
|
|
|
|
|
2010-11-12 19:51:52 +03:00
|
|
|
@property
|
|
|
|
def path_prefix(self):
|
|
|
|
return os.path.join(settings.ADDONS_PATH, str(self.addon_id))
|
|
|
|
|
2011-01-28 01:53:53 +03:00
|
|
|
@property
|
|
|
|
def mirror_path_prefix(self):
|
|
|
|
return os.path.join(settings.MIRROR_STAGE_PATH, str(self.addon_id))
|
|
|
|
|
2011-07-23 01:31:31 +04:00
|
|
|
def license_url(self, impala=False):
|
2011-08-25 22:50:09 +04:00
|
|
|
return reverse('addons.license', args=[self.addon.slug, self.version])
|
2010-10-23 04:30:58 +04:00
|
|
|
|
2010-08-13 20:24:36 +04:00
|
|
|
def flush_urls(self):
|
|
|
|
return self.addon.flush_urls()
|
|
|
|
|
2010-11-06 00:48:05 +03:00
|
|
|
def get_url_path(self):
|
2010-12-20 23:47:01 +03:00
|
|
|
return reverse('addons.versions', args=[self.addon.slug, self.version])
|
2010-11-06 00:48:05 +03:00
|
|
|
|
2010-11-04 23:22:57 +03:00
|
|
|
def delete(self):
|
2012-09-13 20:39:47 +04:00
|
|
|
log.info(u'Version deleted: %r (%s)' % (self, self.id))
|
2010-11-20 00:51:36 +03:00
|
|
|
amo.log(amo.LOG.DELETE_VERSION, self.addon, str(self.version))
|
2013-02-15 04:20:10 +04:00
|
|
|
if settings.MARKETPLACE:
|
|
|
|
self.update(deleted=True)
|
2013-02-21 20:55:11 +04:00
|
|
|
if self.addon.is_packaged:
|
|
|
|
f = self.all_files[0]
|
|
|
|
# Unlink signed packages if packaged app.
|
|
|
|
storage.delete(f.signed_file_path)
|
|
|
|
log.info(u'Unlinked file: %s' % f.signed_file_path)
|
|
|
|
storage.delete(f.signed_reviewer_file_path)
|
|
|
|
log.info(u'Unlinked file: %s' % f.signed_reviewer_file_path)
|
|
|
|
|
2013-02-15 04:20:10 +04:00
|
|
|
else:
|
|
|
|
super(Version, self).delete()
|
2010-11-04 23:22:57 +03:00
|
|
|
|
2011-06-14 03:43:00 +04:00
|
|
|
@property
|
|
|
|
def current_queue(self):
|
|
|
|
"""Return the current queue, or None if not in a queue."""
|
|
|
|
from editors.models import (ViewPendingQueue, ViewFullReviewQueue,
|
|
|
|
ViewPreliminaryQueue)
|
|
|
|
|
|
|
|
if self.addon.status in [amo.STATUS_NOMINATED,
|
|
|
|
amo.STATUS_LITE_AND_NOMINATED]:
|
|
|
|
return ViewFullReviewQueue
|
|
|
|
elif self.addon.status == amo.STATUS_PUBLIC:
|
|
|
|
return ViewPendingQueue
|
|
|
|
elif self.addon.status in [amo.STATUS_LITE, amo.STATUS_UNREVIEWED]:
|
|
|
|
return ViewPreliminaryQueue
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2011-05-27 01:13:13 +04:00
|
|
|
@amo.cached_property(writable=True)
|
|
|
|
def all_activity(self):
|
|
|
|
from devhub.models import VersionLog # yucky
|
|
|
|
al = (VersionLog.objects.filter(version=self.id).order_by('created')
|
|
|
|
.select_related(depth=1).no_cache())
|
|
|
|
return al
|
|
|
|
|
2010-04-14 02:00:03 +04:00
|
|
|
@amo.cached_property(writable=True)
|
2010-02-04 09:15:02 +03:00
|
|
|
def compatible_apps(self):
|
|
|
|
"""Get a mapping of {APP: ApplicationVersion}."""
|
2010-04-17 03:19:49 +04:00
|
|
|
avs = self.apps.select_related(depth=1)
|
2010-04-14 02:00:03 +04:00
|
|
|
return self._compat_map(avs)
|
|
|
|
|
2012-01-04 01:59:53 +04:00
|
|
|
@amo.cached_property
|
2012-01-04 01:44:27 +04:00
|
|
|
def compatible_apps_ordered(self):
|
|
|
|
apps = self.compatible_apps.items()
|
|
|
|
return sorted(apps, key=lambda v: v[0].short)
|
|
|
|
|
2011-02-24 22:01:35 +03:00
|
|
|
def compatible_platforms(self):
|
|
|
|
"""Returns a dict of compatible file platforms for this version.
|
|
|
|
|
|
|
|
The result is based on which app(s) the version targets.
|
|
|
|
"""
|
|
|
|
apps = set([a.application.id for a in self.apps.all()])
|
|
|
|
targets_mobile = amo.MOBILE.id in apps
|
|
|
|
targets_other = any((a != amo.MOBILE.id) for a in apps)
|
|
|
|
all_plats = {}
|
|
|
|
if targets_other:
|
2011-03-24 21:48:57 +03:00
|
|
|
all_plats.update(amo.DESKTOP_PLATFORMS)
|
2011-02-24 22:01:35 +03:00
|
|
|
if targets_mobile:
|
|
|
|
all_plats.update(amo.MOBILE_PLATFORMS)
|
|
|
|
return all_plats
|
|
|
|
|
2012-03-26 04:17:17 +04:00
|
|
|
@amo.cached_property
|
|
|
|
def is_compatible(self):
|
2012-04-06 23:55:11 +04:00
|
|
|
"""Returns tuple of compatibility and reasons why if not.
|
2012-03-26 04:17:17 +04:00
|
|
|
|
|
|
|
Server side conditions for determining compatibility are:
|
|
|
|
* The add-on is an extension (not a theme, app, etc.)
|
|
|
|
* Has not opted in to strict compatibility.
|
|
|
|
* Does not use binary_components in chrome.manifest.
|
|
|
|
|
2012-04-16 23:46:43 +04:00
|
|
|
Note: The lowest maxVersion compat check needs to be checked
|
|
|
|
separately.
|
2012-03-26 04:17:17 +04:00
|
|
|
Note: This does not take into account the client conditions.
|
|
|
|
|
|
|
|
"""
|
2012-04-06 23:55:11 +04:00
|
|
|
compat = True
|
|
|
|
reasons = []
|
|
|
|
if self.addon.type != amo.ADDON_EXTENSION:
|
|
|
|
compat = False
|
|
|
|
# TODO: We may want this. For now we think it may be confusing.
|
|
|
|
# reasons.append(_('Add-on is not an extension.'))
|
|
|
|
if self.files.filter(binary_components=True).exists():
|
|
|
|
compat = False
|
|
|
|
reasons.append(_('Add-on uses binary components.'))
|
|
|
|
if self.files.filter(strict_compatibility=True).exists():
|
|
|
|
compat = False
|
|
|
|
reasons.append(_('Add-on has opted into strict compatibility '
|
|
|
|
'checking.'))
|
|
|
|
return (compat, reasons)
|
2012-04-03 22:55:40 +04:00
|
|
|
|
|
|
|
def is_compatible_app(self, app):
|
|
|
|
"""Returns True if the provided app passes compatibility conditions."""
|
2012-04-04 01:47:09 +04:00
|
|
|
appversion = self.compatible_apps.get(app)
|
2012-05-22 02:48:59 +04:00
|
|
|
if appversion and app.id in amo.D2C_MAX_VERSIONS:
|
2012-04-03 22:55:40 +04:00
|
|
|
return (version_int(appversion.max.version) >=
|
2012-05-22 02:48:59 +04:00
|
|
|
version_int(amo.D2C_MAX_VERSIONS.get(app.id, '*')))
|
2012-04-03 22:55:40 +04:00
|
|
|
return False
|
2012-03-26 04:17:17 +04:00
|
|
|
|
|
|
|
def compat_override_app_versions(self):
|
|
|
|
"""Returns the incompatible app versions range(s).
|
|
|
|
|
|
|
|
If not ranges, returns empty list. Otherwise, this will return all
|
|
|
|
the app version ranges that this particular version is incompatible
|
|
|
|
with.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from addons.models import CompatOverride
|
|
|
|
cos = CompatOverride.objects.filter(addon=self.addon)
|
|
|
|
if not cos:
|
|
|
|
return []
|
|
|
|
app_versions = []
|
|
|
|
for co in cos:
|
|
|
|
for range in co.collapsed_ranges():
|
2012-04-03 03:22:26 +04:00
|
|
|
if (version_int(range.min) <= version_int(self.version)
|
|
|
|
<= version_int(range.max)):
|
2012-03-26 04:17:17 +04:00
|
|
|
app_versions.extend([(a.min, a.max) for a in range.apps])
|
|
|
|
return app_versions
|
|
|
|
|
2010-04-14 02:00:03 +04:00
|
|
|
@amo.cached_property(writable=True)
|
|
|
|
def all_files(self):
|
|
|
|
"""Shortcut for list(self.files.all()). Heavily cached."""
|
|
|
|
return list(self.files.all())
|
2010-02-04 09:15:02 +03:00
|
|
|
|
2010-02-06 01:45:23 +03:00
|
|
|
@amo.cached_property
|
2010-02-04 09:15:02 +03:00
|
|
|
def supported_platforms(self):
|
|
|
|
"""Get a list of supported platform names."""
|
2011-07-09 05:44:02 +04:00
|
|
|
return list(set(amo.PLATFORMS[f.platform_id] for f in self.all_files))
|
2009-12-31 03:57:36 +03:00
|
|
|
|
2011-06-17 04:21:27 +04:00
|
|
|
@property
|
|
|
|
def status(self):
|
2013-02-15 04:20:10 +04:00
|
|
|
if settings.MARKETPLACE and self.deleted:
|
|
|
|
return [amo.STATUS_CHOICES[amo.STATUS_DELETED]]
|
|
|
|
else:
|
|
|
|
return [amo.STATUS_CHOICES[f.status] for f in self.all_files]
|
2011-06-17 04:21:27 +04:00
|
|
|
|
2011-09-02 23:14:02 +04:00
|
|
|
@property
|
|
|
|
def statuses(self):
|
|
|
|
"""Unadulterated statuses, good for an API."""
|
2011-10-03 22:18:34 +04:00
|
|
|
return [(f.id, f.status) for f in self.all_files]
|
2011-09-02 23:14:02 +04:00
|
|
|
|
2010-11-30 02:36:12 +03:00
|
|
|
def is_allowed_upload(self):
|
|
|
|
"""Check that a file can be uploaded based on the files
|
|
|
|
per platform for that type of addon."""
|
2013-02-15 04:20:10 +04:00
|
|
|
|
2011-03-24 21:48:57 +03:00
|
|
|
num_files = len(self.all_files)
|
2010-11-30 02:36:12 +03:00
|
|
|
if self.addon.type == amo.ADDON_SEARCH:
|
2011-03-24 21:48:57 +03:00
|
|
|
return num_files == 0
|
|
|
|
elif num_files == 0:
|
|
|
|
return True
|
2010-12-21 01:45:02 +03:00
|
|
|
elif amo.PLATFORM_ALL in self.supported_platforms:
|
|
|
|
return False
|
2011-03-24 21:48:57 +03:00
|
|
|
elif amo.PLATFORM_ALL_MOBILE in self.supported_platforms:
|
|
|
|
return False
|
2010-11-30 02:36:12 +03:00
|
|
|
else:
|
2011-03-24 21:48:57 +03:00
|
|
|
compatible = (v for k, v in self.compatible_platforms().items()
|
|
|
|
if k not in (amo.PLATFORM_ALL.id,
|
|
|
|
amo.PLATFORM_ALL_MOBILE.id))
|
|
|
|
return bool(set(compatible) - set(self.supported_platforms))
|
2010-11-30 02:36:12 +03:00
|
|
|
|
2011-01-05 22:44:27 +03:00
|
|
|
@property
|
2010-03-02 16:34:07 +03:00
|
|
|
def has_files(self):
|
2010-04-14 02:00:03 +04:00
|
|
|
return bool(self.all_files)
|
2010-03-02 16:34:07 +03:00
|
|
|
|
2011-01-05 22:44:27 +03:00
|
|
|
@property
|
2010-03-09 22:50:56 +03:00
|
|
|
def is_unreviewed(self):
|
2010-06-24 20:22:48 +04:00
|
|
|
return filter(lambda f: f.status in amo.UNREVIEWED_STATUSES,
|
2010-04-14 02:00:03 +04:00
|
|
|
self.all_files)
|
|
|
|
|
2011-05-05 05:18:27 +04:00
|
|
|
@property
|
|
|
|
def is_all_unreviewed(self):
|
|
|
|
return not bool([f for f in self.all_files if f.status not in
|
|
|
|
amo.UNREVIEWED_STATUSES])
|
|
|
|
|
2011-01-05 22:44:27 +03:00
|
|
|
@property
|
2010-06-23 22:44:16 +04:00
|
|
|
def is_beta(self):
|
2011-01-05 22:44:27 +03:00
|
|
|
return filter(lambda f: f.status == amo.STATUS_BETA, self.all_files)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_lite(self):
|
|
|
|
return filter(lambda f: f.status in amo.LITE_STATUSES, self.all_files)
|
2010-06-23 22:44:16 +04:00
|
|
|
|
2011-05-17 01:47:53 +04:00
|
|
|
@property
|
|
|
|
def is_jetpack(self):
|
2011-05-17 03:10:21 +04:00
|
|
|
return all(f.jetpack_version for f in self.all_files)
|
2011-05-17 01:47:53 +04:00
|
|
|
|
2010-04-14 02:00:03 +04:00
|
|
|
@classmethod
|
|
|
|
def _compat_map(cls, avs):
|
|
|
|
apps = {}
|
|
|
|
for av in avs:
|
|
|
|
app_id = av.application_id
|
|
|
|
if app_id in amo.APP_IDS:
|
|
|
|
apps[amo.APP_IDS[app_id]] = av
|
|
|
|
return apps
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def transformer(cls, versions):
|
|
|
|
"""Attach all the compatible apps and files to the versions."""
|
2011-01-12 23:03:39 +03:00
|
|
|
ids = set(v.id for v in versions)
|
2010-04-14 02:00:03 +04:00
|
|
|
if not versions:
|
|
|
|
return
|
|
|
|
|
2011-01-12 23:03:39 +03:00
|
|
|
avs = (ApplicationsVersions.objects.filter(version__in=ids)
|
2011-01-13 22:34:13 +03:00
|
|
|
.select_related(depth=1).no_cache())
|
|
|
|
files = (File.objects.filter(version__in=ids)
|
|
|
|
.select_related('version').no_cache())
|
2010-04-14 02:00:03 +04:00
|
|
|
|
|
|
|
def rollup(xs):
|
2011-01-12 23:03:39 +03:00
|
|
|
groups = amo.utils.sorted_groupby(xs, 'version_id')
|
2010-04-14 02:00:03 +04:00
|
|
|
return dict((k, list(vs)) for k, vs in groups)
|
|
|
|
|
|
|
|
av_dict, file_dict = rollup(avs), rollup(files)
|
|
|
|
|
|
|
|
for version in versions:
|
|
|
|
v_id = version.id
|
|
|
|
version.compatible_apps = cls._compat_map(av_dict.get(v_id, []))
|
|
|
|
version.all_files = file_dict.get(v_id, [])
|
2010-03-09 22:50:56 +03:00
|
|
|
|
2011-05-27 01:13:13 +04:00
|
|
|
@classmethod
|
|
|
|
def transformer_activity(cls, versions):
|
|
|
|
"""Attach all the activity to the versions."""
|
|
|
|
from devhub.models import VersionLog # yucky
|
|
|
|
|
|
|
|
ids = set(v.id for v in versions)
|
|
|
|
if not versions:
|
|
|
|
return
|
|
|
|
|
|
|
|
al = (VersionLog.objects.filter(version__in=ids).order_by('created')
|
|
|
|
.select_related(depth=1).no_cache())
|
|
|
|
|
|
|
|
def rollup(xs):
|
|
|
|
groups = amo.utils.sorted_groupby(xs, 'version_id')
|
|
|
|
return dict((k, list(vs)) for k, vs in groups)
|
|
|
|
|
|
|
|
al_dict = rollup(al)
|
|
|
|
|
|
|
|
for version in versions:
|
|
|
|
v_id = version.id
|
|
|
|
version.all_activity = al_dict.get(v_id, [])
|
|
|
|
|
2011-03-22 03:27:48 +03:00
|
|
|
def disable_old_files(self):
|
|
|
|
if not self.files.filter(status=amo.STATUS_BETA).exists():
|
|
|
|
qs = File.objects.filter(version__addon=self.addon_id,
|
2012-08-01 02:28:50 +04:00
|
|
|
version__lt=self.id,
|
2013-02-21 20:55:11 +04:00
|
|
|
version__deleted=False,
|
2012-08-01 02:28:50 +04:00
|
|
|
status__in=[amo.STATUS_UNREVIEWED,
|
|
|
|
amo.STATUS_PENDING])
|
2011-03-22 03:27:48 +03:00
|
|
|
# Use File.update so signals are triggered.
|
|
|
|
for f in qs:
|
|
|
|
f.update(status=amo.STATUS_DISABLED)
|
|
|
|
|
2013-06-06 20:13:03 +04:00
|
|
|
@property
|
|
|
|
def developer_name(self):
|
|
|
|
if self._developer_name:
|
|
|
|
return self._developer_name
|
|
|
|
elif self.addon.listed_authors:
|
|
|
|
return self.addon.listed_authors[0].name
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
2013-06-21 18:35:29 +04:00
|
|
|
@amo.cached_property
|
|
|
|
def is_privileged(self):
|
2013-09-20 03:29:17 +04:00
|
|
|
if (self.addon.type != amo.ADDON_WEBAPP or
|
|
|
|
not self.addon.is_packaged or not self.all_files):
|
2013-06-21 18:35:29 +04:00
|
|
|
return False
|
|
|
|
data = self.addon.get_manifest_json(file_obj=self.all_files[0])
|
|
|
|
return data.get('type') == 'privileged'
|
|
|
|
|
2013-07-02 22:16:09 +04:00
|
|
|
@amo.cached_property
|
|
|
|
def manifest(self):
|
|
|
|
# To avoid circular import.
|
|
|
|
from mkt.webapps.models import AppManifest
|
|
|
|
|
|
|
|
try:
|
|
|
|
manifest = self.manifest_json.manifest
|
|
|
|
except AppManifest.DoesNotExist:
|
|
|
|
manifest = None
|
|
|
|
|
|
|
|
return json.loads(manifest) if manifest else {}
|
|
|
|
|
2009-12-31 03:57:36 +03:00
|
|
|
|
2010-12-03 02:35:53 +03:00
|
|
|
def update_status(sender, instance, **kw):
|
|
|
|
if not kw.get('raw'):
|
|
|
|
try:
|
|
|
|
instance.addon.update_status(using='default')
|
2011-02-26 04:12:11 +03:00
|
|
|
instance.addon.update_version()
|
2010-12-03 02:35:53 +03:00
|
|
|
except models.ObjectDoesNotExist:
|
2013-05-08 23:12:13 +04:00
|
|
|
log.info('Got ObjectDoesNotExist processing Version change signal',
|
|
|
|
exc_info=True)
|
2010-12-03 02:35:53 +03:00
|
|
|
pass
|
2011-02-11 04:17:31 +03:00
|
|
|
|
|
|
|
|
2011-04-16 00:56:08 +04:00
|
|
|
def inherit_nomination(sender, instance, **kw):
|
|
|
|
"""For new versions pending review, ensure nomination date
|
|
|
|
is inherited from last nominated version.
|
|
|
|
"""
|
|
|
|
if kw.get('raw'):
|
|
|
|
return
|
2013-03-09 04:11:26 +04:00
|
|
|
addon = instance.addon
|
|
|
|
if (addon.type == amo.ADDON_WEBAPP and addon.is_packaged):
|
|
|
|
# If prior version's file is pending, inherit nomination. Otherwise,
|
|
|
|
# set nomination to now.
|
|
|
|
last_ver = (Version.objects.filter(addon=addon)
|
|
|
|
.exclude(pk=instance.pk)
|
|
|
|
.order_by('-nomination'))
|
|
|
|
if (last_ver.exists() and
|
|
|
|
last_ver[0].all_files[0].status == amo.STATUS_PENDING):
|
|
|
|
instance.update(nomination=last_ver[0].nomination, _signal=False)
|
|
|
|
log.debug('[Webapp:%s] Inheriting nomination from prior pending '
|
|
|
|
'version' % addon.id)
|
2013-07-16 18:29:44 +04:00
|
|
|
elif (addon.status in amo.WEBAPPS_APPROVED_STATUSES and
|
|
|
|
not instance.nomination):
|
2013-03-09 04:11:26 +04:00
|
|
|
log.debug('[Webapp:%s] Setting nomination date to now for new '
|
|
|
|
'version.' % addon.id)
|
|
|
|
instance.update(nomination=datetime.datetime.now(), _signal=False)
|
|
|
|
else:
|
|
|
|
if (instance.nomination is None
|
|
|
|
and addon.status in (amo.STATUS_NOMINATED,
|
|
|
|
amo.STATUS_LITE_AND_NOMINATED)
|
|
|
|
and not instance.is_beta):
|
|
|
|
last_ver = (Version.objects.filter(addon=addon)
|
|
|
|
.exclude(nomination=None).order_by('-nomination'))
|
|
|
|
if last_ver.exists():
|
|
|
|
instance.update(nomination=last_ver[0].nomination,
|
|
|
|
_signal=False)
|
2011-04-16 00:56:08 +04:00
|
|
|
|
|
|
|
|
2011-12-03 00:47:21 +04:00
|
|
|
def update_incompatible_versions(sender, instance, **kw):
|
|
|
|
"""When a new version is added or deleted, send to task to update if it
|
|
|
|
matches any compat overrides.
|
|
|
|
"""
|
2012-09-19 21:48:26 +04:00
|
|
|
try:
|
2013-09-19 18:08:34 +04:00
|
|
|
if not instance.addon.reload().type == amo.ADDON_EXTENSION:
|
2012-09-19 21:48:26 +04:00
|
|
|
return
|
|
|
|
except ObjectDoesNotExist:
|
2012-04-25 08:29:40 +04:00
|
|
|
return
|
|
|
|
|
2011-12-03 00:47:21 +04:00
|
|
|
from addons import tasks
|
2011-12-16 22:52:04 +04:00
|
|
|
tasks.update_incompatible_appversions.delay([instance.id])
|
2011-12-03 00:47:21 +04:00
|
|
|
|
|
|
|
|
2012-03-23 23:19:19 +04:00
|
|
|
def cleanup_version(sender, instance, **kw):
|
|
|
|
"""On delete of the version object call the file delete and signals."""
|
|
|
|
if kw.get('raw'):
|
|
|
|
return
|
2012-03-24 01:37:25 +04:00
|
|
|
for file_ in instance.files.all():
|
|
|
|
cleanup_file(file_.__class__, file_)
|
2012-03-23 23:19:19 +04:00
|
|
|
|
|
|
|
|
2012-01-24 21:16:17 +04:00
|
|
|
def clear_compatversion_cache_on_save(sender, instance, created, **kw):
|
|
|
|
"""Clears compatversion cache if new Version created."""
|
2013-06-07 22:02:45 +04:00
|
|
|
try:
|
|
|
|
if not instance.addon.type == amo.ADDON_EXTENSION:
|
|
|
|
return
|
|
|
|
except ObjectDoesNotExist:
|
2012-07-23 23:54:21 +04:00
|
|
|
return
|
|
|
|
|
2012-01-24 21:16:17 +04:00
|
|
|
if not kw.get('raw') and created:
|
|
|
|
instance.addon.invalidate_d2c_versions()
|
|
|
|
|
|
|
|
|
|
|
|
def clear_compatversion_cache_on_delete(sender, instance, **kw):
|
|
|
|
"""Clears compatversion cache when Version deleted."""
|
2012-09-19 21:48:26 +04:00
|
|
|
try:
|
|
|
|
if not instance.addon.type == amo.ADDON_EXTENSION:
|
|
|
|
return
|
|
|
|
except ObjectDoesNotExist:
|
2012-07-23 23:54:21 +04:00
|
|
|
return
|
|
|
|
|
2012-01-24 21:16:17 +04:00
|
|
|
if not kw.get('raw'):
|
|
|
|
instance.addon.invalidate_d2c_versions()
|
|
|
|
|
|
|
|
|
2011-04-13 04:36:55 +04:00
|
|
|
version_uploaded = django.dispatch.Signal()
|
2013-03-06 00:29:33 +04:00
|
|
|
models.signals.pre_save.connect(
|
|
|
|
save_signal, sender=Version, dispatch_uid='version_translations')
|
2013-03-09 04:11:26 +04:00
|
|
|
models.signals.post_save.connect(
|
|
|
|
update_status, sender=Version, dispatch_uid='version_update_status')
|
|
|
|
models.signals.post_save.connect(
|
|
|
|
inherit_nomination, sender=Version,
|
|
|
|
dispatch_uid='version_inherit_nomination')
|
|
|
|
models.signals.post_delete.connect(
|
|
|
|
update_status, sender=Version, dispatch_uid='version_update_status')
|
|
|
|
models.signals.post_save.connect(
|
|
|
|
update_incompatible_versions, sender=Version,
|
|
|
|
dispatch_uid='version_update_incompat')
|
|
|
|
models.signals.post_delete.connect(
|
|
|
|
update_incompatible_versions, sender=Version,
|
|
|
|
dispatch_uid='version_update_incompat')
|
|
|
|
models.signals.pre_delete.connect(
|
|
|
|
cleanup_version, sender=Version, dispatch_uid='cleanup_version')
|
|
|
|
models.signals.post_save.connect(
|
|
|
|
clear_compatversion_cache_on_save, sender=Version,
|
|
|
|
dispatch_uid='clear_compatversion_cache_save')
|
|
|
|
models.signals.post_delete.connect(
|
|
|
|
clear_compatversion_cache_on_delete, sender=Version,
|
|
|
|
dispatch_uid='clear_compatversion_cache_del')
|
2010-12-03 02:35:53 +03:00
|
|
|
|
|
|
|
|
2010-10-05 04:06:39 +04:00
|
|
|
class LicenseManager(amo.models.ManagerBase):
|
2010-03-01 22:21:09 +03:00
|
|
|
|
2010-10-05 04:06:39 +04:00
|
|
|
def builtins(self):
|
|
|
|
return self.filter(builtin__gt=0).order_by('builtin')
|
2009-12-31 03:57:36 +03:00
|
|
|
|
2010-10-05 04:06:39 +04:00
|
|
|
|
|
|
|
class License(amo.models.ModelBase):
|
|
|
|
OTHER = 0
|
|
|
|
|
|
|
|
name = TranslatedField(db_column='name')
|
2013-10-02 22:56:39 +04:00
|
|
|
url = models.URLField(null=True)
|
2010-10-05 04:06:39 +04:00
|
|
|
builtin = models.PositiveIntegerField(default=OTHER)
|
|
|
|
text = LinkifiedField()
|
|
|
|
on_form = models.BooleanField(default=False,
|
|
|
|
help_text='Is this a license choice in the devhub?')
|
|
|
|
some_rights = models.BooleanField(default=False,
|
|
|
|
help_text='Show "Some Rights Reserved" instead of the license name?')
|
|
|
|
icons = models.CharField(max_length=255, null=True,
|
|
|
|
help_text='Space-separated list of icon identifiers.')
|
|
|
|
|
|
|
|
objects = LicenseManager()
|
|
|
|
|
|
|
|
class Meta:
|
2009-12-31 03:57:36 +03:00
|
|
|
db_table = 'licenses'
|
2010-01-14 03:01:23 +03:00
|
|
|
|
2010-03-01 22:21:09 +03:00
|
|
|
def __unicode__(self):
|
2010-12-01 20:56:49 +03:00
|
|
|
return unicode(self.name)
|
2010-03-01 22:21:09 +03:00
|
|
|
|
2013-03-06 00:29:33 +04:00
|
|
|
models.signals.pre_save.connect(
|
|
|
|
save_signal, sender=License, dispatch_uid='version_translations')
|
|
|
|
|
2010-01-14 03:01:23 +03:00
|
|
|
|
2010-01-29 04:59:26 +03:00
|
|
|
class VersionComment(amo.models.ModelBase):
|
2010-01-14 03:01:23 +03:00
|
|
|
"""Editor comments for version discussion threads."""
|
|
|
|
version = models.ForeignKey(Version)
|
2010-01-18 13:51:34 +03:00
|
|
|
user = models.ForeignKey(UserProfile)
|
2010-08-05 00:25:09 +04:00
|
|
|
reply_to = models.ForeignKey(Version, related_name="reply_to",
|
|
|
|
db_column='reply_to', null=True)
|
2010-01-14 03:01:23 +03:00
|
|
|
subject = models.CharField(max_length=1000)
|
|
|
|
comment = models.TextField()
|
|
|
|
|
2010-01-29 04:59:26 +03:00
|
|
|
class Meta(amo.models.ModelBase.Meta):
|
2010-01-14 03:01:23 +03:00
|
|
|
db_table = 'versioncomments'
|
|
|
|
|
|
|
|
|
2010-02-04 08:13:44 +03:00
|
|
|
class ApplicationsVersions(caching.base.CachingMixin, models.Model):
|
2010-01-23 03:52:41 +03:00
|
|
|
|
|
|
|
application = models.ForeignKey(Application)
|
2010-04-17 03:19:49 +04:00
|
|
|
version = models.ForeignKey(Version, related_name='apps')
|
2010-01-27 01:44:12 +03:00
|
|
|
min = models.ForeignKey(AppVersion, db_column='min',
|
|
|
|
related_name='min_set')
|
|
|
|
max = models.ForeignKey(AppVersion, db_column='max',
|
|
|
|
related_name='max_set')
|
2010-01-23 03:52:41 +03:00
|
|
|
|
2010-02-04 08:13:44 +03:00
|
|
|
objects = caching.base.CachingManager()
|
|
|
|
|
2010-01-23 03:52:41 +03:00
|
|
|
class Meta:
|
|
|
|
db_table = u'applications_versions'
|
2010-02-02 23:22:24 +03:00
|
|
|
unique_together = (("application", "version"),)
|
2010-02-25 20:00:03 +03:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-10-10 19:41:44 +04:00
|
|
|
if (self.version.is_compatible[0] and
|
2012-04-03 22:55:40 +04:00
|
|
|
self.version.is_compatible_app(amo.APP_IDS[self.application.id])):
|
2012-04-16 23:46:43 +04:00
|
|
|
return _(u'{app} {min} and later').format(app=self.application,
|
|
|
|
min=self.min)
|
2010-06-18 22:24:18 +04:00
|
|
|
return u'%s %s - %s' % (self.application, self.min, self.max)
|