1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-12 13:24:52 +00:00

Add nickname cache

This commit is contained in:
xiez
2012-08-14 13:18:55 +08:00
parent b9f2678220
commit c42381dc4c
5 changed files with 49 additions and 13 deletions

View File

@@ -4,11 +4,14 @@ import re
from datetime import datetime
from django import template
from django.core.cache import cache
from django.utils.safestring import mark_safe
from profile.models import Profile
from profile.settings import NICKNAME_CACHE_TIMEOUT, NICKNAME_CACHE_PREFIX
from seahub.settings import FILEEXT_ICON_MAP
from seahub.po import TRANSLATION_MAP
from seahub.profile.models import Profile
from seahub.shortcuts import get_first_object_or_none
register = template.Library()
@@ -114,11 +117,12 @@ def translate_remain_time(value):
@register.filter(name='email2nickname')
def email2nickname(value):
try:
profile = Profile.objects.get(user=value)
return profile.nickname
except Profile.DoesNotExist:
return value.split('@')[0]
nickname = cache.get(NICKNAME_CACHE_PREFIX+value)
if not nickname:
profile = get_first_object_or_none(Profile.objects.filter(user=value))
nickname = profile.nickname if profile else value.split('@')[0]
cache.set(NICKNAME_CACHE_PREFIX+value, nickname, NICKNAME_CACHE_TIMEOUT)
return nickname
@register.filter(name='url_target_blank')
def url_target_blank(text):

4
profile/settings.py Normal file
View File

@@ -0,0 +1,4 @@
from django.conf import settings
NICKNAME_CACHE_TIMEOUT = getattr(settings, 'NICKNAME_CACHE_TIMEOUT', 24 * 60 * 60)
NICKNAME_CACHE_PREFIX = getattr(settings, 'NICKNAME_CACHE_PREFIX', 'NICKNAME_')

View File

@@ -7,9 +7,14 @@
{% block right_panel %}
<div id="user-basic-info">
<h2>个人基本信息修改</h2>
{% if modified %}
<p class="notification">修改成功。</p>
{% if messages %}
<ul class="messages hide">
{% for message in messages %}
<li class="info">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<form action="" method="post">
<label>头像:</label>{% avatar request.user.username 60 %}
<a href="{{ SITE_ROOT }}avatar/add/">更改</a><br />

14
profile/utils.py Normal file
View File

@@ -0,0 +1,14 @@
from django.core.cache import cache
from models import Profile
from settings import NICKNAME_CACHE_PREFIX, NICKNAME_CACHE_TIMEOUT
from seahub.shortcuts import get_first_object_or_none
def refresh_cache(user):
"""
Function to be called when change user nickname.
"""
profile = get_first_object_or_none(Profile.objects.filter(user=user))
nickname = profile.nickname if profile else value.split('@')[0]
cache.set(NICKNAME_CACHE_PREFIX+user, nickname, NICKNAME_CACHE_TIMEOUT)

View File

@@ -4,6 +4,7 @@ from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import Context, RequestContext
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from seaserv import ccnet_rpc, ccnet_threaded_rpc, get_binding_peerids
@@ -11,18 +12,20 @@ from pysearpc import SearpcError
from forms import ProfileForm
from models import Profile
from utils import render_error
from utils import refresh_cache
from seahub.utils import render_error
from seahub.base.accounts import User
from seahub.contacts.models import Contact
@login_required
def edit_profile(request):
modified = False
"""
Show and edit user profile.
"""
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
modified = True
nickname = form.cleaned_data['nickname']
intro = form.cleaned_data['intro']
try:
@@ -34,6 +37,13 @@ def edit_profile(request):
profile.nickname = nickname
profile.intro = intro
profile.save()
messages.add_message(request, messages.INFO, u'修改成功')
# refresh nickname cache
refresh_cache(request.user.username)
return HttpResponseRedirect(reverse('edit_profile'))
else:
messages.add_message(request, messages.ERROR, u'修改失败')
else:
try:
profile = Profile.objects.get(user=request.user.username)
@@ -46,7 +56,6 @@ def edit_profile(request):
return render_to_response('profile/set_profile.html', {
'form': form,
'modified': modified,
}, context_instance=RequestContext(request))
@login_required