1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-27 02:43:01 +00:00
nk20/apps/note/views.py

107 lines
4.0 KiB
Python
Raw Normal View History

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
2019-07-17 11:53:58 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
2019-07-17 11:34:07 +00:00
from django.utils.translation import gettext_lazy as _
2020-03-24 23:12:56 +00:00
from django.views.generic import CreateView, UpdateView
from django_tables2 import SingleTableView
2020-03-23 19:21:25 +00:00
from django.urls import reverse_lazy
from note_kfet.inputs import AmountInput
2020-03-20 13:43:35 +00:00
from permission.backends import PermissionBackend
from permission.views import ProtectQuerysetMixin
2020-03-20 01:14:43 +00:00
2020-03-12 22:12:49 +00:00
from .forms import TransactionTemplateForm
from .models import Transaction, TransactionTemplate, RecurrentTransaction, NoteSpecial
2020-03-14 14:13:58 +00:00
from .models.transactions import SpecialTransaction
2020-03-23 14:35:24 +00:00
from .tables import HistoryTable, ButtonTable
2020-02-18 11:31:15 +00:00
class TransactionCreateView(ProtectQuerysetMixin, 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-03-16 11:11:16 +00:00
# Transaction history table
table_class = HistoryTable
def get_queryset(self, **kwargs):
return super().get_queryset(**kwargs).order_by("-id").all()[:50]
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')
context['amount_widget'] = AmountInput(attrs={"id": "amount"})
2020-03-12 22:12:49 +00:00
context['polymorphic_ctype'] = ContentType.objects.get_for_model(Transaction).pk
2020-03-14 14:13:58 +00:00
context['special_polymorphic_ctype'] = ContentType.objects.get_for_model(SpecialTransaction).pk
context['special_types'] = NoteSpecial.objects\
.filter(PermissionBackend.filter_queryset(self.request.user, NoteSpecial, "view"))\
.order_by("special_type").all()
2020-02-21 17:28:21 +00:00
2019-07-17 11:53:58 +00:00
return context
2019-08-11 17:54:18 +00:00
class TransactionTemplateCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
2019-08-11 17:54:18 +00:00
"""
Create TransactionTemplate
"""
model = TransactionTemplate
form_class = TransactionTemplateForm
2020-03-24 21:13:15 +00:00
success_url = reverse_lazy('note:template_list')
2020-02-18 11:31:15 +00:00
2020-03-24 23:03:48 +00:00
class TransactionTemplateListView(ProtectQuerysetMixin, 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
2020-02-18 11:31:15 +00:00
class TransactionTemplateUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
2019-08-11 17:54:18 +00:00
"""
"""
model = TransactionTemplate
2020-02-04 00:18:03 +00:00
form_class = TransactionTemplateForm
2020-03-24 22:32:48 +00:00
success_url = reverse_lazy('note:template_list')
2020-02-18 11:31:15 +00:00
2020-03-24 23:03:48 +00:00
class ConsoView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
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
"""
model = Transaction
2020-02-04 00:18:03 +00:00
template_name = "note/conso_form.html"
2020-03-10 07:07:09 +00:00
# Transaction history table
table_class = HistoryTable
2020-02-04 00:18:03 +00:00
def get_queryset(self, **kwargs):
return super().get_queryset(**kwargs).order_by("-id").all()[:50]
2020-02-04 00:18:03 +00: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 18:00:20 +00:00
from django.db.models import Count
2020-03-20 01:14:43 +00: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 18:00:20 +00:00
context['transaction_templates'] = buttons
context['most_used'] = buttons.order_by('-clicks', 'name')[:10]
2020-03-11 11:05:29 +00:00
context['title'] = _("Consumptions")
context['polymorphic_ctype'] = ContentType.objects.get_for_model(RecurrentTransaction).pk
2020-02-04 00:18:03 +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