|
|
|
|
@ -1,18 +1,20 @@ |
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
import json |
|
|
|
|
from django.http import HttpResponseRedirect, HttpResponse |
|
|
|
|
from django.shortcuts import render_to_response, get_object_or_404 |
|
|
|
|
from django.views.generic import ListView, DetailView, TemplateView, FormView |
|
|
|
|
from accounts.models import User |
|
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
|
from django.views.generic import TemplateView, FormView |
|
|
|
|
from django.db.models import Q |
|
|
|
|
from django.utils import timezone |
|
|
|
|
from forms import ComposeForm, ReplyForm, SendForm |
|
|
|
|
from accounts.models import User |
|
|
|
|
from forms import ReplyForm, SendForm |
|
|
|
|
from models import Message |
|
|
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InboxView(TemplateView): |
|
|
|
|
template_name = 'accounts/messages.html' |
|
|
|
|
""" |
|
|
|
|
returns template with user messages |
|
|
|
|
""" |
|
|
|
|
template_name = 'client/accounts/messages.html' |
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs): |
|
|
|
|
context = super(InboxView, self).get_context_data(**kwargs) |
|
|
|
|
@ -20,51 +22,18 @@ class InboxView(TemplateView): |
|
|
|
|
context['senders'] = [{'sender': s, 'message': Message.objects.filter(recipient=self.request.user, sender=s)[0]}\ |
|
|
|
|
for s in senders] |
|
|
|
|
reply_form = ReplyForm() |
|
|
|
|
|
|
|
|
|
context['reply_form'] = reply_form |
|
|
|
|
return context |
|
|
|
|
|
|
|
|
|
def message_reply(request, message_id): |
|
|
|
|
response = {'success': False} |
|
|
|
|
if request.POST: |
|
|
|
|
|
|
|
|
|
form = ReplyForm(request.POST) |
|
|
|
|
if form.is_valid(): |
|
|
|
|
parent_msg =get_object_or_404(Message, id=message_id)# Message.objects.get(id=parent) |
|
|
|
|
form.save(sender=request.user, parent_msg=parent_msg) |
|
|
|
|
response['success'] = True |
|
|
|
|
return HttpResponse(json.dumps(response), content_type='application/json') |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
return HttpResponse('error') |
|
|
|
|
return HttpResponse('not ajax') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_message(request, url): |
|
|
|
|
response = {'success': False} |
|
|
|
|
try: |
|
|
|
|
url = int(url) |
|
|
|
|
recipient = get_object_or_404(User, id=url) |
|
|
|
|
except ValueError: |
|
|
|
|
|
|
|
|
|
recipient = get_object_or_404(User, url=url) |
|
|
|
|
if request.POST: |
|
|
|
|
form = SendForm(request.POST) |
|
|
|
|
if form.is_valid(): |
|
|
|
|
form.save(sender=request.user, recipient=recipient) |
|
|
|
|
response['success'] = True |
|
|
|
|
return HttpResponse(json.dumps(response), content_type='application/json') |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
return HttpResponse('error') |
|
|
|
|
return HttpResponse('not ajax') |
|
|
|
|
|
|
|
|
|
class MessageHistory(FormView): |
|
|
|
|
template_name = 'accounts/messages_history.html' |
|
|
|
|
""" |
|
|
|
|
returns message history with one user and form to reply to it |
|
|
|
|
""" |
|
|
|
|
template_name = 'client/accounts/messages_history.html' |
|
|
|
|
form_class = ReplyForm |
|
|
|
|
success_url = '' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_initial(self): |
|
|
|
|
return {'recipient':self.kwargs.get('user_id')} |
|
|
|
|
|
|
|
|
|
@ -84,13 +53,13 @@ class MessageHistory(FormView): |
|
|
|
|
def form_invalid(self, form): |
|
|
|
|
user_id = self.kwargs.get('user_id') |
|
|
|
|
return HttpResponseRedirect('/profile/messages/history/'+user_id+'/') |
|
|
|
|
|
|
|
|
|
def form_valid(self, form): |
|
|
|
|
form.save(sender=self.request.user) |
|
|
|
|
user_id = self.kwargs.get('user_id') |
|
|
|
|
return HttpResponseRedirect('/profile/messages/history/'+user_id+'/') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
class ReplyView(FormView): |
|
|
|
|
form_class = ComposeForm |
|
|
|
|
@ -108,3 +77,42 @@ class ReplyView(FormView): |
|
|
|
|
context['parent_msg'] = parent_msg |
|
|
|
|
return context |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def message_reply(request, message_id): |
|
|
|
|
""" |
|
|
|
|
reply to message |
|
|
|
|
""" |
|
|
|
|
response = {'success': False} |
|
|
|
|
if request.POST: |
|
|
|
|
form = ReplyForm(request.POST) |
|
|
|
|
if form.is_valid(): |
|
|
|
|
parent_msg = get_object_or_404(Message, id=message_id) |
|
|
|
|
form.save(sender=request.user, parent_msg=parent_msg) |
|
|
|
|
response['success'] = True |
|
|
|
|
return HttpResponse(json.dumps(response), content_type='application/json') |
|
|
|
|
else: |
|
|
|
|
return HttpResponse('error') |
|
|
|
|
return HttpResponse('not ajax') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_message(request, url): |
|
|
|
|
""" |
|
|
|
|
send message to user with url=url |
|
|
|
|
""" |
|
|
|
|
response = {'success': False} |
|
|
|
|
try: |
|
|
|
|
url = int(url) |
|
|
|
|
recipient = get_object_or_404(User, id=url) |
|
|
|
|
except ValueError: |
|
|
|
|
|
|
|
|
|
recipient = get_object_or_404(User, url=url) |
|
|
|
|
if request.POST: |
|
|
|
|
form = SendForm(request.POST) |
|
|
|
|
if form.is_valid(): |
|
|
|
|
form.save(sender=request.user, recipient=recipient) |
|
|
|
|
response['success'] = True |
|
|
|
|
return HttpResponse(json.dumps(response), content_type='application/json') |
|
|
|
|
else: |
|
|
|
|
return HttpResponse('error') |
|
|
|
|
return HttpResponse('not ajax') |