My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 8 attachment: subscriptions-patch2.txt (10.4 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
Index: views.py
===================================================================
--- views.py (revision 15)
+++ views.py (working copy)
@@ -3,12 +3,17 @@
and posts, adding new threads, and adding replies.
"""

-from forum.models import Forum,Thread,Post
+from forum.models import Forum,Thread,Post,Subscription
from datetime import datetime
from django.shortcuts import get_object_or_404, render_to_response
-from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseServerError
-from django.template import RequestContext
+from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseServerError, HttpResponseForbidden
+from django.template import RequestContext, Context, loader
from django import newforms as forms
+from django.core.mail import EmailMessage
+from django.conf import settings
+from django.template.defaultfilters import striptags, wordwrap
+from django.contrib.sites.models import Site
+from django.core.urlresolvers import reverse

def forum(request, slug):
"""
@@ -31,6 +36,7 @@
"""
t = get_object_or_404(Thread, pk=thread)
p = t.post_set.all().order_by('time')
+ s = t.subscription_set.filter(author=request.user)

t.views += 1
t.save()
@@ -40,6 +46,7 @@
'forum': t.forum,
'thread': t,
'posts': p,
+ 'subscription': s,
}))

def reply(request, thread):
@@ -47,11 +54,11 @@
If a thread isn't closed, and the user is logged in, post a reply
to a thread. Note we don't have "nested" replies at this stage.
"""
- if not request.user.is_authenticated:
- raise HttpResponseServerError
+ if not request.user.is_authenticated():
+ return HttpResponseServerError()
t = get_object_or_404(Thread, pk=thread)
if t.closed:
- raise HttpResponseServerError
+ return HttpResponseServerError()
body = request.POST.get('body', False)
p = Post(
thread=t,
@@ -60,6 +67,54 @@
time=datetime.now(),
)
p.save()
+
+ sub = Subscription.objects.filter(thread=t, author=request.user)
+ if request.POST.get('subscribe',False):
+ if not sub:
+ s = Subscription(
+ author=request.user,
+ thread=t
+ )
+ s.save()
+ else:
+ if sub:
+ sub.delete()
+
+ # Subscriptions are updated now send mail to all the authors subscribed in
+ # this thread.
+ mail_subject = ''
+ try:
+ mail_subject = settings.FORUM_MAIL_PREFIX
+ except AttributeError:
+ mail_subject = '[Forum]'
+
+ mail_from = ''
+ try:
+ mail_from = settings.FORUM_MAIL_FROM
+ except AttributeError:
+ mail_from = settings.DEFAULT_FROM_EMAIL
+
+ mail_tpl = loader.get_template('forum/notify.txt')
+ c = Context({
+ 'body': wordwrap(striptags(body), 72),
+ 'site_name' : Site.objects.get_current().name,
+ 'thread_url':
+ '%s%s' % (
+ Site.objects.get_current().domain,
+ reverse('forumthread', args=(t.id,) )),
+ })
+
+ #email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
+ # ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
+ # headers = {'Reply-To': 'another@example.com'})
+ email = EmailMessage(
+ subject=mail_subject+' '+striptags(t.title),
+ body= mail_tpl.render(c),
+ from_email=mail_from,
+ to=[mail_from],
+ bcc=[s.author.email for s in t.subscription_set.all()],)
+ email.send(fail_silently=True)
+
return HttpResponseRedirect(p.get_absolute_url())

def newthread(request, forum):
@@ -70,8 +125,8 @@

Only allows a user to post if they're logged in.
"""
- if not request.user.is_authenticated:
- raise HttpResponseServerError
+ if not request.user.is_authenticated():
+ return HttpResponseServerError()
f = get_object_or_404(Forum, slug=forum)
t = Thread(
forum=f,
@@ -85,4 +140,33 @@
time=datetime.now(),
)
p.save()
+ if request.POST.get('subscribe',False):
+ s = Subscription(
+ author=request.user,
+ thread=t
+ )
+ s.save()
return HttpResponseRedirect(t.get_absolute_url())
+
+def updatesubs(request):
+ """
+ Allow users to update their subscriptions all in one shot.
+ """
+ if not request.user.is_authenticated():
+ return HttpResponseForbidden('Sorry, you need to login.')
+
+ subs = Subscription.objects.filter(author=request.user)
+
+ if request.POST:
+ # remove the subscriptions that haven't been checked.
+ post_keys = [k for k in request.POST.keys()]
+ for s in subs:
+ if not str(s.thread.id) in post_keys:
+ s.delete()
+ return HttpResponseRedirect(reverse('forumsubs'))
+
+ return render_to_response('forum/updatesubs.html',
+ RequestContext(request, {
+ 'subs': subs,
+ }))
+
Index: models.py
===================================================================
--- models.py (revision 15)
+++ models.py (working copy)
@@ -20,7 +20,7 @@
All of the parent/child recursion code here is borrowed directly from
the Satchmo project: http://www.satchmoproject.com/
"""
- title = models.CharField(maxlength=100)
+ title = models.CharField(max_length=100)
slug = models.SlugField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
description = models.TextField()
@@ -137,7 +137,7 @@
automatically updated with saving a post or viewing the thread.
"""
forum = models.ForeignKey(Forum)
- title = models.CharField(maxlength=100)
+ title = models.CharField(max_length=100)
sticky = models.BooleanField(blank=True, null=True)
closed = models.BooleanField(blank=True, null=True)
posts = models.IntegerField(default=0)
@@ -203,3 +203,19 @@

def __unicode__(self):
return u"%s" % self.id
+
+class Subscription(models.Model):
+ """
+ Allow users to subscribe to threads.
+ """
+ author = models.ForeignKey(User)
+ thread = models.ForeignKey(Thread)
+
+ class Meta:
+ unique_together = (("author", "thread"),)
+
+ class Admin:
+ list_display = ['author','thread']
+
+ def __unicode__(self):
+ return u"%s to %s" % (self.author, self.thread)
Index: urls.py
===================================================================
--- urls.py (revision 15)
+++ urls.py (working copy)
@@ -19,12 +19,15 @@
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', forum_dict),

- (r'^thread/(?P<thread>[0-9]+)/$', 'forum.views.thread'),
+ url(r'^thread/(?P<thread>[0-9]+)/$', 'forum.views.thread', name='forumthread'),
(r'^thread/(?P<thread>[0-9]+)/reply/$', 'forum.views.reply'),

+ url(r'^subscriptions/$', 'forum.views.updatesubs', name='forumsubs'),
+
(r'^(?P<slug>[-\w]+)/$', 'forum.views.forum'),
(r'^(?P<forum>[-\w]+)/new/$', 'forum.views.newthread'),

(r'^([-\w/]+/)(?P<forum>[-\w]+)/new/$', 'forum.views.newthread'),
(r'^([-\w/]+/)(?P<slug>[-\w]+)/$', 'forum.views.forum'),
+
)
Index: templates/forum/updatesubs.html
===================================================================
--- templates/forum/updatesubs.html (revision 0)
+++ templates/forum/updatesubs.html (revision 0)
@@ -0,0 +1,37 @@
+{% extends "forum_base.html" %}
+{% block title %}Update Thread Subscriptions{% endblock %}
+{% block pagetitle %}Update Thread Subscriptions{% endblock %}
+
+
+{% block content %}
+
+{% if user.is_authenticated %}
+{% if not subs %}
+<p>No subscriptions.</p>
+{% else %}
+<form method='post' action='.'>
+<table id='djangoForumThreadList'>
+
+<tr>
+<th>Forum</th>
+<th>Thread</th>
+<th>Subscribed</th>
+</tr>
+
+{% for s in subs %}
+<tr>
+<td><a href='{{ s.thread.forum.get_absolute_url }}'>{{ s.thread.forum.title }}</a></td>
+<td>{% if s.thread.sticky %}Sticky {% endif %}<a href='{{ s.thread.get_absolute_url }}'>{{ s.thread.title }}</a>{% if s.thread.closed %} (Closed){% endif %}</td>
+<td><input type='checkbox' checked='checked' name='{{ s.thread.id }}' /></td>
+</tr>
+{% endfor %}
+</table>
+
+<p><input type='submit' value='Update Subscriptions' name="updatesubs" /></p>
+</form>
+{% endif %}
+{% else %}
+<p>Please login to update your forum subscriptions.</p>
+{% endif %}
+
+{% endblock %}
Index: templates/forum/thread_list.html
===================================================================
--- templates/forum/thread_list.html (revision 15)
+++ templates/forum/thread_list.html (working copy)
@@ -51,6 +51,7 @@
<p><label>Posting As</label><span>{{ user.username }}</span></p>
<p><label for='body'>Body</label>
<textarea name='body' rows='8' cols='50'></textarea></p>
+<p><label for='subscribe'>Subscribe via email:</label><input type='checkbox' name='subscribe' /></p>
<p><input type='submit' value='Post' /></p>
</form>
{% else %}
Index: templates/forum/forum_list.html
===================================================================
--- templates/forum/forum_list.html (revision 15)
+++ templates/forum/forum_list.html (working copy)
@@ -15,4 +15,5 @@
</tr>
{% endfor %}
</table>
+<p><a href="{% url forumsubs %}">Update Subscriptions</a></p>
{% endblock %}
Index: templates/forum/notify.txt
===================================================================
--- templates/forum/notify.txt (revision 0)
+++ templates/forum/notify.txt (revision 0)
@@ -0,0 +1,7 @@
+
+{{ body }}
+
+--
+You received this message because you subscribed to a forum thread at
+{{site_name}}. Login below to undo that:
+http://{{thread_url}}
Index: templates/forum/thread.html
===================================================================
--- templates/forum/thread.html (revision 15)
+++ templates/forum/thread.html (working copy)
@@ -20,6 +20,7 @@

</table>

+<h2><a href="{% url forumsubs %}">Update Subscriptions</a></h2>
<h2>Post a Reply</h2>
{% if thread.closed %}
<p>Sorry, this thread is closed. No further replies are permitted.</p>
@@ -29,6 +30,7 @@
<p><label>Posting As</label><span>{{ user.username }}</span></p>
<p><label for='body'>Your Post</label>
<textarea name='body' rows='8' cols='50'></textarea></p>
+<p><label for='subscribe'>Subscribe via email:</label><input type='checkbox' name='subscribe' {% if subscription %}checked{% endif %}/></p>
<input type='submit' value='Submit' />
</form>
{% else %}
Powered by Google Project Hosting