2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-01-21 02:42:06 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.views.generic import ListView, DetailView, RedirectView
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView, DeleteView
|
2017-02-24 00:50:05 +00:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2017-01-21 02:42:06 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.core.urlresolvers import reverse, reverse_lazy
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.conf import settings
|
|
|
|
from django import forms
|
2017-01-21 11:28:32 +00:00
|
|
|
from django.db import models
|
2017-01-21 02:42:06 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2017-03-15 17:34:22 +00:00
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
2017-01-21 02:42:06 +00:00
|
|
|
|
2017-02-24 03:37:01 +00:00
|
|
|
from ajax_select import make_ajax_form, make_ajax_field
|
2017-02-24 00:50:09 +00:00
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin
|
2017-02-24 00:50:05 +00:00
|
|
|
from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta
|
2017-01-21 02:42:06 +00:00
|
|
|
|
2017-02-24 00:50:00 +00:00
|
|
|
class ForumMainView(ListView):
|
2017-05-30 20:10:50 +00:00
|
|
|
queryset = Forum.objects.filter(parent=None).prefetch_related("children___last_message__author", "children___last_message__topic")
|
2017-01-21 02:42:06 +00:00
|
|
|
template_name = "forum/main.jinja"
|
|
|
|
|
2017-01-28 23:16:41 +00:00
|
|
|
class ForumMarkAllAsRead(RedirectView):
|
|
|
|
permanent = False
|
|
|
|
url = reverse_lazy('forum:last_unread')
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
fi = request.user.forum_infos
|
|
|
|
fi.last_read_date = timezone.now()
|
|
|
|
fi.save()
|
2017-05-20 10:47:48 +00:00
|
|
|
for m in request.user.read_messages.filter(date__lt=fi.last_read_date):
|
|
|
|
m.readers.remove(request.user) # Clean up to keep table low in data
|
2017-01-28 23:16:41 +00:00
|
|
|
except: pass
|
|
|
|
return super(ForumMarkAllAsRead, self).get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
class ForumLastUnread(ListView):
|
|
|
|
model = ForumTopic
|
|
|
|
template_name = "forum/last_unread.jinja"
|
2017-05-20 10:40:30 +00:00
|
|
|
paginate_by = settings.SITH_FORUM_PAGE_LENGTH / 2
|
2017-01-28 23:16:41 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2017-05-20 10:47:48 +00:00
|
|
|
topic_list = self.model.objects.filter(_last_message__date__gt=self.request.user.forum_infos.last_read_date)\
|
|
|
|
.exclude(_last_message__readers=self.request.user)\
|
|
|
|
.order_by('-_last_message__date')\
|
2017-05-30 22:41:39 +00:00
|
|
|
.select_related('_last_message__author', 'author')\
|
|
|
|
.prefetch_related('forum__edit_groups')
|
2017-05-20 10:47:48 +00:00
|
|
|
return topic_list
|
2017-01-28 23:16:41 +00:00
|
|
|
|
2017-02-24 03:37:01 +00:00
|
|
|
class ForumForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Forum
|
2017-03-12 17:06:01 +00:00
|
|
|
fields = ['name', 'parent', 'number', 'owner_club', 'is_category', 'edit_groups', 'view_groups']
|
2017-02-24 03:37:01 +00:00
|
|
|
edit_groups = make_ajax_field(Forum, 'edit_groups', 'groups', help_text="")
|
|
|
|
view_groups = make_ajax_field(Forum, 'view_groups', 'groups', help_text="")
|
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumCreateView(CanCreateMixin, CreateView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = Forum
|
2017-02-24 03:37:01 +00:00
|
|
|
form_class = ForumForm
|
2017-01-21 02:42:06 +00:00
|
|
|
template_name = "core/create.jinja"
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
init = super(ForumCreateView, self).get_initial()
|
2017-01-21 21:47:30 +00:00
|
|
|
try:
|
|
|
|
parent = Forum.objects.filter(id=self.request.GET['parent']).first()
|
|
|
|
init['parent'] = parent
|
2017-01-28 19:58:54 +00:00
|
|
|
init['owner_club'] = parent.owner_club
|
2017-02-24 14:55:50 +00:00
|
|
|
init['edit_groups'] = parent.edit_groups.all()
|
|
|
|
init['view_groups'] = parent.view_groups.all()
|
2017-01-21 21:47:30 +00:00
|
|
|
except: pass
|
2017-01-21 02:42:06 +00:00
|
|
|
return init
|
|
|
|
|
2017-02-24 03:37:01 +00:00
|
|
|
class ForumEditForm(ForumForm):
|
2017-01-28 19:58:54 +00:00
|
|
|
recursive = forms.BooleanField(label=_("Apply rights and club owner recursively"), required=False)
|
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumEditView(CanEditPropMixin, UpdateView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = Forum
|
|
|
|
pk_url_kwarg = "forum_id"
|
2017-01-28 19:58:54 +00:00
|
|
|
form_class = ForumEditForm
|
2017-01-21 02:42:06 +00:00
|
|
|
template_name = "core/edit.jinja"
|
|
|
|
success_url = reverse_lazy('forum:main')
|
|
|
|
|
2017-01-28 19:58:54 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
ret = super(ForumEditView, self).form_valid(form)
|
|
|
|
if form.cleaned_data['recursive']:
|
|
|
|
self.object.apply_rights_recursively()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
class ForumDeleteView(CanEditPropMixin, DeleteView):
|
|
|
|
model = Forum
|
|
|
|
pk_url_kwarg = "forum_id"
|
|
|
|
template_name = "core/delete_confirm.jinja"
|
|
|
|
success_url = reverse_lazy('forum:main')
|
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumDetailView(CanViewMixin, DetailView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = Forum
|
|
|
|
template_name = "forum/forum.jinja"
|
|
|
|
pk_url_kwarg = "forum_id"
|
|
|
|
|
2017-01-21 11:28:32 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ForumDetailView, self).get_context_data(**kwargs)
|
2017-05-30 22:41:39 +00:00
|
|
|
qs = self.object.topics.order_by('-_last_message__date')\
|
|
|
|
.select_related('_last_message__author', 'author')\
|
2017-05-30 20:10:50 +00:00
|
|
|
.prefetch_related("forum__edit_groups")
|
2017-05-20 10:40:30 +00:00
|
|
|
paginator = Paginator(qs,
|
|
|
|
settings.SITH_FORUM_PAGE_LENGTH)
|
|
|
|
page = self.request.GET.get('topic_page')
|
|
|
|
try:
|
|
|
|
kwargs["topics"] = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
kwargs["topics"] = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
kwargs["topics"] = paginator.page(paginator.num_pages)
|
2017-01-21 11:28:32 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-02-24 14:55:50 +00:00
|
|
|
class TopicForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = ForumMessage
|
|
|
|
fields = ['title', 'message']
|
2017-04-12 17:19:25 +00:00
|
|
|
title = forms.CharField(required=True, label=_("Title"))
|
2017-02-24 14:55:50 +00:00
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumTopicCreateView(CanCreateMixin, CreateView):
|
2017-01-21 03:19:15 +00:00
|
|
|
model = ForumMessage
|
2017-02-24 14:55:50 +00:00
|
|
|
form_class = TopicForm
|
2017-04-12 17:19:25 +00:00
|
|
|
template_name = "forum/reply.jinja"
|
2017-01-21 02:42:06 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.forum = get_object_or_404(Forum, id=self.kwargs['forum_id'], is_category=False)
|
|
|
|
if not request.user.can_view(self.forum):
|
|
|
|
raise PermissionDenied
|
|
|
|
return super(ForumTopicCreateView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2017-06-02 08:55:42 +00:00
|
|
|
topic = ForumTopic(_title=form.instance.title, author=self.request.user, forum=self.forum)
|
2017-01-21 03:19:15 +00:00
|
|
|
topic.save()
|
|
|
|
form.instance.topic = topic
|
2017-01-21 02:42:06 +00:00
|
|
|
form.instance.author = self.request.user
|
|
|
|
return super(ForumTopicCreateView, self).form_valid(form)
|
|
|
|
|
2017-01-21 21:47:30 +00:00
|
|
|
class ForumTopicEditView(CanEditMixin, UpdateView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = ForumTopic
|
2017-02-24 00:50:00 +00:00
|
|
|
fields = ['forum']
|
2017-01-21 02:42:06 +00:00
|
|
|
pk_url_kwarg = "topic_id"
|
|
|
|
template_name = "core/edit.jinja"
|
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumTopicDetailView(CanViewMixin, DetailView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = ForumTopic
|
|
|
|
pk_url_kwarg = "topic_id"
|
|
|
|
template_name = "forum/topic.jinja"
|
|
|
|
context_object_name = "topic"
|
2017-02-24 00:50:09 +00:00
|
|
|
queryset = ForumTopic.objects.select_related('forum__parent')
|
2017-01-21 02:42:06 +00:00
|
|
|
|
2017-01-28 23:16:41 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ForumTopicDetailView, self).get_context_data(**kwargs)
|
|
|
|
try:
|
2017-03-12 19:24:16 +00:00
|
|
|
msg = self.object.get_first_unread_message(user)
|
2017-01-28 23:16:41 +00:00
|
|
|
kwargs['first_unread_message_id'] = msg.id
|
|
|
|
except:
|
2017-02-24 03:37:01 +00:00
|
|
|
kwargs['first_unread_message_id'] = float("inf")
|
2017-05-30 20:10:50 +00:00
|
|
|
paginator = Paginator(self.object.messages.select_related('author__avatar_pict')\
|
|
|
|
.prefetch_related('topic__forum__edit_groups', 'readers').order_by('date'),
|
2017-03-15 17:34:22 +00:00
|
|
|
settings.SITH_FORUM_PAGE_LENGTH)
|
|
|
|
page = self.request.GET.get('page')
|
2017-03-12 19:24:16 +00:00
|
|
|
try:
|
2017-03-15 17:34:22 +00:00
|
|
|
kwargs["msgs"] = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
kwargs["msgs"] = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
kwargs["msgs"] = paginator.page(paginator.num_pages)
|
2017-01-28 23:16:41 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-05-20 10:47:48 +00:00
|
|
|
class ForumMessageView(SingleObjectMixin, RedirectView):
|
|
|
|
model = ForumMessage
|
|
|
|
pk_url_kwarg = "message_id"
|
|
|
|
permanent = False
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
return self.object.get_url()
|
|
|
|
|
2017-01-21 11:28:32 +00:00
|
|
|
class ForumMessageEditView(CanEditMixin, UpdateView):
|
|
|
|
model = ForumMessage
|
|
|
|
fields = ['title', 'message']
|
2017-02-25 13:37:45 +00:00
|
|
|
template_name = "forum/reply.jinja"
|
2017-01-21 11:28:32 +00:00
|
|
|
pk_url_kwarg = "message_id"
|
|
|
|
|
2017-02-24 00:50:05 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
ForumMessageMeta(message=self.object, user=self.request.user, action="EDIT").save()
|
|
|
|
return super(ForumMessageEditView, self).form_valid(form)
|
|
|
|
|
2017-02-25 13:37:45 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ForumMessageEditView, self).get_context_data(**kwargs)
|
|
|
|
kwargs['topic'] = self.object.topic
|
|
|
|
return kwargs
|
|
|
|
|
2017-02-24 00:50:05 +00:00
|
|
|
class ForumMessageDeleteView(SingleObjectMixin, RedirectView):
|
|
|
|
model = ForumMessage
|
|
|
|
pk_url_kwarg = "message_id"
|
|
|
|
permanent = False
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2017-02-24 00:50:09 +00:00
|
|
|
self.object = self.get_object()
|
2017-02-24 00:50:05 +00:00
|
|
|
if self.object.can_be_moderated_by(self.request.user):
|
|
|
|
ForumMessageMeta(message=self.object, user=self.request.user, action="DELETE").save()
|
|
|
|
return self.object.get_absolute_url()
|
|
|
|
|
|
|
|
class ForumMessageUndeleteView(SingleObjectMixin, RedirectView):
|
|
|
|
model = ForumMessage
|
|
|
|
pk_url_kwarg = "message_id"
|
|
|
|
permanent = False
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2017-02-24 00:50:09 +00:00
|
|
|
self.object = self.get_object()
|
2017-02-24 00:50:05 +00:00
|
|
|
if self.object.can_be_moderated_by(self.request.user):
|
|
|
|
ForumMessageMeta(message=self.object, user=self.request.user, action="UNDELETE").save()
|
|
|
|
return self.object.get_absolute_url()
|
|
|
|
|
2017-01-21 03:51:37 +00:00
|
|
|
class ForumMessageCreateView(CanCreateMixin, CreateView):
|
2017-01-21 02:42:06 +00:00
|
|
|
model = ForumMessage
|
|
|
|
fields = ['title', 'message']
|
2017-02-24 16:22:13 +00:00
|
|
|
template_name = "forum/reply.jinja"
|
2017-01-21 02:42:06 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.topic = get_object_or_404(ForumTopic, id=self.kwargs['topic_id'])
|
|
|
|
if not request.user.can_view(self.topic):
|
|
|
|
raise PermissionDenied
|
|
|
|
return super(ForumMessageCreateView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
init = super(ForumMessageCreateView, self).get_initial()
|
|
|
|
try:
|
2017-02-24 00:50:09 +00:00
|
|
|
message = ForumMessage.objects.select_related('author').filter(id=self.request.GET['quote_id']).first()
|
|
|
|
init['message'] = "> ##### %s\n" % (_("%(author)s said") % {'author': message.author.get_short_name()})
|
|
|
|
init['message'] += "\n".join([
|
|
|
|
"> " + line for line in message.message.split('\n')
|
2017-01-21 02:42:06 +00:00
|
|
|
])
|
2017-02-24 00:50:09 +00:00
|
|
|
init['message'] += "\n\n"
|
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
2017-01-21 02:42:06 +00:00
|
|
|
return init
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.topic = self.topic
|
|
|
|
form.instance.author = self.request.user
|
|
|
|
return super(ForumMessageCreateView, self).form_valid(form)
|
|
|
|
|
2017-02-24 16:22:13 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ForumMessageCreateView, self).get_context_data(**kwargs)
|
|
|
|
kwargs['topic'] = self.topic
|
|
|
|
return kwargs
|
2017-02-24 00:50:09 +00:00
|
|
|
|