Skip to content
Snippets Groups Projects
views.py 4.67 KiB
Newer Older
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

ynerant's avatar
ynerant committed
from dal import autocomplete
from django.contrib.auth.mixins import LoginRequiredMixin
ynerant's avatar
ynerant committed
from django.db.models import Q
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
from django.views.generic import CreateView, ListView, UpdateView
from .models import Transaction, TransactionTemplate, Alias
ynerant's avatar
ynerant committed
from .forms import TransactionForm, TransactionTemplateForm, ConsoForm
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed

class TransactionCreate(LoginRequiredMixin, CreateView):
    """
    Show transfer page

    TODO: If user have sufficient rights, they can transfer from an other note
    """
    model = Transaction
ynerant's avatar
ynerant committed
    form_class = TransactionForm

    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
    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)

me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
        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.
        """
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
        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)


ynerant's avatar
ynerant committed
class NoteAutocomplete(autocomplete.Select2QuerySetView):
    """
    Auto complete note by aliases
    """
    def get_queryset(self):
ynerant's avatar
ynerant committed
        """
        Quand une personne cherche un alias, une requête est envoyée sur l'API dédiée à l'auto-complétion.
ynerant's avatar
ynerant committed
        Cette fonction récupère la requête, et renvoie la liste filtrée des aliases.
ynerant's avatar
ynerant committed
        """
        #  Un utilisateur non connecté n'a accès à aucune information
        if not self.request.user.is_authenticated:
            return Alias.objects.none()
        qs = Alias.objects.all()
ynerant's avatar
ynerant committed

ynerant's avatar
ynerant committed
        if self.q:
            qs = qs.filter(Q(name__regex=self.q) | Q(normalized_name__regex=Alias.normalize(self.q)))\
                .order_by('normalized_name').distinct()
ynerant's avatar
ynerant committed

        # Filtrage par type de note (user, club, special)
        note_type = self.forwarded.get("note_type", None)
        if note_type:
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
            types = str(note_type).lower()
            if "user" in types:
                qs = qs.filter(note__polymorphic_ctype__model="noteuser")
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
            elif "club" in types:
                qs = qs.filter(note__polymorphic_ctype__model="noteclub")
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
            elif "special" in types:
                qs = qs.filter(note__polymorphic_ctype__model="notespecial")
ynerant's avatar
ynerant committed
        return qs

    def get_result_label(self, result):
ynerant's avatar
ynerant committed
        # Gère l'affichage de l'alias dans la recherche
        res = result.name
        note_name = str(result.note)
        if res != note_name:
            res += " (aka. " + note_name + ")"
    def get_result_value(self, result):
ynerant's avatar
ynerant committed
        # Le résultat renvoyé doit être l'identifiant de la note, et non de l'alias
        return str(result.note.pk)

ynerant's avatar
ynerant committed

me5na7qbjqbrp's avatar
me5na7qbjqbrp committed
class TransactionTemplateCreateView(LoginRequiredMixin, CreateView):
    """
    Create TransactionTemplate
    """
    model = TransactionTemplate
    form_class = TransactionTemplateForm
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed

class TransactionTemplateListView(LoginRequiredMixin, ListView):
    """
    List TransactionsTemplates
    """
    model = TransactionTemplate
    form_class = TransactionTemplateForm
me5na7qbjqbrp's avatar
me5na7qbjqbrp committed

class TransactionTemplateUpdateView(LoginRequiredMixin, UpdateView):
    """
    """
    model = TransactionTemplate
ynerant's avatar
ynerant committed
    form_class = TransactionTemplateForm

me5na7qbjqbrp's avatar
me5na7qbjqbrp committed

class ConsoView(LoginRequiredMixin, CreateView):
ynerant's avatar
ynerant committed
    """
    Consume
    """
    model = Transaction
    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)
        context['transaction_templates'] = TransactionTemplate.objects.all() \
            .order_by('template_type')
        context['title'] = _("Consommations")
ynerant's avatar
ynerant committed
        return context

    def get_success_url(self):
        """
        When clicking a button, reload the same page
        """
        return reverse('note:consos')