Skip to content
Snippets Groups Projects

Feature/provenance

Merged Carlos Vega requested to merge feature/provenance into master
Files
26
@@ -2,11 +2,12 @@ import logging
@@ -2,11 +2,12 @@ import logging
from collections import OrderedDict
from collections import OrderedDict
from django import forms
from django import forms
 
from django.db.models.query import QuerySet
from django.forms import ModelForm
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.forms.forms import DATETIMEPICKER_DATE_ATTRS, APPOINTMENT_TYPES_FIELD_POSITION
from web.models import Appointment, Worker, AppointmentTypeLink, AppointmentType
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 web.views.notifications import get_filter_locations
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
@@ -22,6 +23,47 @@ class AppointmentForm(ModelForm):
@@ -22,6 +23,47 @@ class AppointmentForm(ModelForm):
self.fields['worker_assigned'].queryset = Worker.get_workers_by_worker_type(WORKER_STAFF).filter(
self.fields['worker_assigned'].queryset = Worker.get_workers_by_worker_type(WORKER_STAFF).filter(
locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name')
locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name')
 
def save_changes(self):
 
for change in self.changes:
 
if change.modified_table_id is None:
 
change.modified_table_id = self.instance.id
 
change.save()
 
 
def register_changes(self):
 
self.changes = []
 
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
 
list_of_values = getattr(self.instance, field).all()
 
old_human_values = '; '.join([str(element) for element in list_of_values])
 
previous_value = ','.join([str(element.id) for element in list_of_values])
 
else: #new instance
 
old_human_values = ''
 
previous_value = ''
 
# description
 
description = '{} changed from "{}" to "{}"'.format(field, old_human_values, new_human_values)
 
else:
 
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,
 
)
 
self.changes.append(p)
 
class AppointmentDetailForm(AppointmentForm):
class AppointmentDetailForm(AppointmentForm):
class Meta:
class Meta:
@@ -64,10 +106,16 @@ class AppointmentEditForm(AppointmentForm):
@@ -64,10 +106,16 @@ class AppointmentEditForm(AppointmentForm):
location = self.cleaned_data['location']
location = self.cleaned_data['location']
if self.user.locations.filter(id=location.id).count() == 0:
if self.user.locations.filter(id=location.id).count() == 0:
self.add_error('location', "You cannot create appointment for this location")
self.add_error('location', "You cannot create appointment for this location")
 
raise forms.ValidationError("You cannot create appointment for this location")
else:
else:
return location
return location
 
def clean(self):
 
if len(self.errors) == 0:
 
self.register_changes() # right before instance is changed
 
def save(self, commit=True):
def save(self, commit=True):
 
appointment = super(AppointmentEditForm, self).save(commit)
appointment = super(AppointmentEditForm, self).save(commit)
# if appointment date change, remove appointment_type links
# if appointment date change, remove appointment_type links
if 'datetime_when' in self.changed_data:
if 'datetime_when' in self.changed_data:
@@ -89,6 +137,8 @@ class AppointmentEditForm(AppointmentForm):
@@ -89,6 +137,8 @@ class AppointmentEditForm(AppointmentForm):
appointment_type_link = AppointmentTypeLink(appointment=appointment,
appointment_type_link = AppointmentTypeLink(appointment=appointment,
appointment_type=appointment_type)
appointment_type=appointment_type)
appointment_type_link.save()
appointment_type_link.save()
 
 
self.save_changes()
return appointment
return appointment
@@ -117,12 +167,16 @@ class AppointmentAddForm(AppointmentForm):
@@ -117,12 +167,16 @@ class AppointmentAddForm(AppointmentForm):
)
)
fields['worker_assigned'].widget.attrs = {'class': 'search_worker_availability'}
fields['worker_assigned'].widget.attrs = {'class': 'search_worker_availability'}
fields['datetime_when'].widget.attrs = {'class': 'start_date', 'placeholder': 'yyyy-mm-dd HH:MM',
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'}
fields['length'].widget.attrs = {'class': 'appointment_duration'}
self.fields = fields
self.fields = fields
self.fields['location'].queryset = get_filter_locations(self.user)
self.fields['location'].queryset = get_filter_locations(self.user)
 
def clean(self):
 
if len(self.errors) == 0:
 
self.register_changes() # right before instance is changed
 
def clean_location(self):
def clean_location(self):
location = self.cleaned_data['location']
location = self.cleaned_data['location']
if self.user.locations.filter(id=location.id).count() == 0:
if self.user.locations.filter(id=location.id).count() == 0:
@@ -136,4 +190,5 @@ class AppointmentAddForm(AppointmentForm):
@@ -136,4 +190,5 @@ class AppointmentAddForm(AppointmentForm):
for appointment_type in appointment_types:
for appointment_type in appointment_types:
appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type)
appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type)
appointment_type_link.save()
appointment_type_link.save()
 
self.save_changes()
return appointment
return appointment
Loading