attach mobile_addons to request.amo_user

This commit is contained in:
Jeff Balogh 2010-08-10 16:33:05 -07:00
Родитель 5509886935
Коммит dffa7c1dc9
3 изменённых файлов: 34 добавлений и 3 удалений

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

@ -4,6 +4,7 @@ their ACLs into the request.
""" """
from access import acl from access import acl
from users.models import UserProfile
class ACLMiddleware(object): class ACLMiddleware(object):
@ -15,7 +16,9 @@ class ACLMiddleware(object):
# figure out our list of groups... # figure out our list of groups...
if request.user.is_authenticated(): if request.user.is_authenticated():
request.amo_user = request.user.get_profile() amo_user = (UserProfile.objects.request_user()
.get(pk=request.user.pk))
request.user._profile_cache = request.amo_user = amo_user
request.groups = request.amo_user.groups.all() request.groups = request.amo_user.groups.all()
if acl.action_allowed(request, 'Admin', '%'): if acl.action_allowed(request, 'Admin', '%'):

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

@ -38,6 +38,12 @@ def create_password(algorithm, raw_password):
return '$'.join([algorithm, salt, hsh]) return '$'.join([algorithm, salt, hsh])
class UserManager(amo.models.ManagerBase):
def request_user(self):
return self.transform(UserProfile.request_user_transformer)
class UserProfile(amo.models.ModelBase): class UserProfile(amo.models.ModelBase):
nickname = models.CharField(max_length=255, unique=True, default='', nickname = models.CharField(max_length=255, unique=True, default='',
@ -70,6 +76,8 @@ class UserProfile(amo.models.ModelBase):
user = models.ForeignKey(DjangoUser, null=True, editable=False, blank=True) user = models.ForeignKey(DjangoUser, null=True, editable=False, blank=True)
objects = UserManager()
class Meta: class Meta:
db_table = 'users' db_table = 'users'
@ -210,6 +218,18 @@ class UserProfile(amo.models.ModelBase):
author=self, type=type_, defaults=defaults) author=self, type=type_, defaults=defaults)
return c return c
@staticmethod
def request_user_transformer(users):
"""Adds extra goodies to a UserProfile (meant for request.amo_user)."""
# We don't want to cache these things on every UserProfile; they're
# only used by a user attached to a request.
from bandwagon.models import CollectionAddon
user = users[0]
qs = CollectionAddon.objects.filter(
collection__author=user, collection__type=amo.COLLECTION_MOBILE)
user.mobile_addons = qs.values_list('addon', flat=True)
class BlacklistedNickname(amo.models.ModelBase): class BlacklistedNickname(amo.models.ModelBase):
"""Blacklisted user nicknames.""" """Blacklisted user nicknames."""

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

@ -1,12 +1,12 @@
import json import json
from django import test
from django.core import mail from django.core import mail
from django.core.cache import cache from django.core.cache import cache
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test.client import Client from django.test.client import Client
from nose.tools import eq_ from nose.tools import eq_
import test_utils
from access.models import Group, GroupUser from access.models import Group, GroupUser
from amo.helpers import urlparams from amo.helpers import urlparams
@ -15,7 +15,7 @@ from amo.urlresolvers import reverse
from users.utils import EmailResetCode from users.utils import EmailResetCode
class UserViewBase(test.TestCase): class UserViewBase(test_utils.TestCase):
fixtures = ['users/test_backends'] fixtures = ['users/test_backends']
@ -210,3 +210,11 @@ class TestProfile(UserViewBase):
eq_(links.eq(0).attr('href'), reverse('users.edit')) eq_(links.eq(0).attr('href'), reverse('users.edit'))
eq_(links.eq(1).attr('href'), eq_(links.eq(1).attr('href'),
reverse('admin:users_userprofile_change', args=[self.user.id])) reverse('admin:users_userprofile_change', args=[self.user.id]))
def test_amouser(self):
# request.amo_user should be a special guy.
self.client.login(username='jbalogh@mozilla.com', password='foo')
response = self.client.get(reverse('home'))
request = response.context['request']
assert hasattr(request.amo_user, 'mobile_addons')
assert hasattr(request.user.get_profile(), 'mobile_addons')