add recommended_collection and addon_index fields
This commit is contained in:
Родитель
983a2f45e0
Коммит
077d866e9c
|
@ -1,3 +1,4 @@
|
|||
import hashlib
|
||||
import time
|
||||
import uuid
|
||||
|
||||
|
@ -56,6 +57,10 @@ class Collection(amo.models.ModelBase):
|
|||
users = models.ManyToManyField(UserProfile, through='CollectionUser',
|
||||
related_name='collections')
|
||||
|
||||
addon_index = models.CharField(max_length=40, null=True, db_index=True,
|
||||
help_text='Custom index for the add-ons in this collection')
|
||||
recommended_collection = models.ForeignKey('self', null=True)
|
||||
|
||||
objects = CollectionManager()
|
||||
|
||||
class Meta(amo.models.ModelBase.Meta):
|
||||
|
@ -68,19 +73,26 @@ class Collection(amo.models.ModelBase):
|
|||
if not self.uuid:
|
||||
self.uuid = unicode(uuid.uuid4())
|
||||
|
||||
# Maintain our index of add-on ids.
|
||||
if self.id:
|
||||
ids = self.addons.values_list('id', flat=True)
|
||||
self.addon_index = self.make_index(ids)
|
||||
|
||||
super(Collection, self).save(**kw)
|
||||
|
||||
def get_url_path(self):
|
||||
# TODO(jbalogh): reverse
|
||||
return '/collection/%s' % self.url_slug
|
||||
|
||||
def fetch_translations(self, ids, lang):
|
||||
return translations_with_fallback(ids, lang, self.default_locale)
|
||||
|
||||
@classmethod
|
||||
def get_fallback(cls):
|
||||
return cls._meta.get_field('default_locale')
|
||||
|
||||
@classmethod
|
||||
def make_index(cls, addon_ids):
|
||||
ids = ':'.join(map(str, sorted(addon_ids)))
|
||||
return hashlib.md5(ids).hexdigest()
|
||||
|
||||
@property
|
||||
def url_slug(self):
|
||||
"""uuid or nickname if chosen"""
|
||||
|
|
|
@ -5,7 +5,7 @@ from bandwagon.models import Collection
|
|||
|
||||
|
||||
class TestCollections(test_utils.TestCase):
|
||||
fixtures = ['bandwagon/test_models']
|
||||
fixtures = ['base/addons', 'bandwagon/test_models', 'base/collections']
|
||||
|
||||
def test_translation_default(self):
|
||||
"""Make sure we're getting strings from the default locale."""
|
||||
|
@ -14,20 +14,24 @@ class TestCollections(test_utils.TestCase):
|
|||
|
||||
def test_listed(self):
|
||||
"""Make sure the manager's listed() filter works."""
|
||||
listed_count = Collection.objects.listed().count()
|
||||
# make a private collection
|
||||
private = Collection(
|
||||
name="Hello", uuid="4e2a1acc-39ae-47ec-956f-46e080ac7f69",
|
||||
listed=False)
|
||||
private.save()
|
||||
|
||||
c = Collection.objects.get(pk=512)
|
||||
|
||||
listed = Collection.objects.listed()
|
||||
|
||||
eq_(listed.count(), 1)
|
||||
eq_(listed[0], c)
|
||||
eq_(len(listed), listed_count)
|
||||
|
||||
def test_auto_uuid(self):
|
||||
c = Collection.objects.create()
|
||||
assert c.uuid != ''
|
||||
assert isinstance(c.uuid, basestring)
|
||||
|
||||
def test_addon_index(self):
|
||||
c = Collection.objects.get(pk=5)
|
||||
eq_(c.addon_index, None)
|
||||
ids = c.addons.values_list('id', flat=True)
|
||||
c.save()
|
||||
eq_(c.addon_index, Collection.make_index(ids))
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
ALTER TABLE collections
|
||||
ADD COLUMN `addon_index` varchar(40) NULL,
|
||||
ADD COLUMN `recommended_collection_id` int(11) unsigned NULL,
|
||||
ADD FOREIGN KEY (`recommended_collection_id`) REFERENCES `collections` (`id`);
|
||||
|
||||
CREATE INDEX `collections_addon_index` ON `collections` (`addon_index`);
|
Загрузка…
Ссылка в новой задаче