2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
2017-08-16 22:07:19 +00:00
|
|
|
# - Sli <antoine@bartuccio.fr>
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2019-04-29 17:22:01 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2016-02-04 07:59:03 +00:00
|
|
|
from django import forms
|
2017-10-01 22:10:52 +00:00
|
|
|
from django.views.generic import ListView, DetailView, TemplateView, View
|
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
|
2017-10-01 18:52:29 +00:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse, Http404
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls 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
|
2019-05-09 15:43:47 +00:00
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError, NON_FIELD_ERRORS
|
2017-10-01 22:10:52 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2016-09-08 01:29:49 +00:00
|
|
|
|
2018-10-05 19:51:54 +00:00
|
|
|
from core.views import (
|
|
|
|
CanCreateMixin,
|
|
|
|
CanViewMixin,
|
|
|
|
CanEditMixin,
|
|
|
|
CanEditPropMixin,
|
|
|
|
TabedViewMixin,
|
|
|
|
PageEditViewBase,
|
2019-04-24 01:10:42 +00:00
|
|
|
DetailFormView,
|
2018-10-05 19:51:54 +00:00
|
|
|
)
|
2019-04-29 17:22:01 +00:00
|
|
|
from core.models import PageRev
|
|
|
|
|
|
|
|
from counter.models import Selling
|
|
|
|
|
2018-10-05 19:51:54 +00:00
|
|
|
from com.views import (
|
|
|
|
PosterListBaseView,
|
|
|
|
PosterCreateBaseView,
|
|
|
|
PosterEditBaseView,
|
|
|
|
PosterDeleteBaseView,
|
|
|
|
)
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2019-04-29 17:22:01 +00:00
|
|
|
from club.models import Club, Membership, Mailing, MailingSubscription
|
2019-05-21 08:26:29 +00:00
|
|
|
from club.forms import MailingForm, ClubEditForm, ClubMemberForm, SellingsFormBase
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
|
2016-09-04 17:24:53 +00:00
|
|
|
class ClubTabsMixin(TabedViewMixin):
|
|
|
|
def get_tabs_title(self):
|
2017-12-16 19:48:18 +00:00
|
|
|
obj = self.get_object()
|
|
|
|
if isinstance(obj, PageRev):
|
|
|
|
self.object = obj.page.club
|
2016-09-04 17:24:53 +00:00
|
|
|
return self.object.get_display_name()
|
|
|
|
|
|
|
|
def get_list_of_tabs(self):
|
|
|
|
tab_list = []
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse("club:club_view", kwargs={"club_id": self.object.id}),
|
|
|
|
"slug": "infos",
|
|
|
|
"name": _("Infos"),
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 17:24:53 +00:00
|
|
|
if self.request.user.can_view(self.object):
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_members", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "members",
|
|
|
|
"name": _("Members"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_old_members", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "elderlies",
|
|
|
|
"name": _("Old members"),
|
|
|
|
}
|
|
|
|
)
|
2017-10-01 18:52:29 +00:00
|
|
|
if self.object.page:
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_hist", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "history",
|
|
|
|
"name": _("History"),
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 17:24:53 +00:00
|
|
|
if self.request.user.can_edit(self.object):
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse("club:tools", kwargs={"club_id": self.object.id}),
|
|
|
|
"slug": "tools",
|
|
|
|
"name": _("Tools"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_edit", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "edit",
|
|
|
|
"name": _("Edit"),
|
|
|
|
}
|
|
|
|
)
|
2017-10-01 18:52:29 +00:00
|
|
|
if self.object.page and self.request.user.can_edit(self.object.page):
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"core:page_edit",
|
|
|
|
kwargs={"page_name": self.object.page.get_full_name()},
|
|
|
|
),
|
|
|
|
"slug": "page_edit",
|
|
|
|
"name": _("Edit club page"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_sellings", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "sellings",
|
|
|
|
"name": _("Sellings"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse("club:mailing", kwargs={"club_id": self.object.id}),
|
|
|
|
"slug": "mailing",
|
|
|
|
"name": _("Mailing list"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:poster_list", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "posters",
|
|
|
|
"name": _("Posters list"),
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 17:24:53 +00:00
|
|
|
if self.request.user.is_owner(self.object):
|
2018-10-05 19:51:54 +00:00
|
|
|
tab_list.append(
|
|
|
|
{
|
|
|
|
"url": reverse(
|
|
|
|
"club:club_prop", kwargs={"club_id": self.object.id}
|
|
|
|
),
|
|
|
|
"slug": "props",
|
|
|
|
"name": _("Props"),
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 17:24:53 +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
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
model = Club
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_list.jinja"
|
2016-02-02 15:34:36 +00:00
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2016-09-04 17:24:53 +00:00
|
|
|
class ClubView(ClubTabsMixin, DetailView):
|
2016-02-08 16:09:52 +00:00
|
|
|
"""
|
|
|
|
Front page of a Club
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_detail.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
current_tab = "infos"
|
2016-02-02 15:34:36 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ClubView, self).get_context_data(**kwargs)
|
|
|
|
if self.object.page and self.object.page.revisions.exists():
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["page_revision"] = self.object.page.revisions.last().content
|
2017-10-01 18:52:29 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
|
|
class ClubRevView(ClubView):
|
|
|
|
"""
|
|
|
|
Display a specific page revision
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
obj = self.get_object()
|
2018-10-05 19:51:54 +00:00
|
|
|
self.revision = get_object_or_404(PageRev, pk=kwargs["rev_id"], page__club=obj)
|
2017-10-01 18:52:29 +00:00
|
|
|
return super(ClubRevView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ClubRevView, self).get_context_data(**kwargs)
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["page_revision"] = self.revision.content
|
2017-10-01 18:52:29 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
|
|
class ClubPageEditView(ClubTabsMixin, PageEditViewBase):
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/pagerev_edit.jinja"
|
2017-10-01 18:52:29 +00:00
|
|
|
current_tab = "page_edit"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2018-10-05 19:51:54 +00:00
|
|
|
self.club = get_object_or_404(Club, pk=kwargs["club_id"])
|
2017-10-01 18:52:29 +00:00
|
|
|
if not self.club.page:
|
|
|
|
raise Http404
|
|
|
|
return super(ClubPageEditView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
self.page = self.club.page
|
|
|
|
return self._get_revision()
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2018-10-05 19:51:54 +00:00
|
|
|
return reverse_lazy("club:club_view", kwargs={"club_id": self.club.id})
|
2017-10-01 18:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClubPageHistView(ClubTabsMixin, CanViewMixin, DetailView):
|
|
|
|
"""
|
|
|
|
Modification hostory of the page
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2017-10-01 18:52:29 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/page_history.jinja"
|
2017-10-01 18:52:29 +00:00
|
|
|
current_tab = "history"
|
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2016-09-04 17:24:53 +00:00
|
|
|
class ClubToolsView(ClubTabsMixin, CanEditMixin, DetailView):
|
2016-05-09 09:49:01 +00:00
|
|
|
"""
|
|
|
|
Tools page of a Club
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-05-09 09:49:01 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_tools.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
current_tab = "tools"
|
2016-09-02 07:23:21 +00:00
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2019-04-24 01:10:42 +00:00
|
|
|
class ClubMembersView(ClubTabsMixin, CanViewMixin, DetailFormView):
|
2016-02-08 16:09:52 +00:00
|
|
|
"""
|
|
|
|
View of a club's members
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2016-02-04 07:59:03 +00:00
|
|
|
form_class = ClubMemberForm
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_members.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
current_tab = "members"
|
2016-02-02 15:34:36 +00:00
|
|
|
|
2019-04-24 01:10:42 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(ClubMembersView, self).get_form_kwargs()
|
2019-04-25 17:51:30 +00:00
|
|
|
kwargs["request_user"] = self.request.user
|
2019-04-24 01:10:42 +00:00
|
|
|
kwargs["club"] = self.get_object()
|
2019-04-24 16:17:03 +00:00
|
|
|
kwargs["club_members"] = self.members
|
2019-04-24 01:10:42 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
kwargs = super(ClubMembersView, self).get_context_data(*args, **kwargs)
|
2019-04-24 16:17:03 +00:00
|
|
|
kwargs["members"] = self.members
|
2019-04-24 01:10:42 +00:00
|
|
|
return kwargs
|
2016-02-04 07:59:03 +00:00
|
|
|
|
2017-10-01 21:14:47 +00:00
|
|
|
def form_valid(self, form):
|
2016-09-27 19:05:57 +00:00
|
|
|
"""
|
|
|
|
Check user rights
|
|
|
|
"""
|
2019-04-24 01:10:42 +00:00
|
|
|
resp = super(ClubMembersView, self).form_valid(form)
|
|
|
|
|
|
|
|
data = form.clean()
|
|
|
|
users = data.pop("users", [])
|
2019-04-24 16:17:03 +00:00
|
|
|
users_old = data.pop("users_old", [])
|
2019-04-24 01:10:42 +00:00
|
|
|
for user in users:
|
|
|
|
Membership(club=self.get_object(), user=user, **data).save()
|
2019-04-24 16:17:03 +00:00
|
|
|
for user in users_old:
|
|
|
|
membership = self.get_object().get_membership_for(user)
|
|
|
|
membership.end_date = timezone.now()
|
|
|
|
membership.save()
|
2019-04-24 01:10:42 +00:00
|
|
|
return resp
|
2016-09-27 19:05:57 +00:00
|
|
|
|
2017-10-01 21:14:47 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2019-04-24 16:17:03 +00:00
|
|
|
self.members = (
|
|
|
|
self.get_object().members.filter(end_date=None).order_by("-role").all()
|
|
|
|
)
|
2017-10-01 21:14:47 +00:00
|
|
|
return super(ClubMembersView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2019-04-24 01:10:42 +00:00
|
|
|
return reverse_lazy(
|
|
|
|
"club:club_members", kwargs={"club_id": self.get_object().id}
|
|
|
|
)
|
2017-10-01 21:14:47 +00:00
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2016-09-04 17:24:53 +00:00
|
|
|
class ClubOldMembersView(ClubTabsMixin, CanViewMixin, DetailView):
|
2016-09-02 19:21:57 +00:00
|
|
|
"""
|
|
|
|
Old members of a club
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-09-02 19:21:57 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_old_members.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
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 ClubSellingView(ClubTabsMixin, CanEditMixin, DetailView):
|
|
|
|
"""
|
|
|
|
Sellings of a club
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-09-08 01:29:49 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "club/club_sellings.jinja"
|
2016-09-08 01:29:49 +00:00
|
|
|
current_tab = "sellings"
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
kwargs = {
|
2018-10-05 19:51:54 +00:00
|
|
|
"product": forms.ModelChoiceField(
|
|
|
|
self.object.products.order_by("name").all(),
|
|
|
|
label=_("Product"),
|
|
|
|
required=False,
|
|
|
|
)
|
2017-06-12 06:54:48 +00:00
|
|
|
}
|
2018-10-05 19:51:54 +00:00
|
|
|
return type("SellingsForm", (SellingsFormBase,), kwargs)
|
2016-09-08 01:29:49 +00:00
|
|
|
|
|
|
|
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)
|
2018-10-05 19:51:54 +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()])
|
|
|
|
kwargs["total_quantity"] = sum([s.quantity for s in qs.all()])
|
|
|
|
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:
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["result"] = qs[:0]
|
|
|
|
kwargs["form"] = form
|
2016-09-08 01:29:49 +00:00
|
|
|
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
|
2018-10-05 19:51:54 +00:00
|
|
|
|
|
|
|
response = HttpResponse(content_type="text/csv")
|
2016-11-07 21:05:09 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
name = _("Sellings") + "_" + self.object.name + ".csv"
|
2018-10-05 19:51:54 +00:00
|
|
|
response["Content-Disposition"] = "filename=" + name
|
2016-11-07 21:05:09 +00:00
|
|
|
kwargs = self.get_context_data(**kwargs)
|
2018-10-05 19:51:54 +00:00
|
|
|
writer = csv.writer(
|
|
|
|
response, delimiter=";", lineterminator="\n", quoting=csv.QUOTE_ALL
|
|
|
|
)
|
|
|
|
|
|
|
|
writer.writerow([_t("Quantity"), kwargs["total_quantity"]])
|
|
|
|
writer.writerow([_t("Total"), kwargs["total"]])
|
|
|
|
writer.writerow([_t("Benefit"), kwargs["benefit"]])
|
|
|
|
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"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
for o in kwargs["result"]:
|
2016-11-07 21:05:09 +00:00
|
|
|
row = [o.date, o.counter]
|
|
|
|
if o.seller:
|
|
|
|
row.append(o.seller.get_display_name())
|
2017-06-12 06:54:48 +00:00
|
|
|
else:
|
2018-10-05 19:51:54 +00:00
|
|
|
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:
|
2018-10-05 19:51:54 +00:00
|
|
|
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:
|
2018-10-05 19:51:54 +00:00
|
|
|
row = row + ["", "", ""]
|
2016-11-07 21:05:09 +00:00
|
|
|
writer.writerow(row)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2016-09-04 17:24:53 +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)
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-02-08 16:09:52 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-03 22:15:47 +00:00
|
|
|
form_class = ClubEditForm
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "core/edit.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
current_tab = "edit"
|
2016-09-02 07:23:21 +00:00
|
|
|
|
2017-06-12 06:54:48 +00:00
|
|
|
|
2016-09-04 17:24:53 +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)
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-02-02 15:34:36 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
fields = ["name", "unix_name", "parent", "is_active"]
|
|
|
|
template_name = "core/edit.jinja"
|
2016-09-04 17:24:53 +00:00
|
|
|
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)
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-05-03 10:06:03 +00:00
|
|
|
model = Club
|
|
|
|
pk_url_kwarg = "club_id"
|
2018-10-05 19:51:54 +00:00
|
|
|
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
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2016-09-02 19:21:57 +00:00
|
|
|
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()
|
2018-10-05 19:51:54 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"club:club_members",
|
|
|
|
args=self.args,
|
|
|
|
kwargs={"club_id": self.object.club.id},
|
|
|
|
)
|
|
|
|
)
|
2016-09-02 19:21:57 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2018-10-05 19:51:54 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"club:club_members",
|
|
|
|
args=self.args,
|
|
|
|
kwargs={"club_id": self.object.club.id},
|
|
|
|
)
|
|
|
|
)
|
2016-09-02 19:21:57 +00:00
|
|
|
|
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)
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["club_list"] = Club.objects.all()
|
2016-11-25 14:26:45 +00:00
|
|
|
return kwargs
|
2017-08-16 22:07:19 +00:00
|
|
|
|
|
|
|
|
2019-05-01 13:50:03 +00:00
|
|
|
class ClubMailingView(ClubTabsMixin, CanEditMixin, DetailFormView):
|
2017-08-16 22:07:19 +00:00
|
|
|
"""
|
|
|
|
A list of mailing for a given club
|
|
|
|
"""
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
model = Club
|
|
|
|
form_class = MailingForm
|
|
|
|
pk_url_kwarg = "club_id"
|
2017-08-16 22:07:19 +00:00
|
|
|
template_name = "club/mailing.jinja"
|
2018-10-05 19:51:54 +00:00
|
|
|
current_tab = "mailing"
|
2017-08-17 18:55:20 +00:00
|
|
|
|
2019-05-01 20:52:22 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(ClubMailingView, self).get_form_kwargs()
|
2019-05-01 01:32:28 +00:00
|
|
|
kwargs["club_id"] = self.get_object().id
|
|
|
|
kwargs["user_id"] = self.request.user.id
|
2019-05-01 20:52:22 +00:00
|
|
|
kwargs["mailings"] = self.mailings
|
2019-05-01 01:32:28 +00:00
|
|
|
return kwargs
|
|
|
|
|
2019-05-01 20:52:22 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.mailings = Mailing.objects.filter(club_id=self.get_object().id).all()
|
|
|
|
return super(ClubMailingView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
2017-08-16 22:07:19 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(ClubMailingView, self).get_context_data(**kwargs)
|
2019-05-01 01:32:28 +00:00
|
|
|
kwargs["club"] = self.get_object()
|
|
|
|
kwargs["user"] = self.request.user
|
2019-05-01 20:52:22 +00:00
|
|
|
kwargs["mailings"] = self.mailings
|
2019-05-01 01:32:28 +00:00
|
|
|
kwargs["mailings_moderated"] = (
|
|
|
|
kwargs["mailings"].exclude(is_moderated=False).all()
|
|
|
|
)
|
2019-05-09 15:43:47 +00:00
|
|
|
kwargs["mailings_not_moderated"] = (
|
|
|
|
kwargs["mailings"].exclude(is_moderated=True).all()
|
|
|
|
)
|
2019-05-01 01:32:28 +00:00
|
|
|
kwargs["form_actions"] = {
|
|
|
|
"NEW_MALING": self.form_class.ACTION_NEW_MAILING,
|
|
|
|
"NEW_SUBSCRIPTION": self.form_class.ACTION_NEW_SUBSCRIPTION,
|
2019-05-01 20:52:22 +00:00
|
|
|
"REMOVE_SUBSCRIPTION": self.form_class.ACTION_REMOVE_SUBSCRIPTION,
|
2019-05-01 01:32:28 +00:00
|
|
|
}
|
2017-08-16 22:07:19 +00:00
|
|
|
return kwargs
|
|
|
|
|
2019-05-09 15:43:47 +00:00
|
|
|
def add_new_mailing(self, cleaned_data) -> ValidationError:
|
2019-05-01 01:32:28 +00:00
|
|
|
"""
|
|
|
|
Create a new mailing list from the form
|
|
|
|
"""
|
|
|
|
mailing = Mailing(
|
2019-05-09 15:43:47 +00:00
|
|
|
club=self.get_object(),
|
2019-05-01 01:32:28 +00:00
|
|
|
email=cleaned_data["mailing_email"],
|
2019-05-09 15:43:47 +00:00
|
|
|
moderator=self.request.user,
|
2019-05-01 01:32:28 +00:00
|
|
|
is_moderated=False,
|
|
|
|
)
|
2019-05-09 15:43:47 +00:00
|
|
|
try:
|
|
|
|
mailing.clean()
|
|
|
|
except ValidationError as validation_error:
|
|
|
|
return validation_error
|
2019-05-01 01:32:28 +00:00
|
|
|
mailing.save()
|
2019-05-09 15:43:47 +00:00
|
|
|
return None
|
2017-12-22 11:06:23 +00:00
|
|
|
|
2019-05-09 15:43:47 +00:00
|
|
|
def add_new_subscription(self, cleaned_data) -> ValidationError:
|
2019-05-01 01:32:28 +00:00
|
|
|
"""
|
|
|
|
Add mailing subscriptions for each user given and/or for the specified email in form
|
|
|
|
"""
|
2019-05-09 16:06:11 +00:00
|
|
|
users_to_save = []
|
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
for user in cleaned_data["subscription_users"]:
|
|
|
|
sub = MailingSubscription(
|
|
|
|
mailing=cleaned_data["subscription_mailing"], user=user
|
|
|
|
)
|
2019-05-09 16:06:11 +00:00
|
|
|
try:
|
|
|
|
sub.clean()
|
|
|
|
except ValidationError as validation_error:
|
|
|
|
return validation_error
|
|
|
|
|
|
|
|
users_to_save.append(sub.save())
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
if cleaned_data["subscription_email"]:
|
|
|
|
sub = MailingSubscription(
|
|
|
|
mailing=cleaned_data["subscription_mailing"],
|
|
|
|
email=cleaned_data["subscription_email"],
|
|
|
|
)
|
2019-05-09 15:43:47 +00:00
|
|
|
|
|
|
|
try:
|
2019-05-01 01:32:28 +00:00
|
|
|
sub.clean()
|
2019-05-09 15:43:47 +00:00
|
|
|
except ValidationError as validation_error:
|
|
|
|
return validation_error
|
|
|
|
sub.save()
|
|
|
|
|
2019-05-09 16:06:11 +00:00
|
|
|
# Save users after we are sure there is no error
|
|
|
|
for user in users_to_save:
|
|
|
|
user.save()
|
|
|
|
|
2019-05-09 15:43:47 +00:00
|
|
|
return None
|
2018-10-05 19:51:54 +00:00
|
|
|
|
2019-05-01 20:52:22 +00:00
|
|
|
def remove_subscription(self, cleaned_data):
|
|
|
|
"""
|
|
|
|
Remove specified users from a mailing list
|
|
|
|
"""
|
|
|
|
fields = [
|
|
|
|
cleaned_data[key]
|
|
|
|
for key in cleaned_data.keys()
|
|
|
|
if key.startswith("removal_")
|
|
|
|
]
|
|
|
|
for field in fields:
|
|
|
|
for sub in field:
|
|
|
|
sub.delete()
|
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
resp = super(ClubMailingView, self).form_valid(form)
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
cleaned_data = form.clean()
|
2019-05-09 15:43:47 +00:00
|
|
|
error = None
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
if cleaned_data["action"] == self.form_class.ACTION_NEW_MAILING:
|
2019-05-09 15:43:47 +00:00
|
|
|
error = self.add_new_mailing(cleaned_data)
|
2017-08-16 22:07:19 +00:00
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
if cleaned_data["action"] == self.form_class.ACTION_NEW_SUBSCRIPTION:
|
2019-05-09 15:43:47 +00:00
|
|
|
error = self.add_new_subscription(cleaned_data)
|
2019-05-01 01:32:28 +00:00
|
|
|
|
2019-05-01 20:52:22 +00:00
|
|
|
if cleaned_data["action"] == self.form_class.ACTION_REMOVE_SUBSCRIPTION:
|
|
|
|
self.remove_subscription(cleaned_data)
|
|
|
|
|
2019-05-09 15:43:47 +00:00
|
|
|
if error:
|
|
|
|
form.add_error(NON_FIELD_ERRORS, error)
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2019-05-01 01:32:28 +00:00
|
|
|
return resp
|
2017-08-16 22:07:19 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2019-05-01 01:32:28 +00:00
|
|
|
return reverse_lazy("club:mailing", kwargs={"club_id": self.get_object().id})
|
2017-08-17 18:55:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MailingDeleteView(CanEditMixin, DeleteView):
|
|
|
|
|
|
|
|
model = Mailing
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "core/delete_confirm.jinja"
|
2017-08-17 18:55:20 +00:00
|
|
|
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:
|
2018-10-05 19:51:54 +00:00
|
|
|
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
|
2018-10-05 19:51:54 +00:00
|
|
|
template_name = "core/delete_confirm.jinja"
|
2017-08-17 18:55:20 +00:00
|
|
|
pk_url_kwarg = "mailing_subscription_id"
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.club_id = self.get_object().mailing.club.id
|
2018-10-05 19:51:54 +00:00
|
|
|
return super(MailingSubscriptionDeleteView, self).dispatch(
|
|
|
|
request, *args, **kwargs
|
|
|
|
)
|
2017-08-17 18:55:20 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2018-10-05 19:51:54 +00:00
|
|
|
return reverse_lazy("club:mailing", kwargs={"club_id": self.club_id})
|
2017-10-01 22:10:52 +00:00
|
|
|
|
2017-10-02 11:16:43 +00:00
|
|
|
|
2017-10-01 22:10:52 +00:00
|
|
|
class MailingAutoGenerationView(View):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2018-10-05 19:51:54 +00:00
|
|
|
self.mailing = get_object_or_404(Mailing, pk=kwargs["mailing_id"])
|
2017-10-01 22:10:52 +00:00
|
|
|
if not request.user.can_edit(self.mailing):
|
|
|
|
raise PermissionDenied
|
|
|
|
return super(MailingAutoGenerationView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
club = self.mailing.club
|
2017-10-02 11:16:43 +00:00
|
|
|
self.mailing.subscriptions.all().delete()
|
2018-10-05 19:51:54 +00:00
|
|
|
members = club.members.filter(
|
|
|
|
role__gte=settings.SITH_CLUB_ROLES_ID["Board member"]
|
|
|
|
).exclude(end_date__lte=timezone.now())
|
2017-10-01 22:10:52 +00:00
|
|
|
for member in members.all():
|
|
|
|
MailingSubscription(user=member.user, mailing=self.mailing).save()
|
2018-10-05 19:51:54 +00:00
|
|
|
return redirect("club:mailing", club_id=club.id)
|
2017-10-01 22:10:52 +00:00
|
|
|
|
2017-10-02 11:16:43 +00:00
|
|
|
|
2017-12-21 17:02:08 +00:00
|
|
|
class PosterListView(ClubTabsMixin, PosterListBaseView, CanViewMixin):
|
2017-11-01 17:12:33 +00:00
|
|
|
"""List communication posters"""
|
|
|
|
|
2017-12-05 14:53:36 +00:00
|
|
|
def get_object(self):
|
|
|
|
return self.club
|
2017-12-05 14:24:46 +00:00
|
|
|
|
2017-11-01 17:12:33 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(PosterListView, self).get_context_data(**kwargs)
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["app"] = "club"
|
|
|
|
kwargs["club"] = self.club
|
2017-11-01 17:12:33 +00:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2017-12-05 14:24:46 +00:00
|
|
|
class PosterCreateView(PosterCreateBaseView, CanCreateMixin):
|
2017-11-01 17:12:33 +00:00
|
|
|
"""Create communication poster"""
|
|
|
|
|
2017-12-16 19:48:18 +00:00
|
|
|
pk_url_kwarg = "club_id"
|
2017-11-01 17:12:33 +00:00
|
|
|
|
2017-12-16 19:48:18 +00:00
|
|
|
def get_object(self):
|
|
|
|
obj = super(PosterCreateView, self).get_object()
|
|
|
|
if not obj:
|
|
|
|
return self.club
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
2018-10-05 19:51:54 +00:00
|
|
|
return reverse_lazy("club:poster_list", kwargs={"club_id": self.club.id})
|
2017-11-01 17:12:33 +00:00
|
|
|
|
2017-12-05 14:24:46 +00:00
|
|
|
|
|
|
|
class PosterEditView(ClubTabsMixin, PosterEditBaseView, CanEditMixin):
|
2017-11-01 17:12:33 +00:00
|
|
|
"""Edit communication poster"""
|
|
|
|
|
2017-11-28 13:43:05 +00:00
|
|
|
def get_success_url(self):
|
2018-10-05 19:51:54 +00:00
|
|
|
return reverse_lazy("club:poster_list", kwargs={"club_id": self.club.id})
|
2017-11-01 17:12:33 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(PosterEditView, self).get_context_data(**kwargs)
|
2018-10-05 19:51:54 +00:00
|
|
|
kwargs["app"] = "club"
|
2017-11-01 17:12:33 +00:00
|
|
|
return kwargs
|
|
|
|
|
2017-12-05 14:24:46 +00:00
|
|
|
|
|
|
|
class PosterDeleteView(PosterDeleteBaseView, ClubTabsMixin, CanEditMixin):
|
|
|
|
"""Delete communication poster"""
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2018-10-05 19:51:54 +00:00
|
|
|
return reverse_lazy("club:poster_list", kwargs={"club_id": self.club.id})
|