Mercurial > public > sg101
comparison messages/views.py @ 803:b3eeaefc39a8
Private message refactoring: add options page.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 31 Aug 2014 12:46:21 -0500 |
parents | dbc389a409f5 |
children | 95b3d59913ad |
comparison
equal
deleted
inserted
replaced
802:dbc389a409f5 | 803:b3eeaefc39a8 |
---|---|
110 'outbox_pct': None, | 110 'outbox_pct': None, |
111 }) | 111 }) |
112 | 112 |
113 | 113 |
114 @login_required | 114 @login_required |
115 def compose_to(request, receiver): | |
116 """ | |
117 This function displays the base tabbed private messages view, | |
118 and configures it to display the compose PM tab for the given | |
119 receiver. | |
120 | |
121 """ | |
122 user = get_object_or_404(User, username=receiver) | |
123 tab_index = TAB_INDICES['compose'] | |
124 return render(request, 'messages/tabbed_base.html', { | |
125 'tab': tab_index, | |
126 'receiver': receiver, | |
127 'unread_count': Message.objects.unread_count(request.user), | |
128 }) | |
129 | |
130 | |
131 def message(request): | |
132 """ | |
133 This view function retrieves a message and returns it as a JSON object. | |
134 | |
135 """ | |
136 if not request.user.is_authenticated(): | |
137 return HttpResponseForbidden() | |
138 if request.method != 'POST': | |
139 return HttpResponseNotAllowed(['POST']) | |
140 | |
141 msg_id = request.POST.get('msg_id') | |
142 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) | |
143 if msg.sender != request.user and msg.receiver != request.user: | |
144 return HttpResponseForbidden() | |
145 | |
146 if msg.receiver == request.user and msg.read_date is None: | |
147 msg.read_date = datetime.datetime.now() | |
148 msg.save() | |
149 | |
150 msg_dict = dict(subject=msg.subject, | |
151 sender=msg.sender.username, | |
152 receiver=msg.receiver.username, | |
153 content=msg.html, | |
154 re_subject=reply_subject(msg.subject), | |
155 re_content=quote_message(msg.sender.username, msg.message)) | |
156 | |
157 result = json.dumps(msg_dict, ensure_ascii=False) | |
158 return HttpResponse(result, content_type='application/json') | |
159 | |
160 | |
161 def options(request): | 115 def options(request): |
162 """ | 116 """ |
163 This view handles the displaying and changing of private message options. | 117 This view handles the displaying and changing of private message options. |
164 | 118 |
165 """ | 119 """ |
166 if not request.user.is_authenticated(): | 120 if request.method == 'POST': |
167 return HttpResponseForbidden() | |
168 | |
169 if request.method == "POST": | |
170 options = Options.objects.for_user(request.user) | 121 options = Options.objects.for_user(request.user) |
171 form = OptionsForm(request.POST, instance=options, prefix='opts') | 122 form = OptionsForm(request.POST, instance=options, prefix='opts') |
172 if form.is_valid(): | 123 if form.is_valid(): |
173 form.save() | 124 form.save() |
174 django_messages.success(request, 'Options saved.') | 125 django_messages.success(request, 'Options saved.') |
175 else: | 126 else: |
176 options = Options.objects.for_user(request.user) | 127 options = Options.objects.for_user(request.user) |
177 form = OptionsForm(instance=options, prefix='opts') | 128 form = OptionsForm(instance=options, prefix='opts') |
178 | 129 |
179 return render(request, 'messages/options_tab.html', { | 130 return render(request, 'messages/options.html', { |
131 'tab': 'options', | |
180 'form': form, | 132 'form': form, |
181 }) | 133 }) |
134 | |
135 | |
136 @login_required | |
137 def compose_to(request, receiver): | |
138 """ | |
139 This function displays the base tabbed private messages view, | |
140 and configures it to display the compose PM tab for the given | |
141 receiver. | |
142 | |
143 """ | |
144 user = get_object_or_404(User, username=receiver) | |
145 tab_index = TAB_INDICES['compose'] | |
146 return render(request, 'messages/tabbed_base.html', { | |
147 'tab': tab_index, | |
148 'receiver': receiver, | |
149 'unread_count': Message.objects.unread_count(request.user), | |
150 }) | |
151 | |
152 | |
153 def message(request): | |
154 """ | |
155 This view function retrieves a message and returns it as a JSON object. | |
156 | |
157 """ | |
158 if not request.user.is_authenticated(): | |
159 return HttpResponseForbidden() | |
160 if request.method != 'POST': | |
161 return HttpResponseNotAllowed(['POST']) | |
162 | |
163 msg_id = request.POST.get('msg_id') | |
164 msg = get_object_or_404(Message.objects.select_related(), pk=msg_id) | |
165 if msg.sender != request.user and msg.receiver != request.user: | |
166 return HttpResponseForbidden() | |
167 | |
168 if msg.receiver == request.user and msg.read_date is None: | |
169 msg.read_date = datetime.datetime.now() | |
170 msg.save() | |
171 | |
172 msg_dict = dict(subject=msg.subject, | |
173 sender=msg.sender.username, | |
174 receiver=msg.receiver.username, | |
175 content=msg.html, | |
176 re_subject=reply_subject(msg.subject), | |
177 re_content=quote_message(msg.sender.username, msg.message)) | |
178 | |
179 result = json.dumps(msg_dict, ensure_ascii=False) | |
180 return HttpResponse(result, content_type='application/json') | |
182 | 181 |
183 | 182 |
184 def compose(request, receiver=None): | 183 def compose(request, receiver=None): |
185 """ | 184 """ |
186 Process or prepare the compose form to create a new private message. | 185 Process or prepare the compose form to create a new private message. |