PonyConf/conversations/views.py

101 lines
3.3 KiB
Python
Raw Normal View History

2016-06-14 19:39:04 +00:00
from django.contrib import messages
from django.contrib.auth.decorators import login_required
2016-06-14 21:58:04 +00:00
from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import PermissionDenied
2016-06-14 21:58:04 +00:00
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render
2016-11-12 12:56:21 +00:00
from django.utils.translation import ugettext_lazy as _
2016-06-12 21:39:04 +00:00
2016-07-11 19:33:34 +00:00
from accounts.decorators import staff_required
2016-06-14 19:39:04 +00:00
from accounts.models import Participation
from proposals.models import Talk
2016-06-14 19:39:04 +00:00
from .forms import MessageForm
2016-06-12 21:39:04 +00:00
2016-06-14 19:39:04 +00:00
@login_required
2016-07-07 11:52:25 +00:00
def user_conversation(request, username=None):
2016-06-14 19:39:04 +00:00
2016-06-14 21:58:04 +00:00
if username:
2016-07-12 16:05:15 +00:00
p = Participation.objects.get(user=request.user, site=get_current_site(request))
2016-07-07 11:52:25 +00:00
if not p.is_staff() and not p.is_orga():
2016-06-14 21:58:04 +00:00
raise PermissionDenied()
user = get_object_or_404(User, username=username)
template = 'conversations/conversation.html'
else:
user = request.user
template = 'conversations/inbox.html'
participation = get_object_or_404(Participation, user=user, site=get_current_site(request))
2016-06-14 19:39:04 +00:00
conversation = participation.conversation
message_list = conversation.messages.all()
form = MessageForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
form.instance.conversation = conversation
form.instance.author = request.user
form.save()
2016-11-12 12:56:21 +00:00
messages.success(request, _('Message sent!'))
2016-06-14 21:58:04 +00:00
if username:
2016-11-12 12:56:21 +00:00
return redirect(reverse('user-conversation', args=[username]))
2016-06-14 21:58:04 +00:00
else:
return redirect('inbox')
2016-06-14 19:39:04 +00:00
2016-06-14 21:58:04 +00:00
return render(request, template, {
2016-11-12 12:56:21 +00:00
'correspondent': user,
2016-06-14 19:39:04 +00:00
'message_list': message_list,
'form': form,
2016-06-12 21:39:04 +00:00
})
2016-06-14 21:58:04 +00:00
@login_required
def talk_conversation(request, talk):
talk = get_object_or_404(Talk, slug=talk)
form = MessageForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
form.instance.conversation = talk.conversation
form.instance.author = request.user
form.save()
messages.success(request, 'Message sent!')
return redirect(talk.get_absolute_url())
2016-11-12 12:56:21 +00:00
@staff_required
def correspondent_list(request):
2016-06-14 21:58:04 +00:00
2016-07-11 19:33:34 +00:00
correspondent_list = Participation.objects.filter(site=get_current_site(request),
conversation__subscribers=request.user)
2016-06-14 21:58:04 +00:00
2016-11-12 12:56:21 +00:00
return render(request, 'conversations/correspondent_list.html', {
2016-06-14 21:58:04 +00:00
'correspondent_list': correspondent_list,
})
@staff_required
2016-06-14 21:58:04 +00:00
def subscribe(request, username):
participation = get_object_or_404(Participation, user__username=username, site=get_current_site(request))
2016-06-14 21:58:04 +00:00
participation.conversation.subscribers.add(request.user)
messages.success(request, 'Subscribed.')
2016-11-12 12:56:21 +00:00
next_url = request.GET.get('next') or reverse('user-conversation', args=[username])
2016-06-14 21:58:04 +00:00
return redirect(next_url)
@staff_required
2016-06-14 21:58:04 +00:00
def unsubscribe(request, username):
participation = get_object_or_404(Participation, user__username=username, site=get_current_site(request))
2016-06-14 21:58:04 +00:00
participation.conversation.subscribers.remove(request.user)
messages.success(request, 'Unsubscribed.')
2016-11-12 12:56:21 +00:00
next_url = request.GET.get('next') or reverse('user-conversation', args=[username])
2016-06-14 21:58:04 +00:00
return redirect(next_url)