|
0
|
1 |
from django.conf import settings |
|
|
2 |
from django.core.urlresolvers import reverse |
|
|
3 |
from django.http import HttpResponse, Http404, HttpResponseRedirect |
|
|
4 |
from django.shortcuts import get_object_or_404, render_to_response |
|
|
5 |
from django.template.loader import render_to_string |
|
|
6 |
from django.utils import feedgenerator |
|
|
7 |
from django.utils.translation import ugettext as _ |
|
|
8 |
from django.template import RequestContext |
|
|
9 |
from django.contrib.auth.models import AnonymousUser |
|
|
10 |
from cm.security import get_viewable_activities |
|
|
11 |
from cm.models import ApplicationConfiguration, Activity, Configuration |
|
|
12 |
import time |
|
|
13 |
from cm.exception import UnauthorizedException |
|
|
14 |
import re |
|
169
|
15 |
from urlparse import urlparse |
|
0
|
16 |
# taken from django's feedgenerator.py and changed to support multiple posts in minute |
|
|
17 |
def get_tag_uri(url, date): |
|
|
18 |
"Creates a TagURI. See http://diveintomark.org/archives/2004/05/28/howto-atom-id" |
|
169
|
19 |
parsed_url = urlparse(url) |
|
|
20 |
time_stp = time.mktime(date.timetuple()) if date is not None else '' |
|
|
21 |
return u'tag:%s,%s:%s' %(parsed_url.hostname, time_stp, parsed_url.path) # + '/' + parsed_url.fragment |
|
0
|
22 |
|
|
|
23 |
|
|
|
24 |
def public_feed(request): |
|
|
25 |
feed_title = _(u"%(workspace_name)s's public feed" % {'workspace_name' : ApplicationConfiguration['workspace_name']}) |
|
|
26 |
feed_description = _(u"Workspace %(workspace_name)s public activity feed") % {'workspace_name' : ApplicationConfiguration['workspace_name']} |
|
|
27 |
request.user = AnonymousUser() |
|
|
28 |
activitites = get_viewable_activities(request, {'view_comments' : 1, 'view_texts' : 1}) |
|
|
29 |
return _feed(request, activitites, feed_title, feed_description) |
|
|
30 |
|
|
|
31 |
def private_feed(request, key): |
|
|
32 |
private_feed_key = Configuration.objects.get_key('private_feed_key', None) |
|
|
33 |
if private_feed_key != key: |
|
|
34 |
raise Http404 |
|
|
35 |
|
|
|
36 |
feed_title = _(u"%(workspace_name)s's private feed" % {'workspace_name' : ApplicationConfiguration['workspace_name']}) |
|
|
37 |
feed_description = _(u"Workspace %(workspace_name)s private feed") % {'workspace_name' : ApplicationConfiguration['workspace_name']} |
|
|
38 |
activities = Activity.objects.filter(type__in=Activity.ACTIVITIES_TYPES).order_by('-created') |
|
|
39 |
return _feed(request, activities, feed_title, feed_description) |
|
|
40 |
|
|
|
41 |
def _feed(request, activities, feed_title, feed_description): |
|
|
42 |
feed = feedgenerator.Atom1Feed( |
|
|
43 |
title=feed_title, |
|
|
44 |
link=settings.SITE_URL, |
|
|
45 |
description=feed_description, |
|
|
46 |
language='en' # TODO: something better to do? |
|
|
47 |
) |
|
|
48 |
|
|
|
49 |
for activity in activities: |
|
|
50 |
item_data = activity.printable_data() |
|
|
51 |
item_data_text = activity.printable_data(html=False, link=False) |
|
|
52 |
item_metadata = activity.created # TODO dateformat |
|
|
53 |
item_body = render_to_string('feed/feed_item.txt', |
|
|
54 |
{ 'title': item_data, |
|
|
55 |
'body' : item_metadata, |
|
|
56 |
}, context_instance = RequestContext(request)) |
|
|
57 |
feed.add_item(title=item_data_text, |
|
|
58 |
link=settings.SITE_URL, |
|
|
59 |
description=item_body, |
|
|
60 |
pubdate=activity.created, |
|
|
61 |
unique_id=get_tag_uri(settings.SITE_URL, activity.created), |
|
|
62 |
author_name=activity.user.username if activity.user else '-', |
|
|
63 |
) |
|
|
64 |
|
|
|
65 |
response = HttpResponse(mimetype=feed.mime_type) |
|
|
66 |
feed.write(response, 'utf-8') |
|
|
67 |
return response |
|
|
68 |
|
|
|
69 |
def text_feed(request, key): |
|
|
70 |
from cm.views import get_text_by_keys_or_404 |
|
|
71 |
text = get_text_by_keys_or_404(key) |
|
|
72 |
feed_title = _(u"Text %(text_title)s's public feed" % {'text_title' : text.title}) |
|
|
73 |
feed_description = _(u"Public activity feed for text %(text_title)s") % {'text_title' : text.title} |
|
|
74 |
request.user = AnonymousUser() |
|
|
75 |
activitites = get_viewable_activities(request, {'view_comments' : 1, 'view_texts' : 1}, text=text) |
|
|
76 |
return _feed(request, activitites, feed_title, feed_description) |
|
|
77 |
|
|
|
78 |
def text_feed_private(request, key, private_feed_key): |
|
|
79 |
from cm.views import get_text_by_keys_or_404 |
|
|
80 |
text = get_text_by_keys_or_404(key) |
|
|
81 |
if text.private_feed_key != private_feed_key: |
|
|
82 |
raise Http404 |
|
|
83 |
|
|
|
84 |
feed_title = _(u"Text %(text_title)s's private feed" % {'text_title' : text.title}) |
|
|
85 |
feed_description = _(u"Private activity feed for text %(text_title)s") % {'text_title' : text.title} |
|
|
86 |
activities = Activity.objects.filter(type__in=Activity.ACTIVITIES_TYPES, text=text).order_by('-created') |
|
|
87 |
return _feed(request, activities, feed_title, feed_description) |