From 0e171fbc8ff9ea6bcbb5830bb37f0709f571b906 Mon Sep 17 00:00:00 2001 From: Krophil Date: Fri, 9 Jun 2017 16:04:24 +0200 Subject: [PATCH] add some validationErrors on OperationForm --- accounting/models.py | 4 +++- accounting/views.py | 25 ++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/accounting/models.py b/accounting/models.py index d408ecfb..c6b65551 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -282,7 +282,9 @@ class Operation(models.Model): def clean(self): super(Operation, self).clean() - if self.date < self.journal.start_date: + if self.date is None: + raise ValidationError(_("The date must be set.")) + elif self.date < self.journal.start_date: raise ValidationError(_("""The date can not be before the start date of the journal, which is %(start_date)s.""") % {'start_date': defaultfilters.date(self.journal.start_date, settings.DATE_FORMAT)}) if self.target_type != "OTHER" and self.get_target() is None: diff --git a/accounting/views.py b/accounting/views.py index 76452292..8cb8c87a 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -28,7 +28,7 @@ from django.shortcuts import render from django.core.urlresolvers import reverse_lazy, reverse from django.utils.translation import ugettext_lazy as _ from django.forms.models import modelform_factory -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, ValidationError from django.forms import HiddenInput, TextInput from django.db import transaction from django.db.models import Sum @@ -305,14 +305,21 @@ class OperationForm(forms.ModelForm): def clean(self): self.cleaned_data = super(OperationForm, self).clean() if 'target_type' in self.cleaned_data.keys(): - if self.cleaned_data['target_type'] == "USER": - self.cleaned_data['target_id'] = self.cleaned_data['user'].id - elif self.cleaned_data['target_type'] == "ACCOUNT": - self.cleaned_data['target_id'] = self.cleaned_data['club_account'].id - elif self.cleaned_data['target_type'] == "CLUB": - self.cleaned_data['target_id'] = self.cleaned_data['club'].id - elif self.cleaned_data['target_type'] == "COMPANY": - self.cleaned_data['target_id'] = self.cleaned_data['company'].id + if self.cleaned_data.get("user") is None or self.cleaned_data.get("club") or self.cleaned_data.get("club_account") is None or self.cleaned_data.get("company") is None or self.cleaned_data.get("other"): + self.add_error('target_id', ValidationError(_("The target must be set."))) + else: + if self.cleaned_data['target_type'] == "USER": + self.cleaned_data['target_id'] = self.cleaned_data['user'].id + elif self.cleaned_data['target_type'] == "ACCOUNT": + self.cleaned_data['target_id'] = self.cleaned_data['club_account'].id + elif self.cleaned_data['target_type'] == "CLUB": + self.cleaned_data['target_id'] = self.cleaned_data['club'].id + elif self.cleaned_data['target_type'] == "COMPANY": + self.cleaned_data['target_id'] = self.cleaned_data['company'].id + + if self.cleaned_data.get("amount") is None: + self.add_error('amount', ValidationError(_("The amount must be set."))) + return self.cleaned_data def save(self):