2019-07-17 11:34:07 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
|
|
|
|
# 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
|
2019-07-17 11:34:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-08-11 21:24:54 +00:00
|
|
|
from django.views.generic import CreateView, ListView, DetailView, UpdateView
|
2019-07-17 11:34:07 +00:00
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
from .models import Transaction, TransactionTemplate, Note
|
|
|
|
from .forms import TransactionForm, TransactionTemplateForm
|
2019-07-17 11:53:58 +00:00
|
|
|
|
|
|
|
class TransactionCreate(LoginRequiredMixin, CreateView):
|
2019-07-17 11:34:07 +00:00
|
|
|
"""
|
|
|
|
Show transfer page
|
|
|
|
|
|
|
|
TODO: If user have sufficient rights, they can transfer from an other note
|
|
|
|
"""
|
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')
|
|
|
|
return context
|
2019-08-11 17:54:18 +00:00
|
|
|
|
2020-02-08 19:23:17 +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)
|
|
|
|
|
|
|
|
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
|
|
|
del form.fields['source']
|
|
|
|
|
|
|
|
return form
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""
|
|
|
|
If the user has no right to transfer funds, then it will be the source of the transfer by default.
|
|
|
|
"""
|
|
|
|
if False: # TODO: fix it with "if %user has no right to transfer funds"
|
|
|
|
form.instance.source = self.request.user.note
|
|
|
|
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
class NoteAutocomplete(autocomplete.Select2QuerySetView):
|
|
|
|
"""
|
|
|
|
Auto complete note by aliases
|
|
|
|
"""
|
2020-02-08 20:40:32 +00:00
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
def get_queryset(self):
|
2020-02-08 20:40:32 +00:00
|
|
|
"""
|
|
|
|
Quand une personne cherche un alias, une requête est envoyée sur l'API dédiée à l'auto-complétion.
|
|
|
|
Cette fonction récupère la requête, et renvoie la liste filtrée des notes par aliases.
|
|
|
|
"""
|
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:
|
|
|
|
return Note.objects.none()
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
qs = Note.objects.all()
|
|
|
|
|
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:
|
|
|
|
qs = qs.filter(Q(alias__name__regex=self.q) | Q(alias__normalized_name__regex=self.q))
|
|
|
|
|
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:
|
|
|
|
l = str(note_type).lower()
|
|
|
|
if "user" in l:
|
|
|
|
qs = qs.filter(polymorphic_ctype__model="noteuser")
|
|
|
|
elif "club" in l:
|
|
|
|
qs = qs.filter(polymorphic_ctype__model="noteclub")
|
|
|
|
elif "special" in l:
|
|
|
|
qs = qs.filter(polymorphic_ctype__model="notespecial")
|
|
|
|
else:
|
|
|
|
qs = qs.none()
|
|
|
|
|
2020-02-08 19:23:17 +00:00
|
|
|
return qs
|
|
|
|
|
|
|
|
|
2019-08-11 17:54:18 +00:00
|
|
|
class TransactionTemplateCreateView(LoginRequiredMixin,CreateView):
|
|
|
|
"""
|
|
|
|
Create TransactionTemplate
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
|
|
|
form_class = TransactionTemplateForm
|
2019-08-11 21:24:54 +00:00
|
|
|
|
2019-08-11 17:54:18 +00:00
|
|
|
class TransactionTemplateListView(LoginRequiredMixin,ListView):
|
|
|
|
"""
|
|
|
|
List TransactionsTemplates
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
|
|
|
form_class = TransactionTemplateForm
|
2019-08-11 21:24:54 +00:00
|
|
|
|
|
|
|
class TransactionTemplateUpdateView(LoginRequiredMixin,UpdateView):
|
2019-08-11 17:54:18 +00:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
model = TransactionTemplate
|
2019-08-11 21:24:54 +00:00
|
|
|
form_class=TransactionTemplateForm
|