lingo/lingo/invoicing/forms.py

55 lines
2.1 KiB
Python

# lingo - payment and billing system
# Copyright (C) 2022 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.utils.translation import ugettext_lazy as _
from lingo.invoicing.models import Campaign
class CampaignForm(forms.ModelForm):
class Meta:
model = Campaign
fields = ['date_start', 'date_end', 'date_issue']
widgets = {
'date_start': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
'date_end': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
'date_issue': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
}
def clean(self):
cleaned_data = super().clean()
if 'date_start' in cleaned_data and 'date_end' in cleaned_data:
overlapping_qs = Campaign.objects.extra(
where=["(date_start, date_end) OVERLAPS (%s, %s)"],
params=[cleaned_data['date_start'], cleaned_data['date_end']],
)
if self.instance.pk:
overlapping_qs = overlapping_qs.exclude(pk=self.instance.pk)
if overlapping_qs.exists():
self.add_error(None, _('Another campaign overlapping this period already exists.'))
return cleaned_data
class PoolForm(forms.Form):
draft = forms.BooleanField(label=_('Run a simulation'), initial=True, required=False)
def __init__(self, *args, **kwargs):
self.campaign = kwargs.pop('campaign')
super().__init__(*args, **kwargs)