From 05c9628220ea9644d70da04bc36fc1b3d1cedf44 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Wed, 13 Dec 2017 10:16:37 +0100 Subject: [PATCH] WorkerForm extracted to separate file --- smash/web/forms/__init__.py | 3 ++- smash/web/forms/forms.py | 21 --------------------- smash/web/forms/worker_form.py | 25 +++++++++++++++++++++++++ smash/web/models/visit.py | 2 +- smash/web/views/worker.py | 3 ++- 5 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 smash/web/forms/worker_form.py diff --git a/smash/web/forms/__init__.py b/smash/web/forms/__init__.py index 9ae4bcb1..bc462855 100644 --- a/smash/web/forms/__init__.py +++ b/smash/web/forms/__init__.py @@ -1,4 +1,5 @@ -from forms import WorkerForm, AppointmentDetailForm, AppointmentEditForm, AppointmentAddForm, VisitDetailForm, \ +from worker_form import WorkerForm +from forms import AppointmentDetailForm, AppointmentEditForm, AppointmentAddForm, VisitDetailForm, \ VisitAddForm, ContactAttemptForm, ContactAttemptEditForm, KitRequestForm, StatisticsForm, AvailabilityAddForm, \ AvailabilityEditForm, HolidayAddForm from study_subject_forms import StudySubjectAddForm, StudySubjectDetailForm, StudySubjectEditForm diff --git a/smash/web/forms/forms.py b/smash/web/forms/forms.py index b3d0091b..fc36b7b0 100644 --- a/smash/web/forms/forms.py +++ b/smash/web/forms/forms.py @@ -6,11 +6,9 @@ from django import forms from django.forms import ModelForm, Form from django.utils.dates import MONTHS -from web.models.constants import GLOBAL_STUDY_ID from web.models import StudySubject, Worker, Appointment, Visit, AppointmentType, ContactAttempt, AppointmentTypeLink, \ Availability, Holiday from web.models.constants import SUBJECT_TYPE_CHOICES -from web.models.worker_study_role import STUDY_ROLE_CHOICES, WorkerStudyRole from web.views.notifications import get_filter_locations """ @@ -51,25 +49,6 @@ def get_worker_from_args(kwargs): return result -class WorkerForm(ModelForm): - class Meta: - model = Worker - exclude = ['appointments'] - - def __init__(self, *args, **kwargs): - choices = kwargs.pop('role_choices', STUDY_ROLE_CHOICES) - super(WorkerForm, self).__init__(*args, **kwargs) - self.fields['role'] = forms.ChoiceField(label='Role', choices=choices) - - def save(self, commit=True): - instance = super(WorkerForm, self).save(commit) - roles = WorkerStudyRole.objects.filter(worker=instance, study_id=GLOBAL_STUDY_ID) - if roles.count() > 0: - roles.update(role=self.cleaned_data['role']) - else: - WorkerStudyRole.objects.create(worker=instance, study_id=GLOBAL_STUDY_ID, role=self.cleaned_data['role']) - - class AppointmentDetailForm(ModelForm): class Meta: model = Appointment diff --git a/smash/web/forms/worker_form.py b/smash/web/forms/worker_form.py new file mode 100644 index 00000000..f77894a7 --- /dev/null +++ b/smash/web/forms/worker_form.py @@ -0,0 +1,25 @@ +from django import forms +from django.forms import ModelForm + +from web.models import Worker, WorkerStudyRole +from web.models.constants import GLOBAL_STUDY_ID +from web.models.worker_study_role import STUDY_ROLE_CHOICES + + +class WorkerForm(ModelForm): + class Meta: + model = Worker + exclude = ['appointments'] + + def __init__(self, *args, **kwargs): + choices = kwargs.pop('role_choices', STUDY_ROLE_CHOICES) + super(WorkerForm, self).__init__(*args, **kwargs) + self.fields['role'] = forms.ChoiceField(label='Role', choices=choices) + + def save(self, commit=True): + instance = super(WorkerForm, self).save(commit) + roles = WorkerStudyRole.objects.filter(worker=instance, study_id=GLOBAL_STUDY_ID) + if roles.count() > 0: + roles.update(role=self.cleaned_data['role']) + else: + WorkerStudyRole.objects.create(worker=instance, study_id=GLOBAL_STUDY_ID, role=self.cleaned_data['role']) \ No newline at end of file diff --git a/smash/web/models/visit.py b/smash/web/models/visit.py index 61b29704..2922ea8b 100644 --- a/smash/web/models/visit.py +++ b/smash/web/models/visit.py @@ -5,7 +5,7 @@ from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver -from constants import BOOL_CHOICES, SUBJECT_TYPE_CHOICES_CONTROL +from web.models.constants import BOOL_CHOICES, SUBJECT_TYPE_CHOICES_CONTROL class Visit(models.Model): diff --git a/smash/web/views/worker.py b/smash/web/views/worker.py index 2ed75a0e..5a2927b6 100644 --- a/smash/web/views/worker.py +++ b/smash/web/views/worker.py @@ -4,10 +4,11 @@ import logging from django.contrib.auth.decorators import login_required from django.shortcuts import redirect, get_object_or_404 +from web.forms import WorkerForm +from web.forms import AvailabilityAddForm, AvailabilityEditForm, HolidayAddForm from web.models.worker_study_role import STUDY_ROLE_CHOICES, HEALTH_PARTNER_ROLE_CHOICES, VOUCHER_PARTNER_ROLE_CHOICES, \ WORKER_STAFF, WORKER_HEALTH_PARTNER, WORKER_VOUCHER_PARTNER from . import wrap_response -from ..forms import WorkerForm, AvailabilityAddForm, AvailabilityEditForm, HolidayAddForm from ..models import Worker, Availability, Holiday from ..models.constants import WEEKDAY_CHOICES, GLOBAL_STUDY_ID -- GitLab