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