Mercurial > public > sg101
comparison messages/views.py @ 804:95b3d59913ad
Private messages refactoring: compose functionality.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 31 Aug 2014 13:51:06 -0500 |
parents | b3eeaefc39a8 |
children | e966d5553955 |
comparison
equal
deleted
inserted
replaced
803:b3eeaefc39a8 | 804:95b3d59913ad |
---|---|
132 'form': form, | 132 'form': form, |
133 }) | 133 }) |
134 | 134 |
135 | 135 |
136 @login_required | 136 @login_required |
137 def compose_to(request, receiver): | 137 def compose(request): |
138 """ | 138 """ |
139 This function displays the base tabbed private messages view, | 139 Process or prepare the compose form to create a new private message. |
140 and configures it to display the compose PM tab for the given | 140 |
141 receiver. | 141 """ |
142 | 142 if request.method == 'POST': |
143 """ | 143 compose_form = ComposeForm(request.user, request.POST) |
144 user = get_object_or_404(User, username=receiver) | 144 if compose_form.is_valid(): |
145 tab_index = TAB_INDICES['compose'] | 145 compose_form.save() |
146 return render(request, 'messages/tabbed_base.html', { | 146 django_messages.success(request, 'Message sent.') |
147 'tab': tab_index, | 147 compose_form = ComposeForm(request.user) |
148 'receiver': receiver, | 148 else: |
149 'unread_count': Message.objects.unread_count(request.user), | 149 receiver = request.GET.get('to') |
150 }) | 150 if receiver: |
151 | 151 form_data = {'receiver': receiver} |
152 | 152 compose_form = ComposeForm(request.user, initial=form_data) |
153 def message(request): | 153 else: |
154 compose_form = ComposeForm(request.user) | |
155 | |
156 _quota_check('outbox', Message.objects.outbox(request.user).count(), request) | |
157 | |
158 return render(request, 'messages/compose.html', { | |
159 'tab': 'compose', | |
160 'compose_form': compose_form, | |
161 }) | |
162 | |
163 | |
164 @login_required | |
165 def view(request, msg_id): | |
154 """ | 166 """ |
155 This view function retrieves a message and returns it as a JSON object. | 167 This view function retrieves a message and returns it as a JSON object. |
156 | 168 |
157 """ | 169 """ |
158 if not request.user.is_authenticated(): | 170 if not request.user.is_authenticated(): |
176 re_subject=reply_subject(msg.subject), | 188 re_subject=reply_subject(msg.subject), |
177 re_content=quote_message(msg.sender.username, msg.message)) | 189 re_content=quote_message(msg.sender.username, msg.message)) |
178 | 190 |
179 result = json.dumps(msg_dict, ensure_ascii=False) | 191 result = json.dumps(msg_dict, ensure_ascii=False) |
180 return HttpResponse(result, content_type='application/json') | 192 return HttpResponse(result, content_type='application/json') |
181 | |
182 | |
183 def compose(request, receiver=None): | |
184 """ | |
185 Process or prepare the compose form to create a new private message. | |
186 | |
187 """ | |
188 if not request.user.is_authenticated(): | |
189 return HttpResponseForbidden() | |
190 | |
191 if request.method == "POST": | |
192 compose_form = ComposeForm(request.user, request.POST) | |
193 | |
194 # Is this a reply to another message? | |
195 parent_msg_id = request.POST.get('reply_to') | |
196 if parent_msg_id: | |
197 parent_msg = get_object_or_404(Message, id=parent_msg_id) | |
198 if (request.user != parent_msg.receiver and | |
199 request.user != parent_msg.sender): | |
200 return HttpResponseForbidden() | |
201 else: | |
202 parent_msg = None | |
203 | |
204 if compose_form.is_valid(): | |
205 compose_form.save(parent_msg=parent_msg) | |
206 django_messages.success(request, 'Message sent.') | |
207 compose_form = ComposeForm(request.user) | |
208 else: | |
209 if receiver is not None: | |
210 form_data = {'receiver': receiver} | |
211 compose_form = ComposeForm(request.user, initial=form_data) | |
212 else: | |
213 compose_form = ComposeForm(request.user) | |
214 | |
215 _quota_check('outbox', Message.objects.outbox(request.user).count(), request) | |
216 | |
217 return render(request, 'messages/compose_tab.html', { | |
218 'compose_form': compose_form, | |
219 }) | |
220 | 193 |
221 | 194 |
222 def _only_integers(slist): | 195 def _only_integers(slist): |
223 """ | 196 """ |
224 Accepts a list of strings. Returns a list of integers consisting of only | 197 Accepts a list of strings. Returns a list of integers consisting of only |