1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-01-23 08:31:17 +00:00
nk20/apps/note/forms.py

132 lines
4.4 KiB
Python
Raw Normal View History

2020-02-18 21:30:26 +01:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2019-08-11 19:55:04 +02:00
2020-02-18 21:14:29 +01:00
from dal import autocomplete
2019-08-11 19:55:04 +02:00
from django import forms
2020-02-27 17:22:59 +01:00
from django.utils.translation import gettext_lazy as _
2020-02-18 21:14:29 +01:00
2020-03-07 22:28:59 +01:00
from .models import Alias
2020-02-24 10:36:04 +01:00
from .models import Transaction, TransactionTemplate, TemplateTransaction
2020-03-07 22:28:59 +01:00
2020-02-28 13:37:31 +01:00
class AliasForm(forms.ModelForm):
class Meta:
model = Alias
fields = ("name",)
2020-03-03 14:26:31 +01:00
2020-03-07 22:28:59 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2020-03-01 13:42:22 +01:00
self.fields["name"].label = False
2020-03-07 22:28:59 +01:00
self.fields["name"].widget.attrs = {"placeholder": _('New Alias')}
2020-03-03 14:26:31 +01:00
2020-03-04 16:34:12 +01:00
class ImageForm(forms.Form):
2020-03-07 22:28:59 +01:00
image = forms.ImageField(required=False,
2020-03-04 16:34:12 +01:00
label=_('select an image'),
help_text=_('Maximal size: 2MB'))
2020-03-05 23:32:01 +01:00
x = forms.FloatField(widget=forms.HiddenInput())
y = forms.FloatField(widget=forms.HiddenInput())
width = forms.FloatField(widget=forms.HiddenInput())
height = forms.FloatField(widget=forms.HiddenInput())
2020-03-03 14:26:31 +01:00
2020-03-07 22:28:59 +01:00
2019-08-11 19:55:04 +02:00
class TransactionTemplateForm(forms.ModelForm):
class Meta:
model = TransactionTemplate
2020-02-18 12:31:15 +01:00
fields = '__all__'
2020-02-08 20:23:17 +01:00
2020-02-08 21:40:32 +01:00
# Le champ de destination est remplacé par un champ d'auto-complétion.
# Quand des lettres sont tapées, une requête est envoyée sur l'API d'auto-complétion
# et récupère les aliases valides
# Pour force le type d'une note, il faut rajouter le paramètre :
# forward=(forward.Const('TYPE', 'note_type') où TYPE est dans {user, club, special}
2020-02-08 20:23:17 +01:00
widgets = {
2020-02-18 12:31:15 +01:00
'destination':
2020-03-07 22:28:59 +01:00
autocomplete.ModelSelect2(
url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
},
),
2020-02-08 20:23:17 +01:00
}
class TransactionForm(forms.ModelForm):
def save(self, commit=True):
super().save(commit)
2020-02-27 17:22:59 +01:00
def clean(self):
"""
If the user has no right to transfer funds, then it will be the source of the transfer by default.
Transactions between a note and the same note are not authorized.
"""
cleaned_data = super().clean()
2020-03-07 22:28:59 +01:00
if "source" not in cleaned_data: # TODO Replace it with "if %user has no right to transfer funds"
2020-02-27 17:22:59 +01:00
cleaned_data["source"] = self.user.note
if cleaned_data["source"].pk == cleaned_data["destination"].pk:
self.add_error("destination", _("Source and destination must be different."))
return cleaned_data
2020-02-08 20:23:17 +01:00
class Meta:
model = Transaction
2020-02-18 12:31:15 +01:00
fields = (
'source',
'destination',
'reason',
'amount',
)
2020-02-08 20:23:17 +01:00
2020-02-08 21:40:32 +01:00
# Voir ci-dessus
2020-02-08 20:23:17 +01:00
widgets = {
2020-02-18 12:31:15 +01:00
'source':
2020-03-07 22:28:59 +01:00
autocomplete.ModelSelect2(
url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
},
),
2020-02-18 12:31:15 +01:00
'destination':
2020-03-07 22:28:59 +01:00
autocomplete.ModelSelect2(
url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
},
),
2020-02-08 20:23:17 +01:00
}
2020-02-16 23:27:59 +01:00
2020-02-18 12:31:15 +01:00
class ConsoForm(forms.ModelForm):
2020-02-04 01:18:03 +01:00
def save(self, commit=True):
2020-02-18 12:31:15 +01:00
button: TransactionTemplate = TransactionTemplate.objects.filter(
name=self.data['button']).get()
2020-02-04 01:18:03 +01:00
self.instance.destination = button.destination
self.instance.amount = button.amount
2020-02-24 10:36:04 +01:00
self.instance.reason = '{} ({})'.format(button.name, button.category)
self.instance.name = button.name
self.instance.category = button.category
2020-02-04 01:18:03 +01:00
super().save(commit)
class Meta:
2020-02-24 10:36:04 +01:00
model = TemplateTransaction
2020-03-07 22:28:59 +01:00
fields = ('source',)
2020-02-16 23:27:59 +01:00
# Le champ d'utilisateur est remplacé par un champ d'auto-complétion.
# Quand des lettres sont tapées, une requête est envoyée sur l'API d'auto-complétion
# et récupère les aliases de note valides
widgets = {
2020-02-18 12:31:15 +01:00
'source':
2020-03-07 22:28:59 +01:00
autocomplete.ModelSelect2(
url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
},
),
2020-02-16 23:27:59 +01:00
}