8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-04 18:42:25 +00:00
re2o/deposits/forms.py

90 lines
3 KiB
Python
Raw Permalink Normal View History

2021-08-06 11:23:16 +00:00
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2021 Jean-Romain Garnier
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Deposit form
"""
from django import forms
from django.forms import Form, ModelForm
from django.utils.translation import ugettext_lazy as _
from re2o.mixins import FormRevMixin
from .models import Deposit, DepositItem
2021-08-06 12:18:38 +00:00
class DepositForm(FormRevMixin, ModelForm):
2021-08-06 11:23:16 +00:00
"""
Form used to manage and create an invoice and its fields.
"""
def __init__(self, *args, creation=False, **kwargs):
2021-08-06 13:09:01 +00:00
user = kwargs.pop("user")
2021-08-06 11:23:16 +00:00
super(DepositForm, self).__init__(*args, **kwargs)
# Set the fields' label to handle translation
self.fields["item"].label = _("Deposit item")
self.fields["payment_method"].label = _("Payment method")
self.fields["returned"].label = _("Deposit returned")
2021-08-07 11:23:06 +00:00
if creation:
# During creation, we only need to select the item and payment
# method, no need to add the "returned" checkbox
self.fields = {
"item": self.fields["item"],
"payment_method": self.fields["payment_method"],
}
2021-08-06 11:23:16 +00:00
class Meta:
model = Deposit
2021-08-07 11:23:06 +00:00
fields = ("item", "payment_method", "returned")
2021-08-06 11:23:16 +00:00
class DepositItemForm(FormRevMixin, ModelForm):
"""
Form used to create a deposit item.
"""
class Meta:
model = DepositItem
fields = "__all__"
class DelDepositItemForm(FormRevMixin, Form):
"""
Form used to delete one or more of the currently available deposit items.
The user must choose the one to delete by checking the boxes.
"""
deposit_items = forms.ModelMultipleChoiceField(
queryset=DepositItem.objects.none(),
label=_("Current deposit items"),
widget=forms.CheckboxSelectMultiple,
)
def __init__(self, *args, **kwargs):
instances = kwargs.pop("instances", None)
super(DelDepositItemForm, self).__init__(*args, **kwargs)
if instances:
self.fields["deposit_items"].queryset = instances
else:
self.fields["deposit_items"].queryset = DepositItem.objects.all()