def topic_read(request, topic_id, template_name='private_messages/topic_read.html'): topic = get_object_or_404(Topic.objects.by_user(request.user), id=topic_id) if request.method == 'POST': form = MessageSendForm(data=request.POST) if form.is_valid(): message = form.save(commit=False) message.topic = topic message.sender = request.user message.save() topic.last_sent_at = message.sent_at topic.save() return HttpResponseRedirect(message.get_absolute_url()) else: form = MessageSendForm() # помечаем сообщения как прочитанные Message.objects.mark_read(request.user, topic) return TemplateResponse(request, template_name, { 'pm_topic': topic, 'pm_form': form, })
def send(request): ''' ''' form = MessageSendForm() from_user = request.user to_user = None contact = None if request.method == 'GET': if 'to' in request.GET and request.GET['to']: username = request.GET['to'] try: to_user = User.objects.get(username__iexact=username) except: pass else: if to_user == from_user: return HttpResponseRedirect(reverse('message_inbox')) try: contact = from_user.contact_list.filter(to_user=to_user)[0] except: contact = None elif request.method == "POST": form = MessageSendForm(request.POST) if form.is_valid(): data = form.cleaned_data try: to_user = User.objects.get(username__iexact=data['send_to']) except: messages.error(request, u'用户不存在') else: if to_user == from_user: messages.error(request, u'不可以给自己发送私信') to_user = None else: message = data['message'] send_message(from_user, to_user, message) contact = from_user.contact_list.filter(to_user=to_user)[0] return HttpResponseRedirect( reverse(conversation, kwargs={'contact_id': contact.id})) else: messages.error(request, u'发送失败') try: from_url = request.META['HTTP_REFERER'] return HttpResponseRedirect(from_url) except KeyError: pass else: pass try: from_url = request.META['HTTP_REFERER'] except: from_url = "/" return render_to_response('messages/send.html', { 'form': form, 'to_user': to_user, 'from_url': from_url, 'contact': contact, }, context_instance=RequestContext(request))