Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit 8412830

Browse files
author
Rob Hudson
committed
File.status cleanup (bug 778779)
1 parent beac7ff commit 8412830

File tree

11 files changed

+142
-123
lines changed

11 files changed

+142
-123
lines changed

apps/constants/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@
111111
WEBAPPS_UNLISTED_STATUSES = (STATUS_DISABLED, STATUS_PENDING,
112112
STATUS_PUBLIC_WAITING, STATUS_REJECTED)
113113

114+
# The only statuses we use in the marketplace.
115+
MARKET_STATUSES = (STATUS_NULL, STATUS_PENDING, STATUS_PUBLIC, STATUS_DISABLED,
116+
STATUS_DELETED, STATUS_REJECTED, STATUS_PUBLIC_WAITING)
117+
114118
# Types of administrative review queues for an add-on:
115119
ADMIN_REVIEW_FULL = 1
116120
ADMIN_REVIEW_PRELIM = 2

apps/files/models.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,19 @@ def get_url_path(self, src, addon=None):
150150

151151
@classmethod
152152
def from_upload(cls, upload, version, platform, parse_data={}):
153+
is_webapp = version.addon.is_webapp()
153154
f = cls(version=version, platform=platform)
154155
upload.path = amo.utils.smart_path(nfd_str(upload.path))
155-
ext = os.path.splitext(upload.path)[1]
156-
f.filename = f.generate_filename(extension=ext or '.xpi')
156+
f.filename = f.generate_filename(os.path.splitext(upload.path)[1])
157157
# Size in kilobytes.
158-
f.size = int(max(1, round(storage.size(upload.path) / 1024, 0)))
158+
f.size = int(max(1, round(storage.size(upload.path) / 1024)))
159159
data = cls.get_jetpack_metadata(upload.path)
160160
f.jetpack_version = data['sdkVersion']
161161
f.builder_version = data['builderVersion']
162162
f.no_restart = parse_data.get('no_restart', False)
163163
f.strict_compatibility = parse_data.get('strict_compatibility', False)
164-
if version.addon.is_webapp():
165-
# Files don't really matter for webapps, just make them public.
166-
f.status = amo.STATUS_PUBLIC
164+
if is_webapp:
165+
f.status = amo.STATUS_PENDING
167166
elif version.addon.status == amo.STATUS_PUBLIC:
168167
if amo.VERSION_BETA.search(parse_data.get('version', '')):
169168
f.status = amo.STATUS_BETA
@@ -220,25 +219,32 @@ def generate_hash(self, filename=None):
220219
hash.update(chunk)
221220
return 'sha256:%s' % hash.hexdigest()
222221

223-
def generate_filename(self, extension='.xpi'):
222+
def generate_filename(self, extension=None):
224223
"""
225224
Files are in the format of:
226225
{addon_name}-{version}-{apps}-{platform}
227226
"""
228227
parts = []
228+
addon = self.version.addon
229229
# slugify drops unicode so we may end up with an empty string.
230230
# Apache did not like serving unicode filenames (bug 626587).
231-
name = slugify(self.version.addon.name).replace('-', '_') or 'addon'
232-
parts.append(name)
233-
parts.append(self.version.version)
234-
235-
if self.version.compatible_apps:
236-
apps = '+'.join([a.shortername for a in
237-
self.version.compatible_apps])
238-
parts.append(apps)
239-
240-
if self.platform_id and self.platform_id != amo.PLATFORM_ALL.id:
241-
parts.append(amo.PLATFORMS[self.platform_id].shortname)
231+
if addon.is_webapp():
232+
extension = extension or '.webapp'
233+
parts.append(addon.app_slug)
234+
parts.append(self.version.version)
235+
else:
236+
extension = extension or '.xpi'
237+
name = slugify(addon.name).replace('-', '_') or 'addon'
238+
parts.append(name)
239+
parts.append(self.version.version)
240+
241+
if self.version.compatible_apps:
242+
apps = '+'.join([a.shortername for a in
243+
self.version.compatible_apps])
244+
parts.append(apps)
245+
246+
if self.platform_id and self.platform_id != amo.PLATFORM_ALL.id:
247+
parts.append(amo.PLATFORMS[self.platform_id].shortname)
242248

243249
self.filename = '-'.join(parts) + extension
244250
return self.filename

apps/files/tests/test_models.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ def test_generate_filename(self):
225225
f = File.objects.get(id=67442)
226226
eq_(f.generate_filename(), 'delicious_bookmarks-2.1.072-fx.xpi')
227227

