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

[api2] Improve api for user avatar and add for group avatar

This commit is contained in:
poetwang
2014-04-13 01:37:04 +08:00
parent 6f0c5b97f4
commit 0cf6994d29
5 changed files with 66 additions and 13 deletions

View File

@@ -57,7 +57,8 @@ urlpatterns = patterns('',
url(r'^user/msgs/(?P<id_or_email>[^/]+)/$', UserMsgsView.as_view()),
url(r'^new_replies/$', NewRepliesView.as_view()),
url(r'^avatars/(?P<user>\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/resized/(?P<size>[0-9]+)/$', AvatarView.as_view()),
url(r'^avatars/user/(?P<user>\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/resized/(?P<size>[0-9]+)/$', UserAvatarView.as_view()),
url(r'^avatars/group/(?P<group_id>\d+)/resized/(?P<size>[0-9]+)/$', GroupAvatarView.as_view()),
url(r'^groups/$', Groups.as_view()),
url(r'^groups/(?P<group_id>\d+)/members/$', GroupMembers.as_view()),

View File

@@ -283,7 +283,9 @@ def get_group_msgs(groupid, page, username):
return group_msgs
def get_timetamp(msgtimestamp):
def get_timestamp(msgtimestamp):
if not msgtimestamp:
return 0
timestamp = int(time.mktime(msgtimestamp.timetuple()))
return timestamp

View File

@@ -29,9 +29,10 @@ from serializers import AuthTokenSerializer, AccountSerializer
from utils import is_repo_writable, is_repo_accessible, calculate_repo_info, \
api_error, get_file_size, prepare_starred_files, \
get_groups, get_group_and_contacts, prepare_events, \
get_person_msgs, api_group_check, get_email, get_timetamp, \
get_person_msgs, api_group_check, get_email, get_timestamp, \
get_group_message_json, get_group_msgs, get_group_msgs_json
from seahub.avatar.templatetags.avatar_tags import avatar_url
from seahub.avatar.templatetags.avatar_tags import api_avatar_url
from seahub.avatar.templatetags.group_avatar_tags import api_grp_avatar_url
from seahub.base.accounts import User
from seahub.base.models import FileDiscuss, UserStarredFiles, \
DirFilesLastModifiedInfo, DeviceToken
@@ -137,7 +138,7 @@ class ObtainAuthToken(APIView):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
########## Accounts
########## Accounts
class Accounts(APIView):
"""List all accounts.
Administator permission is required.
@@ -1801,7 +1802,7 @@ class DirSubRepoView(APIView):
return HttpResponse(json.dumps(result), content_type=json_content_type)
########## Sharing
########## Sharing
class SharedRepos(APIView):
"""
List repos that a user share to others/groups/public.
@@ -1953,7 +1954,7 @@ class PrivateSharedFileView(APIView):
if not repo:
return api_error(status.HTTP_404_NOT_FOUND, "Repo not found")
path = fileshare.path.rstrip('/') # Normalize file path
path = fileshare.path.rstrip('/') # Normalize file path
file_name = os.path.basename(path)
file_id = None
@@ -1989,7 +1990,7 @@ class SharedFileView(APIView):
if not repo:
return api_error(status.HTTP_404_NOT_FOUND, "Repo not found")
path = fileshare.path.rstrip('/') # Normalize file path
path = fileshare.path.rstrip('/') # Normalize file path
file_name = os.path.basename(path)
file_id = None
@@ -2086,7 +2087,7 @@ class PrivateSharedFileDetailView(APIView):
if not repo:
return api_error(status.HTTP_404_NOT_FOUND, "Repo not found")
path = fileshare.path.rstrip('/') # Normalize file path
path = fileshare.path.rstrip('/') # Normalize file path
file_name = os.path.basename(path)
file_id = None
@@ -2642,7 +2643,7 @@ class UserMsgsView(APIView):
m = {
'from_email' : msg.from_email,
'nickname' : email2nickname(msg.from_email),
'timestamp' : get_timetamp(msg.timestamp),
'timestamp' : get_timestamp(msg.timestamp),
'msg' : msg.message,
'attachments' : atts,
'msgid' : msg.message_id,
@@ -2688,16 +2689,31 @@ class NewRepliesView(APIView):
UserNotification.objects.seen_group_msg_reply_notice(request.user.username)
return Response(group_msgs)
class AvatarView(APIView):
class UserAvatarView(APIView):
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def get(self, request, user, size, format=None):
url = avatar_url(user, int(size))
ret = { 'url': url }
url, is_default, date_uploaded = api_avatar_url(user, int(size))
ret = {
"url" : request.build_absolute_uri(url),
"is_default" : is_default,
"mtime": get_timestamp(date_uploaded) }
return Response(ret)
class GroupAvatarView(APIView):
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle, )
def get(self, request, group_id, size, format=None):
url, is_default, date_uploaded = api_grp_avatar_url(group_id, int(size))
ret = {
"url" : request.build_absolute_uri(url),
"is_default" : is_default,
"mtime": get_timestamp(date_uploaded)}
return Response(ret)
# Html related code
def html_events(request):

View File

@@ -34,6 +34,17 @@ def avatar_url(user, size=AVATAR_DEFAULT_SIZE):
else:
return get_default_avatar_url()
@cache_result
@register.simple_tag
def api_avatar_url(user, size=AVATAR_DEFAULT_SIZE):
avatar = get_primary_avatar(user, size=size)
if avatar:
url = avatar.avatar_url(size)
date_uploaded = avatar.date_uploaded
return url, False, date_uploaded
else:
return get_default_avatar_url(), True, None
@cache_result
@register.simple_tag
def avatar(user, size=AVATAR_DEFAULT_SIZE):

View File

@@ -25,6 +25,29 @@ def get_default_group_avatar_url():
return '%s/%s' % (base_url, GROUP_AVATAR_DEFAULT_URL)
return '%s%s' % (base_url, GROUP_AVATAR_DEFAULT_URL)
@register.simple_tag
def api_grp_avatar_url(group_id, size=GROUP_AVATAR_DEFAULT_SIZE):
url = None
key = get_grp_cache_key(group_id, size)
val = cache.get(key)
if val:
return val.avatar_url(size), False, val.date_uploaded
# Get from DB, and refresh cache
grp_avatars = GroupAvatar.objects.filter(group_id=group_id)
if grp_avatars:
avatar = grp_avatars.order_by('-date_uploaded')[0]
else:
avatar = None
if avatar:
if not avatar.thumbnail_exists(size):
avatar.create_thumbnail(size)
return avatar.avatar_url(size), False, avatar.date_uploaded
else:
return get_default_group_avatar_url(), True, None
@register.simple_tag
def grp_avatar(group_id, size=GROUP_AVATAR_DEFAULT_SIZE):
# Get from cache