making RequestUser so cache-machine byid caching works
RequestUser objects are different from normal UserProfile objects, so the separate class name will cause different cache entries.
This commit is contained in:
Родитель
e46fe8a06d
Коммит
36f69bc2a0
|
@ -5,7 +5,7 @@ their ACLs into the request.
|
|||
from functools import partial
|
||||
|
||||
from access import acl
|
||||
from users.models import UserProfile
|
||||
from users.models import RequestUser
|
||||
|
||||
|
||||
class ACLMiddleware(object):
|
||||
|
@ -16,8 +16,7 @@ class ACLMiddleware(object):
|
|||
|
||||
# figure out our list of groups...
|
||||
if request.user.is_authenticated():
|
||||
amo_user = (UserProfile.objects.request_user()
|
||||
.get(pk=request.user.pk))
|
||||
amo_user = RequestUser.objects.get(pk=request.user.pk)
|
||||
request.user._profile_cache = request.amo_user = amo_user
|
||||
request.groups = request.amo_user.groups.all()
|
||||
|
||||
|
|
|
@ -41,13 +41,6 @@ def create_password(algorithm, raw_password):
|
|||
return '$'.join([algorithm, salt, hsh])
|
||||
|
||||
|
||||
class UserManager(amo.models.ManagerBase):
|
||||
|
||||
def request_user(self):
|
||||
return (self.extra(select={'request': 1})
|
||||
.transform(UserProfile.request_user_transformer))
|
||||
|
||||
|
||||
class UserForeignKey(models.ForeignKey):
|
||||
"""
|
||||
A replacement for models.ForeignKey('users.UserProfile').
|
||||
|
@ -129,8 +122,6 @@ class UserProfile(amo.models.ModelBase):
|
|||
|
||||
user = models.ForeignKey(DjangoUser, null=True, editable=False, blank=True)
|
||||
|
||||
objects = UserManager()
|
||||
|
||||
class Meta:
|
||||
db_table = 'users'
|
||||
|
||||
|
@ -315,11 +306,36 @@ class UserProfile(amo.models.ModelBase):
|
|||
c = Collection.objects.get(id=c.id)
|
||||
return c
|
||||
|
||||
|
||||
class RequestUserManager(amo.models.ManagerBase):
|
||||
|
||||
def get_query_set(self):
|
||||
qs = super(RequestUserManager, self).get_query_set()
|
||||
return qs.transform(RequestUser.transformer)
|
||||
|
||||
|
||||
class RequestUser(UserProfile):
|
||||
"""
|
||||
A RequestUser has extra attributes we don't care about for normal users.
|
||||
"""
|
||||
|
||||
objects = RequestUserManager()
|
||||
|
||||
def __init__(self, *args, **kw):
|
||||
super(RequestUser, self).__init__(*args, **kw)
|
||||
self.mobile_addons = []
|
||||
self.favorite_addons = []
|
||||
self.watching = []
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
@staticmethod
|
||||
def request_user_transformer(users):
|
||||
"""Adds extra goodies to a UserProfile (meant for request.amo_user)."""
|
||||
def transformer(users):
|
||||
# We don't want to cache these things on every UserProfile; they're
|
||||
# only used by a user attached to a request.
|
||||
if not users:
|
||||
return
|
||||
from bandwagon.models import CollectionAddon, CollectionWatcher
|
||||
SPECIAL = amo.COLLECTION_SPECIAL_SLUGS.keys()
|
||||
user = users[0]
|
||||
|
@ -335,6 +351,12 @@ class UserProfile(amo.models.ModelBase):
|
|||
# Touch this @cached_property so the answer is cached with the object.
|
||||
user.is_developer
|
||||
|
||||
def _cache_keys(self):
|
||||
# Add UserProfile.cache_key so RequestUser gets invalidated when the
|
||||
# UserProfile is changed.
|
||||
keys = super(RequestUser, self)._cache_keys()
|
||||
return keys + (UserProfile(id=self.id).cache_key,)
|
||||
|
||||
|
||||
class BlacklistedUsername(amo.models.ModelBase):
|
||||
"""Blacklisted user usernames."""
|
||||
|
|
|
@ -19,7 +19,7 @@ mongoengine==0.3
|
|||
django-uuidfield==0.1
|
||||
|
||||
-e git://github.com/jbalogh/django-multidb-router.git#egg=django-multidb-router
|
||||
-e git://github.com/jbalogh/django-cache-machine.git@85b0f3ed4#egg=django-cache-machine
|
||||
-e git://github.com/jbalogh/django-cache-machine.git@0ca435683#egg=django-cache-machine
|
||||
-e git://github.com/jbalogh/jingo.git#egg=jingo
|
||||
-e git://github.com/jsocol/jingo-minify.git#egg=jingo-minify
|
||||
-e git://github.com/jsocol/bleach.git#egg=bleach
|
||||
|
|
Загрузка…
Ссылка в новой задаче