diff --git a/smash/web/forms/__init__.py b/smash/web/forms/__init__.py index bc4628557d31ddcaa520bf771ef98cc736e8b6eb..ed536d27ba4831e44de5690522ef9ebb2de285ca 100644 --- a/smash/web/forms/__init__.py +++ b/smash/web/forms/__init__.py @@ -1,7 +1,8 @@ from worker_form import WorkerForm -from forms import AppointmentDetailForm, AppointmentEditForm, AppointmentAddForm, VisitDetailForm, \ +from forms import VisitDetailForm, \ VisitAddForm, ContactAttemptForm, ContactAttemptEditForm, KitRequestForm, StatisticsForm, AvailabilityAddForm, \ AvailabilityEditForm, HolidayAddForm +from appointment_form import AppointmentDetailForm, AppointmentEditForm, AppointmentAddForm from study_subject_forms import StudySubjectAddForm, StudySubjectDetailForm, StudySubjectEditForm from subject_forms import SubjectAddForm, SubjectEditForm, SubjectDetailForm from voucher_forms import VoucherTypeForm, VoucherTypePriceForm, VoucherForm diff --git a/smash/web/forms/appointment_form.py b/smash/web/forms/appointment_form.py new file mode 100644 index 0000000000000000000000000000000000000000..40038af8d7d930b99108cb78a8013886b8804629 --- /dev/null +++ b/smash/web/forms/appointment_form.py @@ -0,0 +1,135 @@ +from collections import OrderedDict + +from django import forms +from django.forms import ModelForm + +from web.forms.forms import DATETIMEPICKER_DATE_ATTRS, APPOINTMENT_TYPES_FIELD_POSITION +from web.models import Appointment, Worker, AppointmentTypeLink, AppointmentType +from web.views.notifications import get_filter_locations + + +class AppointmentDetailForm(ModelForm): + class Meta: + model = Appointment + fields = '__all__' + + datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', + widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) + ) + + +class AppointmentEditForm(ModelForm): + class Meta: + model = Appointment + fields = '__all__' + exclude = ['appointment_types'] + + datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', + widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) + ) + + def __init__(self, *args, **kwargs): + user = kwargs.pop('user', None) + if user is None: + raise TypeError("User not defined") + self.user = Worker.get_by_user(user) + if self.user is None: + raise TypeError("Worker not defined for: " + user.username) + super(AppointmentEditForm, self).__init__(*args, **kwargs) + if 'instance' in kwargs: + initial_appointment_types = AppointmentTypeLink.objects.filter(appointment=kwargs['instance']).values_list( + 'appointment_type', flat=True) + else: + initial_appointment_types = [] + fields = OrderedDict() + for i, tuple in enumerate(self.fields.items()): + key, value = tuple + fields[key] = value + if i == APPOINTMENT_TYPES_FIELD_POSITION: + fields['appointment_types'] = forms.ModelMultipleChoiceField(required=False, + widget=forms.CheckboxSelectMultiple, + queryset=AppointmentType.objects.all(), + initial=initial_appointment_types) + self.fields = fields + self.fields['worker_assigned'].queryset = Worker.objects.filter( + locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name') + self.fields['location'].queryset = get_filter_locations(self.user) + + def clean_location(self): + 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") + else: + return location + + def save(self, commit=True): + appointment = super(AppointmentEditForm, self).save(commit) + # if appointment date change, remove appointment_type links + if 'datetime_when' in self.changed_data: + AppointmentTypeLink.objects.filter(appointment=appointment).delete() + appointment_type_links = [] + else: + appointment_type_links = AppointmentTypeLink.objects.filter(appointment=appointment).all() + appointment_types_from_links = [] + appointment_types = self.cleaned_data['appointment_types'] + for appointment_type_link in appointment_type_links: + if appointment_type_link.appointment_type not in appointment_types: + # we delete appointment links for appointments types that have been removed + appointment_type_link.delete() + else: + appointment_types_from_links.append(appointment_type_link.appointment_type) + for appointment_type in appointment_types: + if appointment_type not in appointment_types_from_links: + # we create new appointment links for appointments types that have been added + appointment_type_link = AppointmentTypeLink(appointment=appointment, + appointment_type=appointment_type) + appointment_type_link.save() + return appointment + + +class AppointmentAddForm(ModelForm): + class Meta: + model = Appointment + exclude = ['status', 'appointment_types'] + + datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', + widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) + ) + + def __init__(self, *args, **kwargs): + user = kwargs.pop('user', None) + if user is None: + raise TypeError("User not defined") + self.user = Worker.get_by_user(user) + if self.user is None: + raise TypeError("Worker not defined for: " + user.username) + + super(ModelForm, self).__init__(*args, **kwargs) + fields = OrderedDict() + for i, tuple in enumerate(self.fields.items()): + key, value = tuple + fields[key] = value + if i == APPOINTMENT_TYPES_FIELD_POSITION: + fields['appointment_types'] = forms.ModelMultipleChoiceField(required=False, + widget=forms.CheckboxSelectMultiple, + queryset=AppointmentType.objects.all(), + ) + self.fields = fields + self.fields['worker_assigned'].queryset = Worker.objects.filter( + locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name') + self.fields['location'].queryset = get_filter_locations(self.user) + + def clean_location(self): + 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") + else: + return location + + def save(self, commit=True): + appointment = super(AppointmentAddForm, self).save(commit) + appointment_types = self.cleaned_data['appointment_types'] + for appointment_type in appointment_types: + appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type) + appointment_type_link.save() + return appointment \ No newline at end of file diff --git a/smash/web/forms/forms.py b/smash/web/forms/forms.py index fc36b7b0b2901d9bbadff08ac60ee8cc048b9c32..4774526f7edd5f92d65da4f15fe743195784dd36 100644 --- a/smash/web/forms/forms.py +++ b/smash/web/forms/forms.py @@ -1,15 +1,12 @@ import datetime import logging -from collections import OrderedDict from django import forms from django.forms import ModelForm, Form from django.utils.dates import MONTHS -from web.models import StudySubject, Worker, Appointment, Visit, AppointmentType, ContactAttempt, AppointmentTypeLink, \ - Availability, Holiday +from web.models import StudySubject, Worker, Visit, AppointmentType, ContactAttempt, Availability, Holiday from web.models.constants import SUBJECT_TYPE_CHOICES -from web.views.notifications import get_filter_locations """ Possible redundancy, but if need arises, contents of forms can be easily customized @@ -49,133 +46,6 @@ def get_worker_from_args(kwargs): return result -class AppointmentDetailForm(ModelForm): - class Meta: - model = Appointment - fields = '__all__' - - datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', - widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) - ) - - -class AppointmentEditForm(ModelForm): - class Meta: - model = Appointment - fields = '__all__' - exclude = ['appointment_types'] - - datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', - widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) - ) - - def __init__(self, *args, **kwargs): - user = kwargs.pop('user', None) - if user is None: - raise TypeError("User not defined") - self.user = Worker.get_by_user(user) - if self.user is None: - raise TypeError("Worker not defined for: " + user.username) - super(AppointmentEditForm, self).__init__(*args, **kwargs) - if 'instance' in kwargs: - initial_appointment_types = AppointmentTypeLink.objects.filter(appointment=kwargs['instance']).values_list( - 'appointment_type', flat=True) - else: - initial_appointment_types = [] - fields = OrderedDict() - for i, tuple in enumerate(self.fields.items()): - key, value = tuple - fields[key] = value - if i == APPOINTMENT_TYPES_FIELD_POSITION: - fields['appointment_types'] = forms.ModelMultipleChoiceField(required=False, - widget=forms.CheckboxSelectMultiple, - queryset=AppointmentType.objects.all(), - initial=initial_appointment_types) - self.fields = fields - self.fields['worker_assigned'].queryset = Worker.objects.filter( - locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name') - self.fields['location'].queryset = get_filter_locations(self.user) - - def clean_location(self): - 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") - else: - return location - - def save(self, commit=True): - appointment = super(AppointmentEditForm, self).save(commit) - # if appointment date change, remove appointment_type links - if 'datetime_when' in self.changed_data: - AppointmentTypeLink.objects.filter(appointment=appointment).delete() - appointment_type_links = [] - else: - appointment_type_links = AppointmentTypeLink.objects.filter(appointment=appointment).all() - appointment_types_from_links = [] - appointment_types = self.cleaned_data['appointment_types'] - for appointment_type_link in appointment_type_links: - if appointment_type_link.appointment_type not in appointment_types: - # we delete appointment links for appointments types that have been removed - appointment_type_link.delete() - else: - appointment_types_from_links.append(appointment_type_link.appointment_type) - for appointment_type in appointment_types: - if appointment_type not in appointment_types_from_links: - # we create new appointment links for appointments types that have been added - appointment_type_link = AppointmentTypeLink(appointment=appointment, - appointment_type=appointment_type) - appointment_type_link.save() - return appointment - - -class AppointmentAddForm(ModelForm): - class Meta: - model = Appointment - exclude = ['status', 'appointment_types'] - - datetime_when = forms.DateTimeField(label='Appointment on (YYYY-MM-DD HH:MM)', - widget=forms.DateTimeInput(DATETIMEPICKER_DATE_ATTRS) - ) - - def __init__(self, *args, **kwargs): - user = kwargs.pop('user', None) - if user is None: - raise TypeError("User not defined") - self.user = Worker.get_by_user(user) - if self.user is None: - raise TypeError("Worker not defined for: " + user.username) - - super(ModelForm, self).__init__(*args, **kwargs) - fields = OrderedDict() - for i, tuple in enumerate(self.fields.items()): - key, value = tuple - fields[key] = value - if i == APPOINTMENT_TYPES_FIELD_POSITION: - fields['appointment_types'] = forms.ModelMultipleChoiceField(required=False, - widget=forms.CheckboxSelectMultiple, - queryset=AppointmentType.objects.all(), - ) - self.fields = fields - self.fields['worker_assigned'].queryset = Worker.objects.filter( - locations__in=get_filter_locations(self.user)).distinct().order_by('first_name', 'last_name') - self.fields['location'].queryset = get_filter_locations(self.user) - - def clean_location(self): - 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") - else: - return location - - def save(self, commit=True): - appointment = super(AppointmentAddForm, self).save(commit) - appointment_types = self.cleaned_data['appointment_types'] - for appointment_type in appointment_types: - appointment_type_link = AppointmentTypeLink(appointment=appointment, appointment_type=appointment_type) - appointment_type_link.save() - return appointment - - class VisitDetailForm(ModelForm): datetime_begin = forms.DateField(label="Visit begins on", widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d") diff --git a/smash/web/tests/forms/test_AppointmentEditForm.py b/smash/web/tests/forms/test_AppointmentEditForm.py index f62fe60937dcb10e313d273f93e3a908619d1b88..ff8140c0db4c7d5dca4a071d6fc34713d7f62559 100644 --- a/smash/web/tests/forms/test_AppointmentEditForm.py +++ b/smash/web/tests/forms/test_AppointmentEditForm.py @@ -1,7 +1,6 @@ from django.test import TestCase -from web.forms import AppointmentAddForm -from web.forms import AppointmentEditForm +from web.forms import AppointmentEditForm, AppointmentAddForm from web.models import Appointment, Worker, AppointmentTypeLink from web.tests.functions import get_test_location, create_user, create_visit, create_location, create_appointment_type diff --git a/smash/web/tests/view/test_appointments.py b/smash/web/tests/view/test_appointments.py index 9560cfa02a577b900b541addbf304c98e0506747..a1544f213bdcfcb70a699f4b9299c61faa5b069d 100644 --- a/smash/web/tests/view/test_appointments.py +++ b/smash/web/tests/view/test_appointments.py @@ -4,7 +4,7 @@ import logging from django.core.files.uploadedfile import SimpleUploadedFile from django.urls import reverse -from web.forms import AppointmentEditForm, SubjectEditForm, StudySubjectEditForm +from web.forms import SubjectEditForm, StudySubjectEditForm, AppointmentEditForm from web.models import Appointment, StudySubject, Visit from web.tests import LoggedInTestCase from web.tests.functions import create_study_subject, create_visit, create_appointment, create_worker, \ diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py index fac01b5a1d1aabe6537fb641e13612734b3a6e76..d45f67f8c6067ac2185519b3284e5f6ffd0c2b76 100644 --- a/smash/web/views/appointment.py +++ b/smash/web/views/appointment.py @@ -9,7 +9,7 @@ from django.shortcuts import get_object_or_404, redirect from web.models.appointment_list import APPOINTMENT_LIST_APPROACHING, APPOINTMENT_LIST_GENERIC, \ APPOINTMENT_LIST_UNFINISHED from . import wrap_response -from ..forms import AppointmentDetailForm, AppointmentAddForm, AppointmentEditForm, SubjectEditForm, \ +from web.forms import AppointmentDetailForm, AppointmentEditForm, AppointmentAddForm, SubjectEditForm, \ StudySubjectEditForm from ..models import Appointment, StudySubject, MailTemplate