Commit 1c98e84f authored by Arthur Del Esposte's avatar Arthur Del Esposte

Add User#packages property based on user's subscriptions

parent 7261a04b
......@@ -27,6 +27,21 @@ class User(EmailAccountsUser):
class Meta:
proxy = True
@property
def packages(self):
"""
This method is a is a convenient way to get all packages that a user
is subscribed to. It is required to enable displaying packages panels
in the user personal page.
"""
from distro_tracker.core.models import PackageName, Subscription
subscriptions = Subscription.objects.get_for_email(self.main_email)
if subscriptions:
packages_ids = subscriptions.values_list('package_id', flat=True)
return PackageName.objects.filter(id__in=packages_ids)
else:
return PackageName.objects.none()
def is_subscribed_to(self, package):
"""
Checks if the user is subscribed to the given package. The user is
......
......@@ -100,6 +100,23 @@ class UserTests(TestCase):
main_email=self.main_email, password='asdf')
self.package = PackageName.objects.create(name='dummy-package')
def test_packages_property_based_on_subscriptions(self):
"""
Tests the
:meth:`packages
<distro_tracker.accounts.models.User.packages>`
method when the user is subscribed to the package with their
main email.
"""
self.assertEqual(len(self.user.packages), 0)
email = self.user.emails.all()[0]
Subscription.objects.create_for(
email=email.email,
package_name=self.package.name)
self.assertEqual(len(self.user.packages), 1)
self.assertIn(self.package, self.user.packages)
def test_is_subscribed_to_main_email(self):
"""
Tests the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment