125 lines
4.5 KiB
Python
125 lines
4.5 KiB
Python
# chrono - agendas 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.forms import ValidationError
|
|
from django.template import Template, TemplateSyntaxError
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from chrono.pricing.models import AgendaPricing, Criteria, CriteriaCategory
|
|
|
|
|
|
class NewCriteriaForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Criteria
|
|
fields = ['label', 'condition']
|
|
|
|
def clean_condition(self):
|
|
condition = self.cleaned_data['condition']
|
|
try:
|
|
Template('{%% if %s %%}OK{%% endif %%}' % condition)
|
|
except TemplateSyntaxError:
|
|
raise ValidationError(_('Invalid syntax.'))
|
|
|
|
return condition
|
|
|
|
|
|
class CriteriaForm(NewCriteriaForm):
|
|
class Meta:
|
|
model = Criteria
|
|
fields = ['label', 'slug', 'condition']
|
|
|
|
def clean_slug(self):
|
|
slug = self.cleaned_data['slug']
|
|
|
|
if self.instance.category.criterias.filter(slug=slug).exclude(pk=self.instance.pk).exists():
|
|
raise ValidationError(_('Another criteria exists with the same identifier.'))
|
|
|
|
return slug
|
|
|
|
|
|
class PricingVariableForm(forms.Form):
|
|
key = forms.CharField(label=_('Variable name'), required=False)
|
|
value = forms.CharField(
|
|
label=_('Value template'), widget=forms.TextInput(attrs={'size': 60}), required=False
|
|
)
|
|
|
|
|
|
PricingVariableFormSet = forms.formset_factory(PricingVariableForm)
|
|
|
|
|
|
class PricingCriteriaCategoryAddForm(forms.Form):
|
|
category = forms.ModelChoiceField(
|
|
label=_('Criteria category to add'), queryset=CriteriaCategory.objects.none(), required=True
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.pricing = kwargs.pop('pricing')
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['category'].queryset = CriteriaCategory.objects.exclude(pricings=self.pricing)
|
|
|
|
|
|
class PricingCriteriaCategoryEditForm(forms.Form):
|
|
criterias = forms.ModelMultipleChoiceField(
|
|
label=_('Criterias'),
|
|
queryset=Criteria.objects.none(),
|
|
required=True,
|
|
widget=forms.CheckboxSelectMultiple(),
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.pricing = kwargs.pop('pricing')
|
|
self.category = kwargs.pop('category')
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['criterias'].queryset = self.category.criterias.all()
|
|
self.initial['criterias'] = self.pricing.criterias.filter(category=self.category)
|
|
|
|
|
|
class AgendaPricingForm(forms.ModelForm):
|
|
class Meta:
|
|
model = AgendaPricing
|
|
fields = ['pricing', 'date_start', 'date_end']
|
|
widgets = {
|
|
'date_start': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
|
|
'date_end': 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:
|
|
if cleaned_data['date_end'] <= cleaned_data['date_start']:
|
|
self.add_error('date_end', _('End date must be greater than start date.'))
|
|
else:
|
|
overlapping_qs = AgendaPricing.objects.filter(agenda=self.instance.agenda,).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():
|
|
raise forms.ValidationError(_('Pricing overlaps existing pricings.'))
|
|
|
|
return cleaned_data
|
|
|
|
|
|
class PricingMatrixForm(forms.Form):
|
|
def __init__(self, *args, **kwargs):
|
|
matrix = kwargs.pop('matrix')
|
|
super().__init__(*args, **kwargs)
|
|
for i in range(len(matrix.rows[0].cells)):
|
|
self.fields['crit_%i' % i] = forms.DecimalField(required=True, max_digits=5, decimal_places=2)
|