diff --git a/seahub/api2/urls.py b/seahub/api2/urls.py index c778fb41e8..85b514fe4f 100644 --- a/seahub/api2/urls.py +++ b/seahub/api2/urls.py @@ -28,6 +28,9 @@ urlpatterns = patterns('', url(r'^beshared-repos/$', BeShared.as_view(), name='beshared'), url(r'^groups/$', Groups.as_view()), + url(r'^events/$', EventsView.as_view()), + url(r'^msgs_count/$', MessagesCountView.as_view()), + url(r'^avatar/$', AvatarView.as_view()), url(r'^html/events/$', ActivityHtml.as_view()), url(r'^html/more_events/$', AjaxEvents.as_view(), name="more_events"), url(r'^html/repo_history_changes/(?P[-0-9a-f]{36})/$', RepoHistoryChangeHtml.as_view(), name='api_repo_history_changes'), diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 8ac0577fa1..719a783581 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -3,6 +3,7 @@ import os import stat import time import simplejson as json +import datetime from urllib2 import unquote, quote from rest_framework import parsers @@ -22,6 +23,7 @@ from django.template import Context, loader, RequestContext from django.template.loader import render_to_string from django.shortcuts import render_to_response from django.utils.translation import ugettext as _ +from django.utils import timezone from models import Token from authentication import TokenAuthentication @@ -37,7 +39,11 @@ from seahub.utils import gen_file_get_url, gen_token, gen_file_upload_url, \ get_ccnet_server_addr_port, string2list, \ gen_block_get_url from seahub.utils.star import star_file, unstar_file +from seahub.base.templatetags.seahub_tags import email2nickname +from seahub.avatar.templatetags.avatar_tags import avatar_url +from seahub.message.models import UserMessage import seahub.settings as settings + try: from seahub.settings import CLOUD_MODE except ImportError: @@ -1873,6 +1879,107 @@ class AjaxDiscussions(APIView): def get(self, request, group_id, format=None): return more_discussions(request, group_id) +class EventsView(APIView): + authentication_classes = (TokenAuthentication, ) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, format=None): + if not EVENTS_ENABLED: + events = None + return api_error(status.HTTP_404_NOT_FOUND, 'Events not enabled.') + + start = request.GET.get('start', '') + + if not start: + start = 0 + else: + try: + start = int(start) + except ValueError: + return api_error(status.HTTP_400_BAD_REQUEST, 'start id must be integer') + + email = request.user.username + events_count = 15 + events, events_more_offset = get_user_events(email, start, events_count) + events_more = True if len(events) == events_count else False + + l = [] + for e in events: + d = dict(etype=e.etype) + l.append(d) + if e.etype == 'repo-update': + d['author'] = e.commit.creator_name + d['time'] = e.commit.ctime + d['desc'] = e.commit.desc + d['repo_id'] = e.repo.id + d['repo_name'] = e.repo.name + else: + d['repo_id'] = e.repo_id + d['repo_name'] = e.repo_name + if e.etype == 'repo-create': + d['author'] = e.creator + else: + d['author'] = e.repo_owner + + def utc_to_local(dt): + tz = timezone.get_default_timezone() + utc = dt.replace(tzinfo=timezone.utc) + local = timezone.make_naive(utc, tz) + return local + + epoch = datetime.datetime(1970, 1, 1) + local = utc_to_local(e.timestamp) + d['time'] = (local - epoch).total_seconds() * 1000 + + d['nick'] = email2nickname(d['author']) + + resp = HttpResponse(json.dumps({ + 'events': l, + 'more': events_more, + 'more_offset': events_more_offset,}), + status=200, + content_type=json_content_type) + return resp + +class MessagesCountView(APIView): + authentication_classes = (TokenAuthentication, ) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, format=None): + username = request.user.username + ret = {} + + notes = UserNotification.objects.filter(to_user=username) + ret['group_messages'] = len(notes) + ret['personal_messages'] = UserMessage.objects.count_unread_messages_by_user(username) + + return HttpResponse(json.dumps(ret), status=200, + content_type=json_content_type) + +class AvatarView(APIView): + authentication_classes = (TokenAuthentication, ) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, format=None): + email = request.GET.get('user', request.user.username) + + size = request.GET.get('size', None) + if size is None: + return api_error(status.HTTP_400_BAD_REQUEST, 'size param is required') + + try: + size = int(size) + except: + return api_error(status.HTTP_400_BAD_REQUEST, 'invalid size param') + + url = avatar_url(email, size) + ret = { 'url': url } + return HttpResponse(json.dumps(ret), status=200, + content_type=json_content_type) + class ActivityHtml(APIView): authentication_classes = (TokenAuthentication, ) permission_classes = (IsAuthenticated,) diff --git a/seahub/utils/__init__.py b/seahub/utils/__init__.py index fe449b1661..7ac467b60d 100644 --- a/seahub/utils/__init__.py +++ b/seahub/utils/__init__.py @@ -974,16 +974,14 @@ def more_files_in_commit(commit): HAS_OFFICE_CONVERTER = False if EVENTS_CONFIG_FILE: def check_office_converter_enabled(): - enabled = False - if hasattr(seafevents, 'is_office_converter_enabled'): - config = ConfigParser.ConfigParser() - config.read(EVENTS_CONFIG_FILE) - enabled = seafevents.is_office_converter_enabled(config) + config = ConfigParser.ConfigParser() + config.read(EVENTS_CONFIG_FILE) + enabled = seafevents.is_office_converter_enabled(config) - if enabled: - logging.debug('office converter: enabled') - else: - logging.debug('office converter: not enabled') + if enabled: + logging.debug('office converter: enabled') + else: + logging.debug('office converter: not enabled') return enabled def get_office_converter_html_dir(): @@ -1059,7 +1057,7 @@ HAS_FILE_SEARCH = False if EVENTS_CONFIG_FILE: def check_search_enabled(): enabled = False - if hasattr(seafevents, 'is_office_converter_enabled'): + if hasattr(seafevents, 'is_search_enabled'): config = ConfigParser.ConfigParser() config.read(EVENTS_CONFIG_FILE) enabled = seafevents.is_search_enabled(config)