Sith/club/views.py

517 lines
19 KiB
Python
Raw Normal View History

# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
2017-08-16 22:07:19 +00:00
# - Sli <antoine@bartuccio.fr>
#
# 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.
#
#
2016-02-04 07:59:03 +00:00
from django import forms
2016-11-25 14:26:45 +00:00
from django.views.generic import ListView, DetailView, TemplateView
2017-08-19 14:13:53 +00:00
from django.views.generic.edit import DeleteView
2017-08-16 22:07:19 +00:00
from django.views.generic.detail import SingleObjectMixin
2016-02-04 07:59:03 +00:00
from django.views.generic.edit import UpdateView, CreateView
2016-11-07 21:05:09 +00:00
from django.http import HttpResponseRedirect, HttpResponse
2017-08-16 22:07:19 +00:00
from django.core.urlresolvers import reverse, reverse_lazy
2016-09-02 19:21:57 +00:00
from django.utils import timezone
2016-10-26 18:01:05 +00:00
from django.utils.translation import ugettext_lazy as _
2016-11-07 21:05:09 +00:00
from django.utils.translation import ugettext as _t
2016-09-27 19:05:57 +00:00
from ajax_select.fields import AutoCompleteSelectField
2017-08-17 18:55:20 +00:00
from django.core.exceptions import PermissionDenied
2017-08-16 22:07:19 +00:00
from django.shortcuts import get_object_or_404
from django.core.validators import RegexValidator, validate_email
2016-09-08 01:29:49 +00:00
2017-08-19 14:13:53 +00:00
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin
2017-06-12 06:54:48 +00:00
from core.views.forms import SelectDate, SelectDateTime
2017-08-16 22:07:19 +00:00
from club.models import Club, Membership, Mailing, MailingSubscription
2017-06-12 06:54:48 +00:00
from sith.settings import SITH_MAXIMUM_FREE_ROLE
from counter.models import Selling, Counter
2017-08-21 17:53:17 +00:00
from core.models import User
2017-06-12 06:54:48 +00:00
2017-08-17 19:46:13 +00:00
from django.conf import settings
2017-08-16 22:07:19 +00:00
# Custom forms
class MailingForm(forms.ModelForm):
class Meta:
model = Mailing
2017-08-21 17:53:17 +00:00
fields = ('email', 'club', 'moderator')
2017-08-16 22:07:19 +00:00
email = forms.CharField(
label=_('Email address'),
validators=[
RegexValidator(
validate_email.user_regex,
_('Enter a valid address. Only the root of the address is needed.')
)
],
required=True)
2017-08-16 22:07:19 +00:00
def __init__(self, *args, **kwargs):
club_id = kwargs.pop('club_id', None)
2017-08-21 17:53:17 +00:00
user_id = kwargs.pop('user_id', -1) # Remember 0 is treated as None
2017-08-16 22:07:19 +00:00
super(MailingForm, self).__init__(*args, **kwargs)
if club_id:
self.fields['club'].queryset = Club.objects.filter(id=club_id)
2017-08-17 18:55:20 +00:00
self.fields['club'].initial = club_id
self.fields['club'].widget = forms.HiddenInput()
2017-08-21 17:53:17 +00:00
if user_id >= 0:
self.fields['moderator'].queryset = User.objects.filter(id=user_id)
self.fields['moderator'].initial = user_id
self.fields['moderator'].widget = forms.HiddenInput()
2017-08-16 22:07:19 +00:00
def clean(self):
cleaned_data = super(MailingForm, self).clean()
if self.is_valid():
cleaned_data['email'] += '@' + settings.SITH_MAILING_DOMAIN
return cleaned_data
2017-08-16 22:07:19 +00:00
class MailingSubscriptionForm(forms.ModelForm):
class Meta:
model = MailingSubscription
fields = ('mailing', 'user', 'email')
def __init__(self, *args, **kwargs):
2017-08-21 17:53:17 +00:00
kwargs.pop('user_id', None) # For standart interface
2017-08-16 22:07:19 +00:00
club_id = kwargs.pop('club_id', None)
super(MailingSubscriptionForm, self).__init__(*args, **kwargs)
2017-08-17 18:55:20 +00:00
self.fields['email'].required = False
2017-08-16 22:07:19 +00:00
if club_id:
2017-08-21 17:53:17 +00:00
self.fields['mailing'].queryset = Mailing.objects.filter(club__id=club_id, is_moderated=True)
2017-08-16 22:07:19 +00:00
user = AutoCompleteSelectField('users', label=_('User'), help_text=None, required=False)
class ClubTabsMixin(TabedViewMixin):
def get_tabs_title(self):
return self.object.get_display_name()
def get_list_of_tabs(self):
tab_list = []
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_view', kwargs={'club_id': self.object.id}),
'slug': 'infos',
'name': _("Infos"),
2017-06-12 06:54:48 +00:00
})
if self.request.user.can_view(self.object):
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_members', kwargs={'club_id': self.object.id}),
'slug': 'members',
'name': _("Members"),
2017-06-12 06:54:48 +00:00
})
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_old_members', kwargs={'club_id': self.object.id}),
'slug': 'elderlies',
'name': _("Old members"),
2017-06-12 06:54:48 +00:00
})
if self.request.user.can_edit(self.object):
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:tools', kwargs={'club_id': self.object.id}),
'slug': 'tools',
'name': _("Tools"),
2017-06-12 06:54:48 +00:00
})
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_edit', kwargs={'club_id': self.object.id}),
'slug': 'edit',
'name': _("Edit"),
2017-06-12 06:54:48 +00:00
})
2016-09-08 01:29:49 +00:00
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_sellings', kwargs={'club_id': self.object.id}),
'slug': 'sellings',
2016-09-08 01:29:49 +00:00
'name': _("Sellings"),
2017-06-12 06:54:48 +00:00
})
2017-08-17 18:55:20 +00:00
tab_list.append({
'url': reverse('club:mailing', kwargs={'club_id': self.object.id}),
'slug': 'mailing',
'name': _("Mailing list"),
})
if self.request.user.is_owner(self.object):
tab_list.append({
2017-06-12 06:54:48 +00:00
'url': reverse('club:club_prop', kwargs={'club_id': self.object.id}),
'slug': 'props',
'name': _("Props"),
2017-06-12 06:54:48 +00:00
})
return tab_list
2017-06-12 06:54:48 +00:00
2016-07-27 15:23:02 +00:00
class ClubListView(ListView):
2016-02-08 16:09:52 +00:00
"""
List the Clubs
"""
model = Club
template_name = 'club/club_list.jinja'
2017-06-12 06:54:48 +00:00
class ClubView(ClubTabsMixin, DetailView):
2016-02-08 16:09:52 +00:00
"""
Front page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_detail.jinja'
current_tab = "infos"
2017-06-12 06:54:48 +00:00
class ClubToolsView(ClubTabsMixin, CanEditMixin, DetailView):
2016-05-09 09:49:01 +00:00
"""
Tools page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_tools.jinja'
current_tab = "tools"
2016-09-02 07:23:21 +00:00
2017-06-12 06:54:48 +00:00
2016-02-04 07:59:03 +00:00
class ClubMemberForm(forms.ModelForm):
2016-02-08 16:09:52 +00:00
"""
Form handling the members of a club
"""
2016-02-04 07:59:03 +00:00
error_css_class = 'error'
required_css_class = 'required'
2017-06-12 06:54:48 +00:00
2016-02-04 07:59:03 +00:00
class Meta:
model = Membership
2016-12-28 23:42:26 +00:00
fields = ['user', 'role', 'start_date', 'description']
widgets = {
2017-06-12 06:54:48 +00:00
'start_date': SelectDate
}
2016-09-27 19:05:57 +00:00
user = AutoCompleteSelectField('users', required=True, label=_("Select user"), help_text=None)
2016-02-04 07:59:03 +00:00
2016-02-08 16:09:52 +00:00
def save(self, *args, **kwargs):
"""
Overloaded to return the club, and not to a Membership object that has no view
"""
2017-06-12 06:54:48 +00:00
super(ClubMemberForm, self).save(*args, **kwargs)
2016-02-08 16:09:52 +00:00
return self.instance.club
2017-06-12 06:54:48 +00:00
class ClubMembersView(ClubTabsMixin, CanViewMixin, UpdateView):
2016-02-08 16:09:52 +00:00
"""
View of a club's members
"""
model = Club
pk_url_kwarg = "club_id"
2016-02-04 07:59:03 +00:00
form_class = ClubMemberForm
template_name = 'club/club_members.jinja'
current_tab = "members"
2016-02-04 07:59:03 +00:00
def get_form(self):
2016-02-08 16:09:52 +00:00
"""
Here we get a Membership object, but the view handles Club object.
That's why the save method of ClubMemberForm is overridden.
"""
2016-02-05 15:59:42 +00:00
form = super(ClubMembersView, self).get_form()
2017-06-12 06:54:48 +00:00
if 'user' in form.data and form.data.get('user') != '': # Load an existing membership if possible
2016-02-04 07:59:03 +00:00
form.instance = Membership.objects.filter(club=self.object).filter(user=form.data.get('user')).filter(end_date=None).first()
2017-06-12 06:54:48 +00:00
if form.instance is None: # Instanciate a new membership
2016-02-04 07:59:03 +00:00
form.instance = Membership(club=self.object, user=self.request.user)
if not self.request.user.is_root:
form.fields.pop('start_date', None)
2016-09-27 19:05:57 +00:00
# form.initial = {'user': self.request.user}
# form._user = self.request.user
2016-02-04 07:59:03 +00:00
return form
2016-09-27 19:05:57 +00:00
def post(self, request, *args, **kwargs):
"""
Check user rights
"""
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
ms = self.object.get_membership_for(request.user)
if (form.cleaned_data['role'] <= SITH_MAXIMUM_FREE_ROLE or
(ms is not None and ms.role >= form.cleaned_data['role']) or
request.user.is_board_member or
2017-06-12 06:54:48 +00:00
request.user.is_root):
2016-09-27 19:05:57 +00:00
return self.form_valid(form)
else:
2016-11-06 10:36:54 +00:00
form.add_error(None, _("You do not have the permission to do that"))
2016-09-27 19:05:57 +00:00
return self.form_invalid(form)
else:
return self.form_invalid(form)
2017-06-12 06:54:48 +00:00
class ClubOldMembersView(ClubTabsMixin, CanViewMixin, DetailView):
2016-09-02 19:21:57 +00:00
"""
Old members of a club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_old_members.jinja'
current_tab = "elderlies"
2016-09-02 19:21:57 +00:00
2017-06-12 06:54:48 +00:00
2016-09-08 01:29:49 +00:00
class SellingsFormBase(forms.Form):
begin_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("Begin date"), required=False, widget=SelectDateTime)
end_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("End date"), required=False, widget=SelectDateTime)
counter = forms.ModelChoiceField(Counter.objects.order_by('name').all(), label=_("Counter"), required=False)
2017-06-12 06:54:48 +00:00
2016-09-08 01:29:49 +00:00
class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailView):
"""
Sellings of a club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_sellings.jinja'
current_tab = "sellings"
def get_form_class(self):
kwargs = {
2017-06-12 06:54:48 +00:00
'product': forms.ModelChoiceField(self.object.products.order_by('name').all(), label=_("Product"), required=False)
}
2016-09-08 01:29:49 +00:00
return type('SellingsForm', (SellingsFormBase,), kwargs)
def get_context_data(self, **kwargs):
kwargs = super(ClubSellingView, self).get_context_data(**kwargs)
2016-09-12 15:34:17 +00:00
form = self.get_form_class()(self.request.GET)
2016-09-08 01:29:49 +00:00
qs = Selling.objects.filter(club=self.object)
if form.is_valid():
2016-09-12 15:34:17 +00:00
if not len([v for v in form.cleaned_data.values() if v is not None]):
qs = Selling.objects.filter(id=-1)
2016-09-08 01:29:49 +00:00
if form.cleaned_data['begin_date']:
qs = qs.filter(date__gte=form.cleaned_data['begin_date'])
if form.cleaned_data['end_date']:
qs = qs.filter(date__lte=form.cleaned_data['end_date'])
if form.cleaned_data['counter']:
qs = qs.filter(counter=form.cleaned_data['counter'])
if form.cleaned_data['product']:
qs = qs.filter(product__id=form.cleaned_data['product'].id)
kwargs['result'] = qs.all().order_by('-id')
kwargs['total'] = sum([s.quantity * s.unit_price for s in qs.all()])
2016-10-03 11:55:20 +00:00
kwargs['total_quantity'] = sum([s.quantity for s in qs.all()])
2017-01-05 14:48:06 +00:00
kwargs['benefit'] = kwargs['total'] - sum([s.product.purchase_price for s in qs.exclude(product=None)])
2016-09-08 01:29:49 +00:00
else:
kwargs['result'] = qs[:0]
kwargs['form'] = form
return kwargs
2017-06-12 06:54:48 +00:00
2016-11-07 21:05:09 +00:00
class ClubSellingCSVView(ClubSellingView):
"""
Generate sellings in csv for a given period
"""
def get(self, request, *args, **kwargs):
import csv
response = HttpResponse(content_type='text/csv')
self.object = self.get_object()
name = _("Sellings") + "_" + self.object.name + ".csv"
response['Content-Disposition'] = 'filename=' + name
kwargs = self.get_context_data(**kwargs)
writer = csv.writer(response, delimiter=";", lineterminator='\n', quoting=csv.QUOTE_ALL)
2017-01-05 14:48:06 +00:00
writer.writerow([_t('Quantity'), kwargs['total_quantity']])
writer.writerow([_t('Total'), kwargs['total']])
writer.writerow([_t('Benefit'), kwargs['benefit']])
2017-06-12 06:54:48 +00:00
writer.writerow([_t('Date'), _t('Counter'), _t('Barman'), _t('Customer'), _t('Label'),
_t('Quantity'), _t('Total'), _t('Payment method'), _t('Selling price'), _t('Purchase price'), _t('Benefit')])
2016-11-07 21:05:09 +00:00
for o in kwargs['result']:
row = [o.date, o.counter]
if o.seller:
row.append(o.seller.get_display_name())
2017-06-12 06:54:48 +00:00
else:
row.append('')
2016-11-07 21:05:09 +00:00
if o.customer:
row.append(o.customer.user.get_display_name())
2017-06-12 06:54:48 +00:00
else:
row.append('')
row = row + [o.label, o.quantity, o.quantity * o.unit_price,
o.get_payment_method_display()]
2017-01-05 14:48:06 +00:00
if o.product:
row.append(o.product.selling_price)
row.append(o.product.purchase_price)
row.append(o.product.selling_price - o.product.purchase_price)
2017-06-12 06:54:48 +00:00
else:
row = row + ['', '', '']
2016-11-07 21:05:09 +00:00
writer.writerow(row)
return response
2017-06-12 06:54:48 +00:00
class ClubEditView(ClubTabsMixin, CanEditMixin, UpdateView):
2016-02-08 16:09:52 +00:00
"""
Edit a Club's main informations (for the club's members)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['address']
template_name = 'core/edit.jinja'
current_tab = "edit"
2016-09-02 07:23:21 +00:00
2017-06-12 06:54:48 +00:00
class ClubEditPropView(ClubTabsMixin, CanEditPropMixin, UpdateView):
2016-02-08 16:09:52 +00:00
"""
Edit the properties of a Club object (for the Sith admins)
"""
model = Club
pk_url_kwarg = "club_id"
2016-04-12 08:00:15 +00:00
fields = ['name', 'unix_name', 'parent']
template_name = 'core/edit.jinja'
current_tab = "props"
2016-05-03 10:06:03 +00:00
2017-06-12 06:54:48 +00:00
2016-05-03 10:06:03 +00:00
class ClubCreateView(CanEditPropMixin, CreateView):
"""
Create a club (for the Sith admin)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['name', 'unix_name', 'parent']
template_name = 'core/edit.jinja'
2016-05-03 10:06:03 +00:00
2017-06-12 06:54:48 +00:00
2016-09-02 19:21:57 +00:00
class MembershipSetOldView(CanEditMixin, DetailView):
"""
Set a membership as beeing old
"""
model = Membership
pk_url_kwarg = "membership_id"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.end_date = timezone.now()
self.object.save()
return HttpResponseRedirect(reverse('club:club_members', args=self.args, kwargs={'club_id': self.object.club.id}))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return HttpResponseRedirect(reverse('club:club_members', args=self.args, kwargs={'club_id': self.object.club.id}))
2017-06-12 06:54:48 +00:00
2016-11-25 14:26:45 +00:00
class ClubStatView(TemplateView):
2017-06-12 06:54:48 +00:00
template_name = "club/stats.jinja"
2016-11-25 14:26:45 +00:00
def get_context_data(self, **kwargs):
kwargs = super(ClubStatView, self).get_context_data(**kwargs)
kwargs['club_list'] = Club.objects.all()
return kwargs
2017-08-16 22:07:19 +00:00
2017-08-17 18:55:20 +00:00
class ClubMailingView(ClubTabsMixin, ListView):
2017-08-16 22:07:19 +00:00
"""
A list of mailing for a given club
"""
action = None
model = Mailing
template_name = "club/mailing.jinja"
2017-08-17 18:55:20 +00:00
current_tab = 'mailing'
def authorized(self):
2017-08-19 14:13:53 +00:00
return self.club.has_rights_in_club(self.user) or self.user.is_root or self.user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID)
2017-08-16 22:07:19 +00:00
def dispatch(self, request, *args, **kwargs):
self.club = get_object_or_404(Club, pk=kwargs['club_id'])
self.user = request.user
2017-08-17 18:55:20 +00:00
self.object = self.club
if not self.authorized():
raise PermissionDenied
2017-08-16 22:07:19 +00:00
self.member_form = MailingSubscriptionForm(club_id=self.club.id)
2017-08-21 17:53:17 +00:00
self.mailing_form = MailingForm(club_id=self.club.id, user_id=self.user.id)
2017-08-16 22:07:19 +00:00
return super(ClubMailingView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
res = super(ClubMailingView, self).get(request, *args, **kwargs)
2017-08-18 23:19:31 +00:00
if self.action != "display":
if self.action == "add_mailing":
2017-08-16 22:07:19 +00:00
form = MailingForm
2017-08-17 18:55:20 +00:00
model = Mailing
2017-08-18 23:19:31 +00:00
elif self.action == "add_member":
2017-08-16 22:07:19 +00:00
form = MailingSubscriptionForm
2017-08-17 18:55:20 +00:00
model = MailingSubscription
2017-08-18 23:19:31 +00:00
return MailingGenericCreateView.as_view(model=model, list_view=self, form_class=form)(request, *args, **kwargs)
2017-08-16 22:07:19 +00:00
return res
def get_queryset(self):
return Mailing.objects.filter(club_id=self.club.id).all()
def get_context_data(self, **kwargs):
kwargs = super(ClubMailingView, self).get_context_data(**kwargs)
2017-08-18 23:19:31 +00:00
kwargs['add_member'] = self.member_form
kwargs['add_mailing'] = self.mailing_form
2017-08-16 22:07:19 +00:00
kwargs['club'] = self.club
kwargs['user'] = self.user
kwargs['has_objects'] = len(kwargs['object_list']) > 0
return kwargs
2017-08-17 18:55:20 +00:00
class MailingGenericCreateView(CreateView, SingleObjectMixin):
2017-08-16 22:07:19 +00:00
"""
Create a new mailing list
"""
list_view = None
form_class = None
def get_context_data(self, **kwargs):
view_kwargs = self.list_view.get_context_data(**kwargs)
for key, data in super(MailingGenericCreateView, self).get_context_data(**kwargs).items():
view_kwargs[key] = data
2017-08-18 23:19:31 +00:00
view_kwargs[self.list_view.action] = view_kwargs['form']
2017-08-16 22:07:19 +00:00
return view_kwargs
def get_form_kwargs(self):
kwargs = super(MailingGenericCreateView, self).get_form_kwargs()
kwargs['club_id'] = self.list_view.club.id
2017-08-21 17:53:17 +00:00
kwargs['user_id'] = self.list_view.user.id
2017-08-16 22:07:19 +00:00
return kwargs
def dispatch(self, request, *args, **kwargs):
2017-08-17 18:55:20 +00:00
if not self.list_view.authorized():
raise PermissionDenied
2017-08-16 22:07:19 +00:00
self.template_name = self.list_view.template_name
return super(MailingGenericCreateView, self).dispatch(request, *args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy('club:mailing', kwargs={'club_id': self.list_view.club.id})
2017-08-17 18:55:20 +00:00
class MailingDeleteView(CanEditMixin, DeleteView):
model = Mailing
template_name = 'core/delete_confirm.jinja'
pk_url_kwarg = "mailing_id"
2017-08-21 17:53:17 +00:00
redirect_page = None
2017-08-17 18:55:20 +00:00
def dispatch(self, request, *args, **kwargs):
self.club_id = self.get_object().club.id
return super(MailingDeleteView, self).dispatch(request, *args, **kwargs)
def get_success_url(self, **kwargs):
2017-08-21 17:53:17 +00:00
if self.redirect_page:
return reverse_lazy(self.redirect_page)
else:
return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id})
2017-08-17 18:55:20 +00:00
class MailingSubscriptionDeleteView(CanEditMixin, DeleteView):
model = MailingSubscription
template_name = 'core/delete_confirm.jinja'
pk_url_kwarg = "mailing_subscription_id"
def dispatch(self, request, *args, **kwargs):
self.club_id = self.get_object().mailing.club.id
return super(MailingSubscriptionDeleteView, self).dispatch(request, *args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id})