From f65c1829f5226062d9ec7a9c07a8e0720854af13 Mon Sep 17 00:00:00 2001 From: Jeff Balogh Date: Mon, 9 Aug 2010 18:37:34 -0700 Subject: [PATCH] helpers for getting mobile/favorites collection --- apps/amo/__init__.py | 4 ++-- apps/bandwagon/models.py | 3 ++- apps/users/models.py | 16 ++++++++++++++++ apps/users/tests/test_models.py | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/amo/__init__.py b/apps/amo/__init__.py index 5e8f3d6022..448ef06a8b 100644 --- a/apps/amo/__init__.py +++ b/apps/amo/__init__.py @@ -421,7 +421,7 @@ COLLECTION_NORMAL = 0 COLLECTION_SYNCHRONIZED = 1 COLLECTION_FEATURED = 2 COLLECTION_RECOMMENDED = 3 -COLLECTION_FAVORITE = 4 +COLLECTION_FAVORITES = 4 COLLECTION_MOBILE = 5 COLLECTION_ANONYMOUS = 6 @@ -430,7 +430,7 @@ COLLECTION_CHOICES = { COLLECTION_SYNCHRONIZED: 'Synchronized', COLLECTION_FEATURED: 'Featured', COLLECTION_RECOMMENDED: 'Generated Recommendations', - COLLECTION_FAVORITE: 'Favorites', + COLLECTION_FAVORITES: 'Favorites', COLLECTION_MOBILE: 'Mobile', COLLECTION_ANONYMOUS: 'Anonymous', } diff --git a/apps/bandwagon/models.py b/apps/bandwagon/models.py index 05ae6a5977..2ee50fde86 100644 --- a/apps/bandwagon/models.py +++ b/apps/bandwagon/models.py @@ -46,7 +46,7 @@ class CollectionManager(amo.models.ManagerBase): def manual(self): """Only hand-crafted, favorites, and featured collections should appear in this filter.""" - types = (amo.COLLECTION_NORMAL, amo.COLLECTION_FAVORITE, + types = (amo.COLLECTION_NORMAL, amo.COLLECTION_FAVORITES, amo.COLLECTION_FEATURED, ) return self.filter(type__in=types) @@ -63,6 +63,7 @@ class Collection(amo.models.ModelBase): uuid = models.CharField(max_length=36, blank=True, unique=True) name = TranslatedField() + # nickname is deprecated. Use slug. nickname = models.CharField(max_length=30, blank=True, unique=True, null=True) slug = models.CharField(max_length=30, blank=True, null=True) diff --git a/apps/users/models.py b/apps/users/models.py index c17bb59c19..ff85a3229f 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -194,6 +194,22 @@ class UserProfile(amo.models.ModelBase): self.save() return self.user + def mobile_collection(self): + return self.special_collection(amo.COLLECTION_MOBILE, + defaults={'slug': 'mobile', 'listed': False, + 'name': _('My Mobile Add-ons')}) + + def favorites_collection(self): + return self.special_collection(amo.COLLECTION_FAVORITES, + defaults={'slug': 'favorites', 'listed': False, + 'name': _('My Favorite Add-ons')}) + + def special_collection(self, type_, defaults): + from bandwagon.models import Collection + c, _ = Collection.objects.get_or_create( + author=self, type=type_, defaults=defaults) + return c + class BlacklistedNickname(amo.models.ModelBase): """Blacklisted user nicknames.""" diff --git a/apps/users/tests/test_models.py b/apps/users/tests/test_models.py index c8f83fc959..75c6d46474 100644 --- a/apps/users/tests/test_models.py +++ b/apps/users/tests/test_models.py @@ -9,6 +9,7 @@ from nose.tools import eq_ import amo.test_utils from addons.models import Addon, AddonUser +from bandwagon.models import Collection from reviews.models import Review from users.models import UserProfile, get_hexdigest, BlacklistedNickname @@ -117,6 +118,22 @@ class TestUserProfile(amo.test_utils.ExtraSetup, test.TestCase): addons = u.addons_listed.values_list('id', flat=True) eq_(sorted(addons), [3615, 4664]) + def test_mobile_collection(self): + u = UserProfile.objects.get(id='4043307') + assert not Collection.objects.filter(author=u) + + c = u.mobile_collection() + eq_(c.type, amo.COLLECTION_MOBILE) + eq_(c.slug, 'mobile') + + def test_favorites_collection(self): + u = UserProfile.objects.get(id='4043307') + assert not Collection.objects.filter(author=u) + + c = u.favorites_collection() + eq_(c.type, amo.COLLECTION_FAVORITES) + eq_(c.slug, 'favorites') + class TestPasswords(test.TestCase):