228+
def test_generate_filename_webapp(self):
229+
f = File.objects.get(id=67442)
230+
f.version.addon.app_slug = 'testing-123'
231+
f.version.addon.type = amo.ADDON_WEBAPP
232+
eq_(f.generate_filename(), 'testing-123-2.1.072.webapp')
233+
228234
def test_pretty_filename(self):
229235
f = File.objects.get(id=67442)
230236
f.generate_filename()
@@ -260,7 +266,6 @@ def clean_files(self, f):
260266
with storage.open(f.file_path, 'w') as fp:
261267
fp.write('sample data\n')
262268

263-
264269
def test_copy_to_mirror(self):
265270
f = File.objects.get(id=67442)
266271
self.clean_files(f)
@@ -574,7 +579,8 @@ def test_validator_sets_binary_via_extensions(self):
574579
"id": "gkobes@gkobes",
575580
}
576581
})
577-
upload = self.get_upload(filename='extension.xpi', validation=validation)
582+
upload = self.get_upload(filename='extension.xpi',
583+
validation=validation)
578584
version = Version.objects.filter(addon__pk=3615)[0]
579585
plat = Platform.objects.get(pk=amo.PLATFORM_LINUX.id)
580586
file_ = File.from_upload(upload, version, plat)
@@ -595,7 +601,8 @@ def test_validator_sets_binary_via_content(self):
595601
"id": "gkobes@gkobes",
596602
}
597603
})
598-
upload = self.get_upload(filename='extension.xpi', validation=validation)
604+
upload = self.get_upload(filename='extension.xpi',
605+
validation=validation)
599606
version = Version.objects.filter(addon__pk=3615)[0]
600607
plat = Platform.objects.get(pk=amo.PLATFORM_LINUX.id)
601608
file_ = File.from_upload(upload, version, plat)
@@ -616,7 +623,8 @@ def test_validator_sets_require_chrome(self):
616623
"requires_chrome": True
617624
}
618625
})
619-
upload = self.get_upload(filename='extension.xpi', validation=validation)
626+
upload = self.get_upload(filename='extension.xpi',
627+
validation=validation)
620628
version = Version.objects.filter(addon__pk=3615)[0]
621629
plat = Platform.objects.get(pk=amo.PLATFORM_LINUX.id)
622630
file_ = File.from_upload(upload, version, plat)

apps/files/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import collections
2-
import cPickle as pickle
32
import glob
43
import hashlib
54
import json
@@ -57,6 +56,7 @@ def get_filepath(fileorpath):
5756
return fileorpath.path
5857
return fileorpath
5958

