1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-02 07:47:32 +00:00

Merge pull request #1636 from haiwen/file-modifier

[api] file modifier
This commit is contained in:
xiez 2017-06-19 15:38:39 +08:00 committed by GitHub
commit b487ca463b
6 changed files with 70 additions and 6 deletions

View File

@ -47,7 +47,8 @@ from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url, \
from seahub.base.accounts import User
from seahub.base.models import UserStarredFiles, DeviceToken
from seahub.base.templatetags.seahub_tags import email2nickname, \
translate_seahub_time, translate_commit_desc_escape
translate_seahub_time, translate_commit_desc_escape, \
email2contact_email
from seahub.group.views import remove_group_common, \
rename_group_with_new_name, is_group_staff
from seahub.group.utils import BadGroupNameError, ConflictGroupNameError, \
@ -1491,6 +1492,9 @@ def get_dir_entrys_by_id(request, repo, path, dir_id, request_type=None):
else:
entry["locked_by_me"] = False
entry['modifier_email'] = dirent.modifier
entry['modifier_contact_email'] = email2contact_email(dirent.modifier)
entry['modifier_name'] = email2nickname(dirent.modifier)
entry["type"] = dtype
entry["name"] = dirent.obj_name
entry["id"] = dirent.obj_id

View File

@ -18,7 +18,8 @@ from django.utils.html import escape
from seahub.base.accounts import User
from seahub.profile.models import Profile
from seahub.profile.settings import NICKNAME_CACHE_TIMEOUT, NICKNAME_CACHE_PREFIX, \
EMAIL_ID_CACHE_TIMEOUT, EMAIL_ID_CACHE_PREFIX
EMAIL_ID_CACHE_TIMEOUT, EMAIL_ID_CACHE_PREFIX, CONTACT_CACHE_TIMEOUT, \
CONTACT_CACHE_PREFIX
from seahub.cconvert import CConvert
from seahub.po import TRANSLATION_MAP
from seahub.shortcuts import get_first_object_or_none
@ -371,7 +372,14 @@ def email2contact_email(value):
if not value:
return ''
return Profile.objects.get_contact_email_by_user(value)
key = normalize_cache_key(value, CONTACT_CACHE_PREFIX)
contact_email = cache.get(key)
if contact_email and contact_email.strip():
return contact_email
contact_email = Profile.objects.get_contact_email_by_user(value)
cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT)
return contact_email
@register.filter(name='email2id')
def email2id(value):

View File

@ -28,6 +28,19 @@ class ProfileManager(models.Manager):
profile.save(using=self._db)
return profile
def update_contact_email(self, username, contact_email):
"""
update contact_email of profile
"""
try:
profile = self.get(user=username)
profile.contact_email = contact_email
except Profile.DoesNotExist:
logger.warn('%s profile does not exists' % username)
return None
profile.save(using=self._db)
return profile
def get_profile_by_user(self, username):
"""Get a user's profile.
"""
@ -143,6 +156,9 @@ def clean_email_id_cache(sender, **kwargs):
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):
@receiver(post_save, sender=Profile, dispatch_uid="update_profile_cache")
def update_profile_cache(sender, instance, **kwargs):
"""
Set profile data to cache when profile data change.
"""
refresh_cache(instance.user)

View File

@ -6,3 +6,6 @@ NICKNAME_CACHE_PREFIX = getattr(settings, 'NICKNAME_CACHE_PREFIX', 'NICKNAME_')
EMAIL_ID_CACHE_TIMEOUT = getattr(settings, 'EMAIL_ID_CACHE_TIMEOUT', 14 * 24 * 60 * 60)
EMAIL_ID_CACHE_PREFIX = getattr(settings, 'EMAIL_ID_CACHE_PREFIX', 'EMAIL_ID_')
CONTACT_CACHE_TIMEOUT = getattr(settings, 'CONTACT_CACHE_TIMEOUT', 24 * 60 * 60)
CONTACT_CACHE_PREFIX = getattr(settings, 'CONTACT_CACHE_PREFIX', 'CONTACT_')

View File

@ -2,7 +2,8 @@
from django.core.cache import cache
from models import Profile
from settings import NICKNAME_CACHE_PREFIX, NICKNAME_CACHE_TIMEOUT
from settings import NICKNAME_CACHE_PREFIX, NICKNAME_CACHE_TIMEOUT, \
CONTACT_CACHE_TIMEOUT, CONTACT_CACHE_PREFIX
from seahub.shortcuts import get_first_object_or_none
from seahub.utils import normalize_cache_key
@ -12,7 +13,10 @@ def refresh_cache(username):
"""
profile = get_first_object_or_none(Profile.objects.filter(user=username))
nickname = profile.nickname if profile else username.split('@')[0]
contactemail = profile.contact_email if profile else ''
key = normalize_cache_key(username, NICKNAME_CACHE_PREFIX)
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
contact_key = normalize_cache_key(username, CONTACT_CACHE_PREFIX)
cache.set(contact_key, contactemail, CONTACT_CACHE_TIMEOUT)

View File

@ -1,9 +1,14 @@
import json
import os
import time
from django.core.urlresolvers import reverse
from django.core.cache import cache
from seahub.test_utils import BaseTestCase
from seahub.profile.models import Profile
from seahub.base.templatetags.seahub_tags import email2nickname, email2contact_email
from seahub.utils import normalize_cache_key
class DirTest(BaseTestCase):
def setUp(self):
@ -35,3 +40,27 @@ class DirTest(BaseTestCase):
})
self.assertEqual(400, resp.status_code)
def test_get_dir_file_modifier(self):
# upload the file , then test whether can get modifier
self.login_as(self.user)
self.text = self.create_file(repo_id=self.repo.id,
parent_dir='/',
filename='test.az',
username=self.user.username)
resp = self.client.get(self.endpoint)
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
assert json_resp[1]['type'] == 'file'
assert json_resp[1]['modifier_email'] == self.user.username
assert json_resp[1]['modifier_name'] == \
email2nickname(self.user.username)
assert json_resp[1]['modifier_contact_email'] == \
email2contact_email(self.user.username)
p = Profile.objects.add_or_update(self.user.username,
'test')
p = Profile.objects.update_contact_email(self.user.username, self.user.username)
assert cache.get(normalize_cache_key(self.user.username, 'CONTACT_')) == \
self.user.username