2020-02-18 20:30:26 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
2019-07-17 11:34:07 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
from dal import autocomplete
|
2019-07-17 11:53:58 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-02-08 19:23:17 +00:00
|
|
|
from django.db.models import Q
|
2020-02-18 20:14:29 +00:00
|
|
|
from django.urls import reverse
|
2019-07-17 11:34:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-02-18 20:14:29 +00:00
|
|
|
from django.views.generic import CreateView, ListView, UpdateView
|
2019-07-17 11:34:07 +00:00
|
|
|
|
2020-03-09 19:59:04 +00:00
|
|
|
from django_tables2 import SingleTableView
|
|
|
|
|
2020-02-16 21:25:00 +00:00
|
|
|
from .forms import TransactionForm, TransactionTemplateForm, ConsoForm
|
2020-03-07 21:28:59 +00:00
|
|
|
from .models import Transaction, TransactionTemplate, Alias, TemplateTransaction
|
2020-03-09 19:59:04 +00:00
|
|
|
from .tables import ButtonTable
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-23 14:28:09 +00:00
|
|
|
class TransactionCreateView(LoginRequiredMixin, SingleTableView):
|
2019-07-17 11:34:07 +00:00
|
|
|
"""
|
2020-03-23 14:28:09 +00: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 11:34:07 +00:00
|
|
|
"""
|
2020-03-23 14:28:09 +00:00
|
|
|
template_name = "note/transaction_form.html"
|
|
|
|
|
2019-07-17 11:53:58 +00:00
|
|
|
model = Transaction
|
2020-02-08 19:23:17 +00:00
|
|
|
form_class = TransactionForm
|
2019-07-17 11:53:58 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Add some context variables in template such as page title
|
|
|
|
"""
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['title'] = _('Transfer money from your account '
|
|
|
|
'to one or others')
|
2020-02-21 17:28:21 +00:00
|
|
|
|
|
|
|
context['no_cache'] = True
|
|
|
|
|
2019-07-17 11:53:58 +00:00
|
|
|
return context
|
2019-08-11 17:54:18 +00:00
|
|
|
|
2020-02-08 22:24:49 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
"""
|
|
|
|
If the user has no right to transfer funds, then it won't have the choice of the source of the transfer.
|
|
|
|
"""
|
|
|
|
form = super().get_form(form_class)
|
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
2020-02-08 22:24:49 +00:00
|
|
|
del form.fields['source']
|
2020-02-27 16:22:59 +00:00
|
|
|
form.user = self.request.user
|
2020-02-08 22:24:49 +00:00
|
|
|
|
|
|
|
return form
|
|
|
|
|
2020-02-27 16:22:59 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('note:transfer')
|
2020-02-08 22:24:49 +00:00
|
|
|
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
|
|
|
"""
|
2020-03-23 14:28:09 +00:00
|
|
|
Auto complete note by aliases. Used in every search field for note
|
|
|
|
ex: :view:`ConsoView`, :view:`TransactionCreateView`
|
2020-02-08 19:23:17 +00:00
|
|
|
"""
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
def get_queryset(self):
|
2020-02-08 20:40:32 +00:00
|
|
|
"""
|
2020-03-23 14:28:09 +00: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 20:40:32 +00:00
|
|
|
"""
|
2020-02-08 22:24:49 +00:00
|
|
|
# Un utilisateur non connecté n'a accès à aucune information
|
|
|
|
if not self.request.user.is_authenticated:
|
2020-02-17 10:19:16 +00:00
|
|
|
return Alias.objects.none()
|
2020-02-08 22:24:49 +00:00
|
|
|
|
2020-02-17 10:19:16 +00:00
|
|
|
qs = Alias.objects.all()
|
2020-02-08 19:23:17 +00:00
|
|
|
|
2020-02-08 22:24:49 +00:00
|
|
|
# self.q est le paramètre de la recherche
|
2020-02-08 19:23:17 +00:00
|
|
|
if self.q:
|
2020-03-07 21:28:59 +00:00
|
|
|
qs = qs.filter(Q(name__regex=self.q) | Q(normalized_name__regex=Alias.normalize(self.q))) \
|
2020-02-17 10:19:16 +00:00
|
|
|
.order_by('normalized_name').distinct()
|
2020-02-08 19:23:17 +00:00
|
|
|
|
2020-02-08 22:24:49 +00:00
|
|
|
# Filtrage par type de note (user, club, special)
|
|
|
|
note_type = self.forwarded.get("note_type", None)
|
|
|
|
if note_type:
|
2020-02-18 20:14:29 +00:00
|
|
|
types = str(note_type).lower()
|
|
|
|
if "user" in types:
|
2020-02-17 10:19:16 +00:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="noteuser")
|
2020-02-18 20:14:29 +00:00
|
|
|
elif "club" in types:
|
2020-02-17 10:19:16 +00:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="noteclub")
|
2020-02-18 20:14:29 +00:00
|
|
|
elif "special" in types:
|
2020-02-17 10:19:16 +00:00
|
|
|
qs = qs.filter(note__polymorphic_ctype__model="notespecial")
|
2020-02-08 22:24:49 +00:00
|
|
|
else:
|
|
|
|
qs = qs.none()
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
return qs
|
|
|
|
|
2020-02-17 00:13:14 +00:00
|
|
|
def get_result_label(self, result):
|
2020-03-23 14:28:09 +00:00
|
|
|
"""
|
|
|
|
Show the selected alias and the username associated
|
|
|
|
<Alias> (aka. <Username> )
|
|
|
|
"""
|
2020-02-17 10:36:46 +00:00
|
|
|
# Gère l'affichage de l'alias dans la recherche
|
2020-02-17 10:19:16 +00:00
|
|
|
res = result.name
|
|
|
|
note_name = str(result.note)
|
|
|
|
if res != note_name:
|
|
|
|
res += " (aka. " + note_name + ")"
|
2020-02-17 00:13:14 +00:00
|
|
|
return res
|
|
|
|
|
2020-02-17 10:19:16 +00:00
|
|
|
def get_result_value(self, result):
|
2020-03-23 14:28:09 +00:00
|
|
|
"""
|
|
|
|
The value used for the transactions will be the id of the Note.
|
|
|
|
"""
|
2020-02-17 10:19:16 +00:00
|
|
|
return str(result.note.pk)
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
class TransactionTemplateCreateView(LoginRequiredMixin, CreateView):
|
2019-08-11 17:54:18 +00:00
|
|
|
"""
|
|
|
|
Create TransactionTemplate
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
|
|
|
form_class = TransactionTemplateForm
|
2019-08-11 21:24:54 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
2020-03-09 19:59:04 +00:00
|
|
|
class TransactionTemplateListView(LoginRequiredMixin, SingleTableView):
|
2019-08-11 17:54:18 +00:00
|
|
|
"""
|
|
|
|
List TransactionsTemplates
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
2020-03-10 19:17:56 +00:00
|
|
|
table_class = ButtonTable
|
2019-08-11 21:24:54 +00:00
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
|
|
|
class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
|
2019-08-11 17:54:18 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
2020-02-04 00:18:03 +00:00
|
|
|
form_class = TransactionTemplateForm
|
|
|
|
|
2020-02-18 11:31:15 +00:00
|
|
|
|
|
|
|
class ConsoView(LoginRequiredMixin, CreateView):
|
2020-02-04 00:18:03 +00:00
|
|
|
"""
|
2020-03-23 14:28:09 +00: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 00:18:03 +00:00
|
|
|
"""
|
2020-02-24 09:36:04 +00:00
|
|
|
model = TemplateTransaction
|
2020-02-04 00:18:03 +00:00
|
|
|
template_name = "note/conso_form.html"
|
|
|
|
form_class = ConsoForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Add some context variables in template such as page title
|
|
|
|
"""
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-02-23 17:56:03 +00:00
|
|
|
context['transaction_templates'] = TransactionTemplate.objects.filter(display=True) \
|
|
|
|
.order_by('category')
|
2020-02-21 21:40:58 +00:00
|
|
|
context['title'] = _("Consommations")
|
2020-02-04 00:18:03 +00:00
|
|
|
|
2020-02-22 10:09:45 +00:00
|
|
|
# select2 compatibility
|
2020-02-21 18:46:56 +00:00
|
|
|
context['no_cache'] = True
|
|
|
|
|
2020-02-04 00:18:03 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2020-02-21 21:40:58 +00:00
|
|
|
"""
|
|
|
|
When clicking a button, reload the same page
|
|
|
|
"""
|
|
|
|
return reverse('note:consos')
|