diff --git a/smash/web/forms/worker_form.py b/smash/web/forms/worker_form.py index f77894a77e19478a72b9066ef69ef0e27196680b..90bb384110bdbd8d07a1dbb6596e4b6e3ad83b3e 100644 --- a/smash/web/forms/worker_form.py +++ b/smash/web/forms/worker_form.py @@ -3,7 +3,45 @@ 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 +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 + + +def role_choices_by_worker_type(worker_type): + if worker_type == WORKER_STAFF: + return STUDY_ROLE_CHOICES + elif worker_type == WORKER_HEALTH_PARTNER: + return HEALTH_PARTNER_ROLE_CHOICES + elif worker_type == WORKER_VOUCHER_PARTNER: + return VOUCHER_PARTNER_ROLE_CHOICES + else: + return () + + +def worker_type_by_worker(worker): + roles = worker.roles.filter(study=GLOBAL_STUDY_ID) + if roles.count() == 0: + return WORKER_STAFF + role = roles[0] + for role_type, role_name in STUDY_ROLE_CHOICES.items(): + if role_type == role: + return WORKER_STAFF + for role_type, role_name in HEALTH_PARTNER_ROLE_CHOICES.items(): + if role_type == role: + return WORKER_HEALTH_PARTNER + for role_type, role_name in VOUCHER_PARTNER_ROLE_CHOICES.items(): + if role_type == role: + return WORKER_VOUCHER_PARTNER + return WORKER_STAFF + + +def roles_by_worker_type(worker_type): + role_choices = role_choices_by_worker_type(worker_type) + roles = [] + + for role_type, role_name in role_choices: + roles.append(role_type) + return roles class WorkerForm(ModelForm): @@ -12,7 +50,8 @@ class WorkerForm(ModelForm): exclude = ['appointments'] def __init__(self, *args, **kwargs): - choices = kwargs.pop('role_choices', STUDY_ROLE_CHOICES) + worker_type = kwargs.pop('worker_type', WORKER_STAFF) + choices = role_choices_by_worker_type(worker_type) super(WorkerForm, self).__init__(*args, **kwargs) self.fields['role'] = forms.ChoiceField(label='Role', choices=choices) @@ -22,4 +61,4 @@ class WorkerForm(ModelForm): 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 + WorkerStudyRole.objects.create(worker=instance, study_id=GLOBAL_STUDY_ID, role=self.cleaned_data['role']) diff --git a/smash/web/views/worker.py b/smash/web/views/worker.py index 5a2927b60f403f4a2106095c83c1ca447e7c5f63..61eeb94da90141bdaa8484e82cb05d14a502d215 100644 --- a/smash/web/views/worker.py +++ b/smash/web/views/worker.py @@ -4,25 +4,20 @@ 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 web.forms import WorkerForm +from web.forms.worker_form import roles_by_worker_type, worker_type_by_worker +from web.models import Worker, Availability, Holiday +from web.models.constants import WEEKDAY_CHOICES, GLOBAL_STUDY_ID +from web.models.worker_study_role import WORKER_STAFF from . import wrap_response -from ..models import Worker, Availability, Holiday -from ..models.constants import WEEKDAY_CHOICES, GLOBAL_STUDY_ID logger = logging.getLogger(__name__) def worker_list(request, worker_type=WORKER_STAFF): - role_choices = role_choices_by_worker_type(worker_type) - roles = [] - - for role_type, role_name in role_choices: - roles.append(role_type) - - doctors_list = Worker.objects.filter(roles__study_id=GLOBAL_STUDY_ID, roles__role__in=roles).order_by( + doctors_list = Worker.objects.filter(roles__study_id=GLOBAL_STUDY_ID, + roles__role__in=roles_by_worker_type(worker_type)).order_by( '-last_name') context = { 'doctors_list': doctors_list, @@ -32,50 +27,22 @@ def worker_list(request, worker_type=WORKER_STAFF): return wrap_response(request, "doctors/index.html", context) -def role_choices_by_worker_type(worker_type): - if worker_type == WORKER_STAFF: - return STUDY_ROLE_CHOICES - elif worker_type == WORKER_HEALTH_PARTNER: - return HEALTH_PARTNER_ROLE_CHOICES - elif worker_type == WORKER_VOUCHER_PARTNER: - return VOUCHER_PARTNER_ROLE_CHOICES - else: - return () - - def worker_add(request, worker_type): if request.method == 'POST': - form = WorkerForm(request.POST, request.FILES, role_choices=role_choices_by_worker_type(worker_type)) + form = WorkerForm(request.POST, request.FILES, worker_type=worker_type) if form.is_valid(): form.save() return redirect('web.views.workers') else: - form = WorkerForm(role_choices=role_choices_by_worker_type(worker_type)) + form = WorkerForm(worker_type=worker_type) return wrap_response(request, 'doctors/add.html', {'form': form}) -def role_choices_by_worker(worker): - roles = worker.roles.filter(study=GLOBAL_STUDY_ID) - if roles.count() == 0: - return STUDY_ROLE_CHOICES - role = roles[0] - for role_type, role_name in STUDY_ROLE_CHOICES.items(): - if role_type == role: - return STUDY_ROLE_CHOICES - for role_type, role_name in HEALTH_PARTNER_ROLE_CHOICES.items(): - if role_type == role: - return HEALTH_PARTNER_ROLE_CHOICES - for role_type, role_name in VOUCHER_PARTNER_ROLE_CHOICES.items(): - if role_type == role: - return VOUCHER_PARTNER_ROLE_CHOICES - return STUDY_ROLE_CHOICES - - def worker_edit(request, worker_id): worker = get_object_or_404(Worker, id=worker_id) if request.method == 'POST': - form = WorkerForm(request.POST, request.FILES, instance=worker, role_choices=role_choices_by_worker(worker)) + form = WorkerForm(request.POST, request.FILES, instance=worker, worker_type=worker_type_by_worker(worker)) if form.is_valid(): form.save() return redirect('web.views.workers')