1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-25 06:33:48 +00:00

add events to community version; add events api

This commit is contained in:
lins05
2013-11-27 12:48:50 +08:00
parent 3360cc6780
commit cd20110375
3 changed files with 118 additions and 10 deletions

View File

@@ -28,6 +28,9 @@ urlpatterns = patterns('',
url(r'^beshared-repos/$', BeShared.as_view(), name='beshared'), url(r'^beshared-repos/$', BeShared.as_view(), name='beshared'),
url(r'^groups/$', Groups.as_view()), 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/events/$', ActivityHtml.as_view()),
url(r'^html/more_events/$', AjaxEvents.as_view(), name="more_events"), url(r'^html/more_events/$', AjaxEvents.as_view(), name="more_events"),
url(r'^html/repo_history_changes/(?P<repo_id>[-0-9a-f]{36})/$', RepoHistoryChangeHtml.as_view(), name='api_repo_history_changes'), url(r'^html/repo_history_changes/(?P<repo_id>[-0-9a-f]{36})/$', RepoHistoryChangeHtml.as_view(), name='api_repo_history_changes'),

View File

@@ -3,6 +3,7 @@ import os
import stat import stat
import time import time
import simplejson as json import simplejson as json
import datetime
from urllib2 import unquote, quote from urllib2 import unquote, quote
from rest_framework import parsers 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.template.loader import render_to_string
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils import timezone
from models import Token from models import Token
from authentication import TokenAuthentication 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, \ get_ccnet_server_addr_port, string2list, \
gen_block_get_url gen_block_get_url
from seahub.utils.star import star_file, unstar_file 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 import seahub.settings as settings
try: try:
from seahub.settings import CLOUD_MODE from seahub.settings import CLOUD_MODE
except ImportError: except ImportError:
@@ -1873,6 +1879,107 @@ class AjaxDiscussions(APIView):
def get(self, request, group_id, format=None): def get(self, request, group_id, format=None):
return more_discussions(request, group_id) 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): class ActivityHtml(APIView):
authentication_classes = (TokenAuthentication, ) authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)

View File

@@ -974,16 +974,14 @@ def more_files_in_commit(commit):
HAS_OFFICE_CONVERTER = False HAS_OFFICE_CONVERTER = False
if EVENTS_CONFIG_FILE: if EVENTS_CONFIG_FILE:
def check_office_converter_enabled(): def check_office_converter_enabled():
enabled = False config = ConfigParser.ConfigParser()
if hasattr(seafevents, 'is_office_converter_enabled'): config.read(EVENTS_CONFIG_FILE)
config = ConfigParser.ConfigParser() enabled = seafevents.is_office_converter_enabled(config)
config.read(EVENTS_CONFIG_FILE)
enabled = seafevents.is_office_converter_enabled(config)
if enabled: if enabled:
logging.debug('office converter: enabled') logging.debug('office converter: enabled')
else: else:
logging.debug('office converter: not enabled') logging.debug('office converter: not enabled')
return enabled return enabled
def get_office_converter_html_dir(): def get_office_converter_html_dir():
@@ -1059,7 +1057,7 @@ HAS_FILE_SEARCH = False
if EVENTS_CONFIG_FILE: if EVENTS_CONFIG_FILE:
def check_search_enabled(): def check_search_enabled():
enabled = False enabled = False
if hasattr(seafevents, 'is_office_converter_enabled'): if hasattr(seafevents, 'is_search_enabled'):
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
config.read(EVENTS_CONFIG_FILE) config.read(EVENTS_CONFIG_FILE)
enabled = seafevents.is_search_enabled(config) enabled = seafevents.is_search_enabled(config)