1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 15:09:14 +00:00

Update nickname cache invalidation

This commit is contained in:
zhengxie
2016-03-18 15:49:31 +08:00
parent 865429e5ec
commit 3d5d98ffb8
10 changed files with 95 additions and 11 deletions

View File

@@ -75,7 +75,6 @@ class Account(APIView):
profile.intro = note
profile.save()
refresh_profile_cache(email)
def _update_account_quota(self, request, email):
storage = request.DATA.get("storage", None)

View File

@@ -129,7 +129,11 @@ class DetailedProfile(models.Model):
telephone = models.CharField(max_length=100)
objects = DetailedProfileManager()
########## signal handler
########## signal handlers
from django.db.models.signals import post_save
from .utils import refresh_cache
@receiver(user_registered)
def clean_email_id_cache(sender, **kwargs):
from seahub.utils import normalize_cache_key
@@ -137,3 +141,7 @@ def clean_email_id_cache(sender, **kwargs):
user = kwargs['user']
key = normalize_cache_key(user.email, EMAIL_ID_CACHE_PREFIX)
cache.set(key, user.id, EMAIL_ID_CACHE_TIMEOUT)
@receiver(post_save, sender=Profile, dispatch_uid="update_nickname_cache")
def update_nickname_cache(sender, instance, **kwargs):
refresh_cache(instance.user)

View File

@@ -36,8 +36,6 @@ def edit_profile(request):
if form.is_valid():
form.save(username=username)
messages.success(request, _(u'Successfully edited profile.'))
# refresh nickname cache
refresh_cache(request.user.username)
return HttpResponseRedirect(reverse('edit_profile'))
else:

View File

@@ -1,11 +1,11 @@
from .settings import *
# no cache for testing
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
# CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
# }
# }
# enlarge api throttle
REST_FRAMEWORK = {

View File

@@ -1,6 +1,7 @@
import os
from uuid import uuid4
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.test import TestCase
from exam.decorators import fixture
@@ -108,3 +109,8 @@ class BaseTestCase(TestCase, Fixtures):
reverse('auth_login'), {'login': user.username,
'password': 'secret'}
)
def clear_cache(self):
# clear cache between every test case to avoid config option cache
# issue which cause test failed
cache.clear()

View File

@@ -18,6 +18,8 @@ class RepoPublicTest(BaseTestCase):
def tearDown(self):
self.remove_repo(self.repo_id)
# clear cache between every test case to avoid config option cache issue
self.clear_cache()
def test_admin_can_set_pub_repo(self):
self.login_as(self.admin)

View File

@@ -16,6 +16,7 @@ class SharedRepoTest(BaseTestCase):
def tearDown(self):
self.remove_repo(self.repo_id)
self.clear_cache()
def test_admin_can_share_repo_to_public(self):
self.login_as(self.admin)

View File

@@ -0,0 +1,35 @@
from seahub.base.templatetags.seahub_tags import email2nickname
from seahub.profile.models import Profile
from seahub.test_utils import BaseTestCase
from tests.common.utils import randstring
class UpdateNicknameCacheTest(BaseTestCase):
def setUp(self):
self.tmp_user = self.create_user('user_%s@test.com' % randstring(4),
is_staff=False)
assert len(Profile.objects.all()) == 0
def tearDown(self):
self.remove_user(self.tmp_user.username)
def test_update_when_call_object_method(self):
username = self.tmp_user.username
assert email2nickname(username) == username.split('@')[0]
Profile.objects.add_or_update(username, 'nickname')
assert email2nickname(username) == 'nickname'
def test_updated_when_call_save(self):
username = self.tmp_user.username
assert email2nickname(username) == username.split('@')[0]
p = Profile.objects.get_profile_by_user(username)
if p is None:
p = Profile(user=username)
p.nickname = 'nickname'
p.save()
assert email2nickname(username) == 'nickname'

View File

@@ -0,0 +1,33 @@
from django.core.urlresolvers import reverse
from seahub.base.templatetags.seahub_tags import email2nickname
from seahub.profile.models import Profile
from seahub.test_utils import BaseTestCase
from tests.common.utils import randstring
class EditProfileTest(BaseTestCase):
def setUp(self):
self.tmp_user = self.create_user('user_%s@test.com' % randstring(4),
is_staff=False)
assert len(Profile.objects.all()) == 0
self.url = reverse('edit_profile')
self.login_as(self.tmp_user)
def tearDown(self):
self.remove_user(self.tmp_user.username)
def test_can_render_edit_page(self):
resp = self.client.get(self.url)
self.assertEqual(200, resp.status_code)
self.assertTemplateUsed(resp, 'profile/set_profile.html')
def test_can_edit(self):
assert email2nickname(self.tmp_user.username) == self.tmp_user.username.split('@')[0]
resp = self.client.post(self.url, {
'nickname': 'new nickname'
})
self.assertEqual(302, resp.status_code)
self.assertRegexpMatches(resp['Location'], r'http://testserver/profile/')
assert email2nickname(self.tmp_user.username) == 'new nickname'

View File

@@ -27,6 +27,8 @@ class LibrariesTest(BaseTestCase):
assert UserOptions.objects.is_user_guide_enabled(username) is False
def test_pub_repo_creation_config(self):
self.clear_cache()
# user
self.login_as(self.user)