diff --git a/smash/web/forms/appointment_form.py b/smash/web/forms/appointment_form.py index 2bc99e48341054ee355dcd6438a598101a5500ba..9a45e77c16678765584a9445147e5e77ab61891f 100644 --- a/smash/web/forms/appointment_form.py +++ b/smash/web/forms/appointment_form.py @@ -2,13 +2,13 @@ import logging from collections import OrderedDict from django import forms +from django.db.models.query import QuerySet from django.forms import ModelForm -from web.models.worker_study_role import WORKER_STAFF from web.forms.forms import DATETIMEPICKER_DATE_ATTRS, APPOINTMENT_TYPES_FIELD_POSITION from web.models import Appointment, Worker, AppointmentTypeLink, AppointmentType, Provenance +from web.models.worker_study_role import WORKER_STAFF from web.views.notifications import get_filter_locations -from django.db.models.query import QuerySet logger = logging.getLogger(__name__) @@ -34,36 +34,37 @@ class AppointmentForm(ModelForm): for field in self.changed_data: new_value = self.cleaned_data[field] or self.data[field] if isinstance(new_value, QuerySet): - new_human_values = '; '.join([str(element) for element in new_value]) - new_value = ','.join([str(element.id) for element in new_value]) #overwrite variable - #old value - if self.instance.id: #update instance + new_human_values = '; '.join([str(element) for element in new_value]) + new_value = ','.join([str(element.id) for element in new_value]) # overwrite variable + # old value + if self.instance.id: # update instance previous_value = getattr(self.instance, field).all() - old_human_values = '; '.join([str(element) for element in previous_value]) - previous_value = ','.join([str(element.id) for element in previous_value]) #overwrite variable - else: #new instance + old_human_values = '; '.join([str(element) for element in previous_value]) + previous_value = ','.join([str(element.id) for element in previous_value]) # overwrite variable + else: # new instance old_human_values = '' previous_value = '' - #description + # description description = '{} changed from "{}" to "{}"'.format(field, old_human_values, new_human_values) else: - if self.instance.id: #update instance + if self.instance.id: # update instance previous_value = str(getattr(self.instance, field)) else: previous_value = '' new_value = str(self.cleaned_data[field]) description = '{} changed from "{}" to "{}"'.format(field, previous_value, new_value) - - p = Provenance(modified_table = Appointment._meta.db_table, - modified_table_id = self.instance.id, - modification_author=self.user, - previous_value = previous_value, - new_value = new_value, - modification_description = description, - modified_field = field, - ) + + p = Provenance(modified_table=Appointment._meta.db_table, + modified_table_id=self.instance.id, + modification_author=self.user, + previous_value=previous_value, + new_value=new_value, + modification_description=description, + modified_field=field, + ) self.changes.append(p) + class AppointmentDetailForm(AppointmentForm): class Meta: model = Appointment @@ -110,7 +111,8 @@ class AppointmentEditForm(AppointmentForm): return location def clean(self): - self.register_changes() #right before instance is changed + if len(self.errors) == 0: + self.register_changes() # right before instance is changed def save(self, commit=True): @@ -135,7 +137,7 @@ class AppointmentEditForm(AppointmentForm): appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type) appointment_type_link.save() - + self.save_changes() return appointment @@ -165,14 +167,15 @@ class AppointmentAddForm(AppointmentForm): ) fields['worker_assigned'].widget.attrs = {'class': 'search_worker_availability'} fields['datetime_when'].widget.attrs = {'class': 'start_date', 'placeholder': 'yyyy-mm-dd HH:MM', - 'pattern': '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}'} + 'pattern': '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}'} fields['length'].widget.attrs = {'class': 'appointment_duration'} self.fields = fields self.fields['location'].queryset = get_filter_locations(self.user) def clean(self): - self.register_changes() #right before instance is changed + if len(self.errors) == 0: + self.register_changes() # right before instance is changed def clean_location(self): location = self.cleaned_data['location']