UUID ALL THE APPS! (bug 814131)

This commit is contained in:
Rob Hudson 2012-12-04 15:23:15 -08:00
Родитель 6cd145606d
Коммит def797bb14
4 изменённых файлов: 49 добавлений и 2 удалений

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

@ -0,0 +1,11 @@
#!/usr/bin/env python
import amo
from mkt.webapps.models import Webapp
def run():
"""Add uuid to apps that don't have one."""
for app in (Webapp.uncached.filter(guid=None)
.exclude(status=amo.STATUS_DELETED)
.no_transforms()):
app.save()

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

@ -297,7 +297,8 @@ class TestCreateWebApp(BaseWebAppTest):
addon = self.post_addon()
eq_(addon.type, amo.ADDON_WEBAPP)
eq_(addon.is_packaged, False)
eq_(addon.guid, None)
assert addon.guid is not None, (
'Expected app to have a UUID assigned to guid')
eq_(unicode(addon.name), u'MozillaBall ょ')
eq_(addon.slug, 'app-%s' % addon.id)
eq_(addon.app_slug, u'mozillaball-ょ')
@ -443,7 +444,8 @@ class TestCreatePackagedApp(BasePackagedAppTest):
eq_(addon.type, amo.ADDON_WEBAPP)
eq_(addon.current_version.version, '1.0')
eq_(addon.is_packaged, True)
eq_(addon.guid, None)
assert addon.guid is not None, (
'Expected app to have a UUID assigned to guid')
eq_(unicode(addon.name), u'Packaged MozillaBall ょ')
eq_(addon.slug, 'app-%s' % addon.id)
eq_(addon.app_slug, u'packaged-mozillaball-ょ')

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

@ -110,6 +110,7 @@ class Webapp(Addon):
# Make sure we have the right type.
self.type = amo.ADDON_WEBAPP
self.clean_slug(slug_field='app_slug')
self.assign_uuid()
creating = not self.id
super(Addon, self).save(**kw)
if creating:
@ -668,6 +669,22 @@ class Webapp(Addon):
return
return packaged.sign(version_pk, reviewer=reviewer)
def assign_uuid(self):
"""Generates a UUID if self.guid is not already set."""
if not self.guid:
max_tries = 10
tried = 1
guid = str(uuid.uuid4())
while tried <= max_tries:
if not Webapp.objects.filter(guid=guid).exists():
self.guid = guid
break
else:
guid = str(uuid.uuid4())
tried += 1
else:
raise Exception('Could not auto-generate a unique UUID')
# Pull all translated_fields from Addon over to Webapp.
Webapp._meta.translated_fields = Addon._meta.translated_fields

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

@ -1,6 +1,7 @@
from datetime import datetime, timedelta
import json
import unittest
import uuid
import zipfile
from django.conf import settings
@ -267,6 +268,22 @@ class TestWebapp(amo.tests.TestCase):
webapp = Webapp.objects.create(manifest_url='http://foo.com')
eq_(webapp.is_packaged, False)
def test_assign_uuid(self):
app = Webapp()
eq_(app.guid, None)
app.save()
assert app.guid is not None, (
'Expected app to have a UUID assigned to guid')
@mock.patch.object(uuid, 'uuid4', 'abcdef')
def test_assign_uuid_max_tries(self):
# Create another webapp with and set the guid.
Webapp.objects.create(guid='abcdef')
# Now `assign_uuid()` should fail.
app = Webapp()
with self.assertRaises(Exception):
app.assign_uuid()
class TestWebappVersion(amo.tests.TestCase):
fixtures = ['base/platforms']