From 077d866e9cd215c22dd57f60d39a7bd34fdc634a Mon Sep 17 00:00:00 2001 From: Jeff Balogh Date: Thu, 22 Apr 2010 19:37:51 -0700 Subject: [PATCH] add recommended_collection and addon_index fields --- apps/bandwagon/models.py | 18 +++++++++++++++--- apps/bandwagon/tests/test_models.py | 16 ++++++++++------ migrations/29-collection-recs.sql | 6 ++++++ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 migrations/29-collection-recs.sql diff --git a/apps/bandwagon/models.py b/apps/bandwagon/models.py index 6fa7e92820..d3e02b5f89 100644 --- a/apps/bandwagon/models.py +++ b/apps/bandwagon/models.py @@ -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""" diff --git a/apps/bandwagon/tests/test_models.py b/apps/bandwagon/tests/test_models.py index 273c974e59..051b67ed71 100644 --- a/apps/bandwagon/tests/test_models.py +++ b/apps/bandwagon/tests/test_models.py @@ -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)) diff --git a/migrations/29-collection-recs.sql b/migrations/29-collection-recs.sql new file mode 100644 index 0000000000..87bf82c617 --- /dev/null +++ b/migrations/29-collection-recs.sql @@ -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`);