comparison gpp/messages/views.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children ab7830b067b3
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 """Views for the messages application"""
2
3 import datetime
4 from django.shortcuts import render_to_response
5 from django.template import RequestContext
6 from django.contrib.auth.models import User
7 from django.http import HttpResponseRedirect
8 from django.http import HttpResponse
9 from django.http import HttpResponseBadRequest
10 from django.http import HttpResponseForbidden
11 from django.contrib.auth.decorators import login_required
12 from django.shortcuts import get_object_or_404
13 from django.core.urlresolvers import reverse
14 from django.http import Http404
15
16 from messages.models import Message
17 from messages.models import Options
18 from messages.forms import ComposeForm
19 from messages.forms import OptionsForm
20 from messages.utils import reply_subject
21 from messages.utils import quote_message
22
23
24 BOX_MAP = {
25 'inbox': 'messages-inbox',
26 'outbox': 'messages-outbox',
27 'trash': 'messages-trash',
28 }
29
30
31 def box_redirect(request):
32 """
33 Determines which box to redirect to by looking for a GET or
34 POST parameter.
35 """
36 if request.method == 'GET':
37 box = request.GET.get('box', 'inbox')
38 else:
39 box = request.POST.get('box', 'inbox')
40 if BOX_MAP.has_key(box):
41 url = reverse(BOX_MAP[box])
42 else:
43 url = reverse(BOX_MAP['inbox'])
44 return HttpResponseRedirect(url)
45
46
47 @login_required
48 def inbox(request):
49 """Displays the inbox for the user making the request."""
50 msgs = Message.objects.inbox(request.user)
51 return render_to_response('messages/inbox.html', {
52 'msgs': msgs,
53 },
54 context_instance = RequestContext(request))
55
56
57 @login_required
58 def outbox(request):
59 """Displays the outbox for the user making the request."""
60 msgs = Message.objects.outbox(request.user)
61 return render_to_response('messages/outbox.html', {
62 'msgs': msgs,
63 },
64 context_instance = RequestContext(request))
65
66
67 @login_required
68 def trash(request):
69 """Displays the trash for the user making the request."""
70 msgs = Message.objects.trash(request.user)
71 return render_to_response('messages/trash.html', {
72 'msgs': msgs,
73 },
74 context_instance = RequestContext(request))
75
76
77 @login_required
78 def view(request, msg_id):
79 """
80 View a given message. Only the sender or receiver can see
81 the message.
82 """
83 msg = get_object_or_404(Message, pk=msg_id)
84 if msg.sender != request.user and msg.receiver != request.user:
85 raise Http404
86
87 if msg.receiver == request.user and msg.read_date is None:
88 msg.read_date = datetime.datetime.now()
89 msg.save()
90
91 box = request.GET.get('box', None)
92
93 return render_to_response('messages/view.html', {
94 'box': box,
95 'msg': msg,
96 'is_deleted': msg.is_deleted(request.user),
97 },
98 context_instance = RequestContext(request))
99
100
101 @login_required
102 def reply(request, msg_id):
103 """
104 Process or prepare the compose form in order to reply
105 to a given message.
106 """
107 msg = get_object_or_404(Message, pk=msg_id)
108
109 if request.method == "POST":
110 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
111 return box_redirect(request)
112 compose_form = ComposeForm(request.user, request.POST)
113 if compose_form.is_valid():
114 compose_form.save(sender=request.user, parent_msg=msg)
115 request.user.message_set.create(message='Reply sent.')
116 return box_redirect(request)
117 else:
118 if msg.receiver == request.user:
119 receiver_name = msg.sender.username
120 else:
121 # replying to message in outbox
122 receiver_name = msg.receiver.username
123
124 form_data = {
125 'receiver': receiver_name,
126 'subject': reply_subject(msg.subject),
127 'message': quote_message(msg.sender, msg.send_date, msg.message),
128 'box': request.GET.get('box', 'inbox'),
129 }
130
131 compose_form = ComposeForm(request.user, initial=form_data)
132
133 return render_to_response('messages/compose.html', {
134 'compose_form': compose_form,
135 },
136 context_instance = RequestContext(request))
137
138
139 @login_required
140 def compose(request, receiver=None):
141 """
142 Process or prepare the compose form in order to create
143 a new message.
144 """
145 if request.method == "POST":
146 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
147 return HttpResponseRedirect(reverse('messages-inbox'))
148 compose_form = ComposeForm(request.user, request.POST)
149 if compose_form.is_valid():
150 compose_form.save(sender=request.user)
151 request.user.message_set.create(message='Message sent.')
152 return HttpResponseRedirect(reverse('messages-inbox'))
153 else:
154 if receiver is not None:
155 form_data = {
156 'receiver': receiver,
157 }
158 compose_form = ComposeForm(request.user, initial=form_data)
159 else:
160 compose_form = ComposeForm(request.user)
161
162 return render_to_response('messages/compose.html', {
163 'compose_form': compose_form,
164 },
165 context_instance = RequestContext(request))
166
167
168 @login_required
169 def delete(request, msg_id):
170 """
171 Deletes a given message. The user must be either the sender or
172 receiver for this to succeed.
173 """
174 msg = get_object_or_404(Message, pk=msg_id)
175 if msg.sender == request.user:
176 msg.sender_delete_date = datetime.datetime.now()
177 elif msg.receiver == request.user:
178 msg.receiver_delete_date = datetime.datetime.now()
179 else:
180 raise Http404
181 msg.save()
182 request.user.message_set.create(message='Message sent to trash.')
183
184 return box_redirect(request)
185
186
187 @login_required
188 def delete_bulk(request):
189 """
190 Deletes messages in bulk. The message ID's to be deleted are expected
191 to be in the delete POST array. The user must be either the sender
192 or receiver for this to succeed.
193 """
194 if request.method == "POST":
195 delete_ids = request.POST.getlist('delete_ids')
196 try:
197 delete_ids = [int(id) for id in delete_ids]
198 except ValueError:
199 raise Http404
200 msgs = Message.objects.filter(id__in = delete_ids)
201 now = datetime.datetime.now()
202 for msg in msgs:
203 if msg.sender == request.user:
204 msg.sender_delete_date = now
205 msg.save()
206 elif msg.receiver == request.user:
207 msg.receiver_delete_date = now
208 msg.save()
209 request.user.message_set.create(message='Messages sent to the trash.')
210
211 return box_redirect(request)
212
213
214 @login_required
215 def undelete(request, msg_id):
216 """
217 Undeletes a given message. The user must be either the sender or
218 receiver for this to succeed.
219 """
220 msg = get_object_or_404(Message, pk=msg_id)
221 if msg.sender == request.user:
222 msg.sender_delete_date = None
223 elif msg.receiver == request.user:
224 msg.receiver_delete_date = None
225 else:
226 raise Http404
227 msg.save()
228 request.user.message_set.create(message='Message retrieved from the trash.')
229
230 return box_redirect(request)
231
232
233 @login_required
234 def undelete_bulk(request):
235 """
236 Undeletes messages in bulk. The message ID's to be deleted are expected
237 to be in the delete POST array. The user must be either the sender
238 or receiver for this to succeed.
239 """
240 if request.method == "POST":
241 undelete_ids = request.POST.getlist('undelete_ids')
242 try:
243 undelete_ids = [int(id) for id in undelete_ids]
244 except ValueError:
245 raise Http404
246 msgs = Message.objects.filter(id__in = undelete_ids)
247 for msg in msgs:
248 if msg.sender == request.user:
249 msg.sender_delete_date = None
250 msg.save()
251 elif msg.receiver == request.user:
252 msg.receiver_delete_date = None
253 msg.save()
254 request.user.message_set.create(message='Messages retrieved from the trash.')
255
256 return box_redirect(request)
257
258
259 @login_required
260 def options(request):
261 """
262 View to display/change user options.
263 """
264 if request.method == "POST":
265 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
266 return HttpResponseRedirect(reverse('messages-inbox'))
267 options = Options.objects.for_user(request.user)
268 form = OptionsForm(request.POST, instance=options)
269 if form.is_valid():
270 form.save()
271 request.user.message_set.create(message='Options saved.')
272 return HttpResponseRedirect(reverse('messages-inbox'))
273 else:
274 try:
275 options = Options.objects.for_user(request.user)
276 except:
277 options = Options()
278 options.user = request.user
279 options.save()
280
281 form = OptionsForm(instance=options)
282
283 return render_to_response('messages/options.html', {
284 'form': form,
285 },
286 context_instance = RequestContext(request))
287
288
289 def ajax_users(request):
290 """
291 If the user is authenticated, return a string of usernames whose names start with
292 the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated
293 by newlines.
294 If the user is not authenticated, return an empty string.
295 This is used by the auto-complete function in the compose form.
296 """
297 q = request.GET.get('q', None)
298 if q is None:
299 return HttpResponseBadRequest()
300
301 if request.user.is_authenticated():
302 q = request.GET.get('q', ' ')
303 limit = int(request.GET.get('limit', 10))
304 users = User.objects.filter(username__istartswith=q).values_list('username', flat=True)[:limit]
305 user_list = u"\n".join(users)
306 return HttpResponse(user_list)
307 return HttpResponseForbidden()
308
309
310 # vim: ts=4 sw=4