59+
6060
def get_file(fileorpath):
6161
"""Get a file-like object, whether given a FileUpload object or a path."""
6262
if hasattr(fileorpath, 'path'): # FileUpload
@@ -438,17 +438,15 @@ def parse_addon(pkg, addon=None):
438438
or files.models.FileUpload.
439439
"""
440440
name = getattr(pkg, 'name', pkg)
441-
if (getattr(pkg, 'is_webapp', False) or
442-
name.endswith('.webapp') or name.endswith('.json')):
441+
if getattr(pkg, 'is_webapp', False) or name.endswith(('.webapp', '.json')):
443442
parsed = WebAppParser().parse(pkg, addon)
444443
elif name.endswith('.xml'):
445444
parsed = parse_search(pkg, addon)
446445
else:
447446
parsed = parse_xpi(pkg, addon)
448447

449448
if addon and addon.type != parsed['type']:
450-
raise forms.ValidationError(
451-
_("<em:type> doesn't match add-on"))
449+
raise forms.ValidationError(_("<em:type> doesn't match add-on"))
452450
return parsed
453451

454452

apps/versions/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def from_upload(cls, upload, addon, platforms, send_signal=True):
8383
AV(version=v, min=app.min, max=app.max,
8484
application_id=app.id).save()
8585
if addon.type in [amo.ADDON_SEARCH, amo.ADDON_WEBAPP]:
86-
# Search extensions are always for all platforms.
86+
# Search extensions and webapps are always for all platforms.
8787
platforms = [Platform.objects.get(id=amo.PLATFORM_ALL.id)]
8888
else:
8989
platforms = cls._make_safe_platform_files(platforms)
@@ -92,8 +92,7 @@ def from_upload(cls, upload, addon, platforms, send_signal=True):
9292
File.from_upload(upload, v, platform, parse_data=data)
9393

9494
v.disable_old_files()
95-
# After the upload has been copied to all
96-
# platforms, remove the upload.
95+
# After the upload has been copied to all platforms, remove the upload.
9796
storage.delete(upload.path)
9897
if send_signal:
9998
version_uploaded.send(sender=v)
@@ -385,8 +384,9 @@ def rollup(xs):
385384
def disable_old_files(self):
386385
if not self.files.filter(status=amo.STATUS_BETA).exists():
387386
qs = File.objects.filter(version__addon=self.addon_id,
388-
version__lt=self,
389-
status=amo.STATUS_UNREVIEWED)
387+
version__lt=self.id,
388+
status__in=[amo.STATUS_UNREVIEWED,
389+
amo.STATUS_PENDING])
390390
# Use File.update so signals are triggered.
391391
for f in qs:
392392
f.update(status=amo.STATUS_DISABLED)

mkt/developers/tests/test_tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import mkt
2727
from mkt.developers import tasks
28-
from mkt.developers.tests.test_views import BaseWebAppTest
28+
from mkt.submit.tests.test_views import BaseWebAppTest
2929
from mkt.webapps.models import AddonExcludedRegion as AER
3030

3131

mkt/developers/tests/test_views.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from devhub.models import UserLog
3030
from files.models import FileUpload
3131
from files.tests.test_models import UploadTest as BaseUploadTest
32-
from lib.pay_server import client
3332
from market.models import AddonPremium, Price, Refund
3433
from mkt.developers import tasks
3534
from mkt.developers.models import ActivityLog
@@ -1457,69 +1456,6 @@ def assert_json_field(request, field, msg):
14571456
eq_(content[field], msg)
14581457

14591458

1460-
class BaseWebAppTest(BaseUploadTest, amo.tests.TestCase):
1461-
fixtures = ['base/apps', 'base/users', 'base/platforms']
1462-
1463-
def setUp(self):
1464-
super(BaseWebAppTest, self).setUp()
1465-
self.manifest = os.path.join(settings.ROOT, 'apps', 'devhub', 'tests',
1466-
'addons', 'mozball.webapp')
1467-
self.upload = self.get_upload(abspath=self.manifest)
1468-
self.url = reverse('submit.app.manifest')
1469-
assert self.client.login(username='regular@mozilla.com',
1470-
password='password')
1471-
self.client.post(reverse('submit.app.terms'),
1472-
{'read_dev_agreement': True})
1473-
1474-
def post(self, desktop_platforms=[amo.PLATFORM_ALL], mobile_platforms=[],
1475-
expect_errors=False):
1476-
d = dict(upload=self.upload.pk,
1477-
desktop_platforms=[p.id for p in desktop_platforms],
1478-
mobile_platforms=[p.id for p in mobile_platforms])
1479-
r = self.client.post(self.url, d, follow=True)
1480-
eq_(r.status_code, 200)
1481-
if not expect_errors:
1482-
# Show any unexpected form errors.
1483-
if r.context and 'new_addon_form' in r.context:
1484-
eq_(r.context['new_addon_form'].errors.as_text(), '')
1485-
return r
1486-
1487-
def post_addon(self):
1488-
eq_(Addon.objects.count(), 0)
1489-
self.post()
1490-
return Addon.objects.get()
1491-
1492-
1493-
class TestCreateWebApp(BaseWebAppTest):
1494-
1495-
def test_post_app_redirect(self):
1496-
r = self.post()
1497-
addon = Addon.objects.get()
1498-
self.assertRedirects(r, reverse('submit.app.details',
1499-
args=[addon.app_slug]))
1500-
1501-
def test_addon_from_uploaded_manifest(self):
1502-
addon = self.post_addon()
1503-
eq_(addon.type, amo.ADDON_WEBAPP)
1504-
eq_(addon.guid, None)
1505-
eq_(unicode(addon.name), 'MozillaBall')
1506-
eq_(addon.slug, 'app-%s' % addon.id)
1507-
eq_(addon.app_slug, 'mozillaball')
1508-
eq_(addon.summary, u'Exciting Open Web development action!')
1509-
eq_(Translation.objects.get(id=addon.summary.id, locale='it'),
1510-
u'Azione aperta emozionante di sviluppo di fotoricettore!')
1511-
1512-
def test_version_from_uploaded_manifest(self):
1513-
addon = self.post_addon()
1514-
eq_(addon.current_version.version, '1.0')
1515-
1516-
def test_file_from_uploaded_manifest(self):
1517-
addon = self.post_addon()
1518-
files = addon.current_version.files
1519-
eq_(files.count(), 1)
1520-
eq_(files.all()[0].status, amo.STATUS_PUBLIC)
1521-
1522-
15231459
class TestDeleteApp(amo.tests.TestCase):
15241460
fixtures = ['base/apps', 'base/users', 'webapps/337141-steamcube']
15251461

0 commit comments

Comments
 (0)