Sith/trombi/views.py

223 lines
8.2 KiB
Python
Raw Normal View History

2017-05-09 21:42:01 +00:00
# -*- coding:utf-8 -*
#
# Copyright 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-05-11 16:33:45 +00:00
from django.http import Http404
2017-05-09 21:42:01 +00:00
from django.shortcuts import render, get_object_or_404, redirect
from django.core.urlresolvers import reverse_lazy, reverse
2017-05-09 21:42:01 +00:00
from django.views.generic import ListView, DetailView, RedirectView, TemplateView
from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView, SingleObjectMixin
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.forms.models import modelform_factory
2017-05-09 21:42:01 +00:00
2017-05-11 16:33:45 +00:00
from datetime import date
2017-05-10 20:17:05 +00:00
from trombi.models import Trombi, TrombiUser, TrombiComment
2017-05-09 21:42:01 +00:00
from core.views.forms import SelectFile, SelectDate
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, CanCreateMixin, QuickNotifMixin
from core.models import User
from club.models import Club
2017-05-10 20:17:05 +00:00
class TrombiForm(forms.ModelForm):
2017-05-09 21:42:01 +00:00
class Meta:
2017-05-10 20:17:05 +00:00
model = Trombi
2017-05-12 08:25:26 +00:00
fields = ['subscription_deadline', 'comments_deadline', 'max_chars', 'show_profiles']
2017-05-09 21:42:01 +00:00
widgets = {
'subscription_deadline': SelectDate,
'comments_deadline': SelectDate,
}
2017-05-10 20:17:05 +00:00
class TrombiCreateView(CanEditPropMixin, CreateView):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
Create a trombi for a club
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
model = Trombi
form_class = TrombiForm
2017-05-09 21:42:01 +00:00
template_name = 'core/create.jinja'
def post(self, request, *args, **kwargs):
"""
Affect club
"""
form = self.get_form()
if form.is_valid():
club = get_object_or_404(Club, id=self.kwargs['club_id'])
form.instance.club = club
ret = self.form_valid(form)
return ret
else:
return self.form_invalid(form)
2017-05-10 20:17:05 +00:00
class TrombiEditView(CanEditPropMixin, UpdateView):
model = Trombi
form_class = TrombiForm
2017-05-09 21:42:01 +00:00
template_name = 'core/edit.jinja'
2017-05-10 20:17:05 +00:00
pk_url_kwarg = 'trombi_id'
2017-05-09 21:42:01 +00:00
def get_success_url(self):
return super(TrombiEditView, self).get_success_url()+"?qn_success"
class TrombiDetailView(CanEditMixin, QuickNotifMixin, DetailView):
2017-05-10 20:17:05 +00:00
model = Trombi
template_name = 'trombi/detail.jinja'
pk_url_kwarg = 'trombi_id'
2017-05-09 21:42:01 +00:00
2017-05-10 20:17:05 +00:00
class TrombiDeleteUserView(CanEditPropMixin, SingleObjectMixin, RedirectView):
model = Trombi
pk_url_kwarg = 'trombi_id'
2017-05-09 21:42:01 +00:00
permanent = False
def get(self, request, *args, **kwargs):
self.object = self.get_object()
2017-05-10 20:17:05 +00:00
user = get_object_or_404(TrombiUser, id=self.kwargs['user_id'])
2017-05-09 21:42:01 +00:00
user.delete()
# See if we need to also delete the comments on the user, or if we keep them
return redirect(self.object.get_absolute_url()+"?qn_success")
2017-05-09 21:42:01 +00:00
# User side
2017-05-11 16:33:45 +00:00
class TrombiModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return _("%(name)s (deadline: %(date)s)") % {'name': str(obj), 'date': str(obj.subscription_deadline)}
2017-05-10 20:17:05 +00:00
class UserTrombiForm(forms.Form):
2017-05-11 16:33:45 +00:00
trombi = TrombiModelChoiceField(Trombi.availables.all(), required=False, label=_("Select trombi"),
2017-05-10 20:17:05 +00:00
help_text=_("This allows you to subscribe to a Trombi. "
2017-05-09 21:42:01 +00:00
"Be aware that you can subscribe only once, so don't play with that, "
"or you will expose yourself to the admins' wrath!"))
2017-05-10 20:17:05 +00:00
class UserTrombiToolsView(QuickNotifMixin, TemplateView):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
Display a user's trombi tools
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
template_name = "trombi/user_tools.jinja"
2017-05-09 21:42:01 +00:00
def post(self, request, *args, **kwargs):
2017-05-10 20:17:05 +00:00
self.form = UserTrombiForm(request.POST)
2017-05-09 21:42:01 +00:00
if self.form.is_valid():
2017-05-10 20:17:05 +00:00
trombi_user = TrombiUser(user=request.user,
trombi=self.form.cleaned_data['trombi'])
trombi_user.save()
2017-05-09 21:42:01 +00:00
self.quick_notif_list += ['qn_success']
2017-05-10 20:17:05 +00:00
return super(UserTrombiToolsView, self).get(request, *args, **kwargs)
2017-05-09 21:42:01 +00:00
def get_context_data(self, **kwargs):
2017-05-10 20:17:05 +00:00
kwargs = super(UserTrombiToolsView, self).get_context_data(**kwargs)
2017-05-09 21:42:01 +00:00
kwargs['user'] = self.request.user
2017-05-10 20:17:05 +00:00
if not hasattr(self.request.user, 'trombi_user'):
kwargs['subscribe_form'] = UserTrombiForm()
2017-05-11 16:33:45 +00:00
else:
kwargs['trombi'] = self.request.user.trombi_user.trombi
kwargs['date'] = date
2017-05-09 21:42:01 +00:00
return kwargs
class UserTrombiEditPicturesView(UpdateView):
model = TrombiUser
fields = ['profile_pict', 'scrub_pict']
template_name = "core/edit.jinja"
def get_object(self):
return self.request.user.trombi_user
def get_success_url(self):
return reverse('trombi:user_tools')+"?qn_success"
2017-05-10 20:17:05 +00:00
class UserTrombiEditProfileView(UpdateView):
2017-05-10 19:59:22 +00:00
model = User
form_class = modelform_factory(User,
fields=['second_email', 'phone', 'department', 'dpt_option',
'quote', 'parent_address'],
labels={
'second_email': _("Personal email (not UTBM)"),
'phone': _("Phone"),
'parent_address': _("Native town"),
})
template_name = "core/edit.jinja"
def get_object(self):
return self.request.user
def get_success_url(self):
return reverse('trombi:user_tools')+"?qn_success"
2017-05-12 08:25:26 +00:00
class UserTrombiProfileView(DetailView):
model = TrombiUser
pk_url_kwarg = "user_id"
template_name = "trombi/user_profile.jinja"
context_object_name = "trombi_user"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if (self.object.trombi.id != request.user.trombi_user.trombi.id or
self.object.user.id == request.user.id or
not self.object.trombi.show_profiles):
raise Http404()
return super(UserTrombiProfileView, self).get(request, *args, **kwargs)
2017-05-10 20:17:05 +00:00
class TrombiCommentFormView():
"""
2017-05-10 20:17:05 +00:00
Create/edit a trombi comment
"""
2017-05-10 20:17:05 +00:00
model = TrombiComment
fields = ['content']
template_name = 'trombi/comment.jinja'
def get_form_class(self):
2017-05-10 20:17:05 +00:00
self.trombi = self.request.user.trombi_user.trombi
2017-05-11 16:33:45 +00:00
if date.today() <= self.trombi.subscription_deadline:
raise Http404(_("You can not yet write comment, you must wait for "
"the subscription deadline to be passed."))
if self.trombi.comments_deadline < date.today():
raise Http404(_("You can not write comment anymore, the deadline is "
"already passed."))
return modelform_factory(self.model, fields=self.fields,
widgets={
2017-05-10 20:17:05 +00:00
'content': forms.widgets.Textarea(attrs={'maxlength': self.trombi.max_chars})
},
help_texts={
2017-05-10 20:17:05 +00:00
'content': _("Maximum characters: %(max_length)s") % {'max_length': self.trombi.max_chars}
})
def get_success_url(self):
return reverse('trombi:user_tools')+"?qn_success"
def get_context_data(self, **kwargs):
kwargs = super(TrombiCommentFormView, self).get_context_data(**kwargs)
if 'user_id' in self.kwargs.keys():
kwargs['target'] = get_object_or_404(TrombiUser, id=self.kwargs['user_id'])
else:
kwargs['target'] = self.object.target
return kwargs
class TrombiCommentCreateView(TrombiCommentFormView, CreateView):
def form_valid(self, form):
2017-05-10 20:17:05 +00:00
target = get_object_or_404(TrombiUser, id=self.kwargs['user_id'])
form.instance.author = self.request.user.trombi_user
form.instance.target = target
2017-05-10 20:17:05 +00:00
return super(TrombiCommentCreateView, self).form_valid(form)
2017-05-10 20:17:05 +00:00
class TrombiCommentEditView(TrombiCommentFormView, CanViewMixin, UpdateView):
pk_url_kwarg = "comment_id"