2020-02-18 21:30:26 +01:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
2019-07-17 13:34:07 +02:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-02-08 20:23:17 +01:00
|
|
|
from dal import autocomplete
|
2019-07-17 13:53:58 +02:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-03-11 21:47:04 +01:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2020-02-08 20:23:17 +01:00
|
|
|
from django.db.models import Q
|
2019-07-17 13:34:07 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-02-18 21:14:29 +01:00
|
|
|
from django.views.generic import CreateView, ListView, UpdateView
|
2020-03-09 20:59:04 +01:00
|
|
|
from django_tables2 import SingleTableView
|
2020-03-23 20:21:25 +01:00
|
|
|
from django.urls import reverse_lazy
|
2020-03-20 14:43:35 +01:00
|
|
|
from permission.backends import PermissionBackend
|
2020-03-20 02:14:43 +01:00
|
|
|
|
2020-03-12 23:12:49 +01:00
|
|
|
from .forms import TransactionTemplateForm
|
2020-03-19 20:37:48 +01:00
|
|
|
from .models import Transaction, TransactionTemplate, Alias, RecurrentTransaction, NoteSpecial
|
2020-03-14 15:13:58 +01:00
|
|
|
from .models.transactions import SpecialTransaction
|
2020-03-23 15:35:24 +01:00
|
|
|
from .tables import HistoryTable, ButtonTable
|
2020-03-09 20:59:04 +01:00
|
|
|
|
2020-02-18 12:31:15 +01:00
|
|
|
|
2020-03-23 15:28:09 +01:00
|
|
|
class TransactionCreateView(LoginRequiredMixin, SingleTableView):
|
2019-07-17 13:34:07 +02:00
|
|
|
"""
|
2020-03-23 15:28:09 +01:00
|
|
|
View for the creation of Transaction between two note which are not :models:`transactions.RecurrentTransaction`.
|
|
|
|
e.g. for donation/transfer between people and clubs or for credit/debit with :models:`note.NoteSpecial`
|
2019-07-17 13:34:07 +02:00
|
|
|
"""
|
2020-03-23 15:28:09 +01:00
|
|
|
template_name = "note/transaction_form.html"
|
|
|
|
|
2019-07-17 13:53:58 +02:00
|
|
|
model = Transaction
|
2020-03-16 12:11:16 +01:00
|
|
|
# Transaction history table
|
|
|
|
table_class = HistoryTable
|
|
|
|
table_pagination = {"per_page": 50}
|
|
|
|
|
2020-03-19 02:26:06 +01:00
|
|
|
def get_queryset(self):
|
2020-03-20 02:14:43 +01:00
|
|
|
return Transaction.objects.filter(PermissionBackend.filter_queryset(
|
|
|
|
self.request.user, Transaction, "view")
|
|
|
|
).order_by("-id").all()[:50]
|
2019-07-17 13:53:58 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Add some context variables in template such as page title
|
|
|
|
"""
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-14 15:36:03 +01:00
|
|
|
context['title'] = _('Transfer money')
|
2020-03-12 23:12:49 +01:00
|
|
|
context['polymorphic_ctype'] = ContentType.objects.get_for_model(Transaction).pk
|
2020-03-14 15:13:58 +01:00
|
|
|
context['special_polymorphic_ctype'] = ContentType.objects.get_for_model(SpecialTransaction).pk
|
|
|
|
context['special_types'] = NoteSpecial.objects.order_by("special_type").all()
|
2020-02-21 18:28:21 +01:00
|
|
|
|
2019-07-17 13:53:58 +02:00
|
|
|
return context
|
2019-08-11 19:54:18 +02:00
|
|
|
|
2020-02-08 23:24:49 +01:00
|
|
|
|
2020-02-08 20:23:17 +01:00
|
|
|
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
|
|
|
"""
|
2020-03-23 15:28:09 +01:00
|
|
|
Auto complete note by aliases. Used in every search field for note
|
|
|
|
ex: :view:`ConsoView`, :view:`TransactionCreateView`
|
2020-02-08 20:23:17 +01:00
|
|
|
"""
|
2020-03-07 22:28:59 +01:00
|
|
|
|
2020-02-08 20:23:17 +01:00
|
|
|
def get_queryset(self):
|
2020-02-08 21:40:32 +01:00
|
|
|
"""
|
2020-03-23 15:28:09 +01:00
|
|
|
When someone look for an :models:`note.Alias`, a query is sent to the dedicated API.
|
|
|
|
This function handles the result and return a filtered list of aliases.
|
2020-02-08 21:40:32 +01:00
|
|
|
"""
|
2020-02-08 23:24:49 +01:00
|
|
|
# Un utilisateur non connecté n'a accès à aucune information
|
|
|
|
if not self.request.user.is_authenticated:
|
2020-02-17 11:19:16 +01:00
|
|
|
return Alias.objects.none()
|
2020-02-08 23:24:49 +01:00
|
|
|
|
2020-02-17 11:19:16 +01:00
|
|
|
qs = Alias.objects.all()
|
2020-02-08 20:23:17 +01:00
|
|
|
|
2020-02-08 23:24:49 +01:00
|
|
|
# self.q est le paramètre de la recherche
|
2020-02-08 20:23:17 +01:00
|
|
|
if self.q:
|
2020-03-10 17:16:03 +01:00
|
|
|
qs = qs.filter(Q(name__regex="^" + self.q) | Q(normalized_name__regex="^" + Alias.normalize(self.q))) \
|
2020-02-17 11:19:16 +01:00
|
|
|
.order_by('normalized_name').distinct()
|
2020-02-08 20:23:17 +01:00
|
|
|
|
2020-02-08 23:24:49 +01:00
|
|
|
# Filtrage par type de note (user, club, special)
|
|
|
|
note_type = self.forwarded.get("note_type", None)
|
|
|
|
if note_type:
|
2020-02-18 21:14:29 +01:00
|
|
|
types = str(note_type).lower()
|
|
|
|
if "user" in types:
|
2020-02-17 11:19:16 +01:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="noteuser")
|
2020-02-18 21:14:29 +01:00
|
|
|
elif "club" in types:
|
2020-02-17 11:19:16 +01:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="noteclub")
|
2020-02-18 21:14:29 +01:00
|
|
|
elif "special" in types:
|
2020-02-17 11:19:16 +01:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="notespecial")
|
2020-02-08 23:24:49 +01:00
|
|
|
else:
|
|
|
|
qs = qs.none()
|
|
|
|
|
2020-02-08 20:23:17 +01:00
|
|
|
return qs
|
|
|
|
|
2020-02-17 01:13:14 +01:00
|
|
|
def get_result_label(self, result):
|
2020-03-23 15:28:09 +01:00
|
|
|
"""
|
|
|
|
Show the selected alias and the username associated
|
|
|
|
<Alias> (aka. <Username> )
|
|
|
|
"""
|
2020-02-17 11:36:46 +01:00
|
|
|
# Gère l'affichage de l'alias dans la recherche
|
2020-02-17 11:19:16 +01:00
|
|
|
res = result.name
|
|
|
|
note_name = str(result.note)
|
|
|
|
if res != note_name:
|
|
|
|
res += " (aka. " + note_name + ")"
|
2020-02-17 01:13:14 +01:00
|
|
|
return res
|
|
|
|
|
2020-02-17 11:19:16 +01:00
|
|
|
def get_result_value(self, result):
|
2020-03-23 15:28:09 +01:00
|
|
|
"""
|
|
|
|
The value used for the transactions will be the id of the Note.
|
|
|
|
"""
|
2020-02-17 11:19:16 +01:00
|
|
|
return str(result.note.pk)
|
|
|
|
|
2020-02-08 20:23:17 +01:00
|
|
|
|
2020-02-18 12:31:15 +01:00
|
|
|
class TransactionTemplateCreateView(LoginRequiredMixin, CreateView):
|
2019-08-11 19:54:18 +02:00
|
|
|
"""
|
|
|
|
Create TransactionTemplate
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
|
|
|
form_class = TransactionTemplateForm
|
2020-03-24 22:13:15 +01:00
|
|
|
success_url = reverse_lazy('note:template_list')
|
2020-02-18 12:31:15 +01:00
|
|
|
|
2020-03-09 20:59:04 +01:00
|
|
|
class TransactionTemplateListView(LoginRequiredMixin, SingleTableView):
|
2019-08-11 19:54:18 +02:00
|
|
|
"""
|
|
|
|
List TransactionsTemplates
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
2020-03-10 20:17:56 +01:00
|
|
|
table_class = ButtonTable
|
2019-08-11 23:24:54 +02:00
|
|
|
|
2020-03-23 20:21:25 +01:00
|
|
|
def get_queryset(self):
|
|
|
|
name = self.request.GET.get('name','')
|
|
|
|
if (name != ''):
|
|
|
|
object_list = self.model.objects.filter(name__icontains = name)
|
|
|
|
else:
|
|
|
|
object_list = self.model.objects.all()
|
|
|
|
return object_list
|
|
|
|
|
2020-02-18 12:31:15 +01:00
|
|
|
|
|
|
|
class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
|
2019-08-11 19:54:18 +02:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
2020-02-04 01:18:03 +01:00
|
|
|
form_class = TransactionTemplateForm
|
2020-03-24 23:32:48 +01:00
|
|
|
success_url = reverse_lazy('note:template_list')
|
2020-02-18 12:31:15 +01:00
|
|
|
|
2020-03-10 08:07:09 +01:00
|
|
|
class ConsoView(LoginRequiredMixin, SingleTableView):
|
2020-02-04 01:18:03 +01:00
|
|
|
"""
|
2020-03-23 15:28:09 +01:00
|
|
|
The Magic View that make people pay their beer and burgers.
|
|
|
|
(Most of the magic happens in the dark world of Javascript see consos.js)
|
2020-02-04 01:18:03 +01:00
|
|
|
"""
|
|
|
|
template_name = "note/conso_form.html"
|
2020-03-10 08:07:09 +01:00
|
|
|
|
|
|
|
# Transaction history table
|
|
|
|
table_class = HistoryTable
|
2020-03-13 10:29:27 +01:00
|
|
|
table_pagination = {"per_page": 50}
|
2020-02-04 01:18:03 +01:00
|
|
|
|
2020-03-19 02:26:06 +01:00
|
|
|
def get_queryset(self):
|
2020-03-20 02:20:13 +01:00
|
|
|
return Transaction.objects.filter(
|
|
|
|
PermissionBackend.filter_queryset(self.request.user, Transaction, "view")
|
|
|
|
).order_by("-id").all()[:50]
|
2020-02-04 01:18:03 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Add some context variables in template such as page title
|
|
|
|
"""
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-14 19:00:20 +01:00
|
|
|
from django.db.models import Count
|
2020-03-20 02:14:43 +01:00
|
|
|
buttons = TransactionTemplate.objects.filter(
|
|
|
|
PermissionBackend().filter_queryset(self.request.user, TransactionTemplate, "view")
|
|
|
|
).filter(display=True).annotate(clicks=Count('recurrenttransaction')).order_by('category__name', 'name')
|
2020-03-14 19:00:20 +01:00
|
|
|
context['transaction_templates'] = buttons
|
|
|
|
context['most_used'] = buttons.order_by('-clicks', 'name')[:10]
|
2020-03-11 12:05:29 +01:00
|
|
|
context['title'] = _("Consumptions")
|
2020-03-19 20:37:48 +01:00
|
|
|
context['polymorphic_ctype'] = ContentType.objects.get_for_model(RecurrentTransaction).pk
|
2020-02-04 01:18:03 +01:00
|
|
|
|
2020-02-22 11:09:45 +01:00
|
|
|
# select2 compatibility
|
2020-02-21 19:46:56 +01:00
|
|
|
context['no_cache'] = True
|
|
|
|
|
2020-02-04 01:18:03 +01:00
|
|
|
return context
|