Skip to content
Snippets Groups Projects

Feature/provenance

Merged Carlos Vega requested to merge feature/provenance into master
All threads resolved!
@@ -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__)
@@ -32,38 +32,39 @@ class AppointmentForm(ModelForm):
def register_changes(self):
self.changes = []
for field in self.changed_data:
new_value = self.cleaned_data[field]
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
@@ -105,11 +106,13 @@ class AppointmentEditForm(AppointmentForm):
location = self.cleaned_data['location']
if self.user.locations.filter(id=location.id).count() == 0:
self.add_error('location', "You cannot create appointment for this location")
raise forms.ValidationError("You cannot create appointment for this location")
else:
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):
@@ -134,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
@@ -164,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']
Loading