diff --git a/smash/web/forms/voucher_forms.py b/smash/web/forms/voucher_forms.py index 68de4581885be709aca5cc71916a63c7d073ec63..99bb89736762a3c49a1e859d95feb0389f033fa9 100644 --- a/smash/web/forms/voucher_forms.py +++ b/smash/web/forms/voucher_forms.py @@ -38,7 +38,11 @@ class VoucherForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): + voucher_types = kwargs.pop('voucher_types', VoucherType.objects.all()) super(VoucherForm, self).__init__(*args, **kwargs) + + self.fields['voucher_type'].queryset = voucher_types + self.fields['number'].widget.attrs['readonly'] = True self.fields['number'].required = False diff --git a/smash/web/templates/appointments/edit.html b/smash/web/templates/appointments/edit.html index 44a3ca2709e203ba4573717b306d30fcf6790aba..dc1df827cfd7967dbad32f7e7e69ae79545bacd9 100644 --- a/smash/web/templates/appointments/edit.html +++ b/smash/web/templates/appointments/edit.html @@ -35,7 +35,7 @@ </div> <div class="box box-info"> - <form method="post" action="" enctype="multipart/form-data" class="form-horizontal"> + <form method="post" action="" enctype="multipart/form-data" class="form-horizontal"> {% csrf_token %} <fieldset> <div class="box-header with-border"> @@ -136,6 +136,11 @@ </div><!-- /.box-footer --> </form> </div> + + {% if appointment.visit.subject.study.columns.vouchers %} + {% include 'includes/subject_vouchers_box.html' with subject=appointment.visit.subject %} + {% endif %} + {% include 'includes/mail_templates_box.html' with instance_id=appointment.id %} {% if appointment.visit %} {% include 'includes/contact_attempts_box.html' with subject=appointment.visit.subject contact_attempts=contact_attempts appointment_id=appointment.id %} diff --git a/smash/web/templates/includes/subject_vouchers_box.html b/smash/web/templates/includes/subject_vouchers_box.html new file mode 100644 index 0000000000000000000000000000000000000000..36638b10787183d64546baaa7fc5d2ec3810f496 --- /dev/null +++ b/smash/web/templates/includes/subject_vouchers_box.html @@ -0,0 +1,48 @@ +<div class="row"> + <div class="col-lg-12"> + <div class="box box-success"> + <div class="box-header with-border"> + <h3>Vouchers <a title="add a new voucher" + id="add-voucher" + href="{% url 'web.views.voucher_add' %}?study_subject_id={{ subject.id }}" + class="text-primary" + ><i class="fa fa-plus-circle text-success"></i></a></h3> + </div> + <div class="box-body"> + <table class="table table-bordered table-striped"> + <thead> + <tr> + + <th class="text-center">Number</th> + <th class="text-center">Type</th> + <th class="text-center">Issue date</th> + <th class="text-center">Expiry date</th> + <th class="text-center">Status</th> + <th class="text-center">Use date</th> + <th class="text-center">Partner</th> + <th class="text-center">Feedback</th> + <th class="text-center">Edit</th> + </tr> + </thead> + <tbody> + {% for voucher in subject.vouchers.all %} + <tr> + <td>{{ voucher.number }}</td> + <td>{{ voucher.voucher_type }}</td> + <td>{{ voucher.issue_date }}</td> + <td>{{ voucher.expiry_date }}</td> + <td>{{ voucher.status }}</td> + <td>{{ voucher.use_date }}</td> + <td>{{ voucher.usage_partner.first_name }} {{ voucher.usage_partner.last_name }}</td> + <td>{{ voucher.feedback }}</td> + <td><a href="{% url 'web.views.voucher_edit' voucher.id %}"><i class="fa fa-edit"></i></a> + </td> + </tr> + {% endfor %} + </tbody> + </table> + </div> + </div> + + </div> +</div> diff --git a/smash/web/templates/subjects/edit.html b/smash/web/templates/subjects/edit.html index 1dedffbc01f4a185c5cc4b9c94871f2ec3e8910e..669f863086174f60aaca05907bf6713c84725340 100644 --- a/smash/web/templates/subjects/edit.html +++ b/smash/web/templates/subjects/edit.html @@ -109,11 +109,16 @@ </div><!-- /.col-md-12 --> </div><!-- /.row --> + {% if study_subject.study.columns.vouchers %} + {% include 'includes/subject_vouchers_box.html' with subject=study_subject %} + {% endif %} + {% include 'includes/mail_templates_box.html' with instance_id=study_subject.id %} {% include 'includes/contact_attempts_box.html' with subject=study_subject contact_attempts=contact_attempts %} + <div class="modal modal-danger fade" id="confirm-dead-resigned-mark-dialog" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> diff --git a/smash/web/tests/forms/test_voucher_forms.py b/smash/web/tests/forms/test_voucher_forms.py index e7310f34cc85ce677cec636da9be9de5fdead66b..64a78eef15b2365f515aecfa030b4da88aaeb166 100644 --- a/smash/web/tests/forms/test_voucher_forms.py +++ b/smash/web/tests/forms/test_voucher_forms.py @@ -16,14 +16,16 @@ class VoucherFormTests(LoggedInWithWorkerTestCase): super(VoucherFormTests, self).setUp() def test_auto_generated_use_date(self): + voucher_type = create_voucher_type() study_subject = create_study_subject() + study_subject.voucher_types.add(voucher_type) create_voucher(study_subject) voucher_form = VoucherForm() form_data = { "status": VOUCHER_STATUS_USED, "usage_partner": str(self.worker.id), - "voucher_type": create_voucher_type().id + "voucher_type": voucher_type.id } for key, value in voucher_form.initial.items(): form_data[key] = format_form_field(value) diff --git a/smash/web/tests/view/test_voucher.py b/smash/web/tests/view/test_voucher.py index 871bf563689ad15dfc4d97c2d1cb1ebd28b6b4a4..583328498a851e367e7ca832f9b581cdc2316bcc 100644 --- a/smash/web/tests/view/test_voucher.py +++ b/smash/web/tests/view/test_voucher.py @@ -13,7 +13,9 @@ logger = logging.getLogger(__name__) class VoucherTypeViewTests(LoggedInTestCase): def test_render_add_voucher_request(self): - response = self.client.get(reverse('web.views.voucher_add')) + study_subject = create_study_subject() + url = reverse('web.views.voucher_add') + "?study_subject_id=" + str(study_subject.id) + response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_render_edit_voucher_request(self): @@ -22,11 +24,13 @@ class VoucherTypeViewTests(LoggedInTestCase): self.assertEqual(response.status_code, 200) def test_add_voucher(self): + voucher_type = create_voucher_type() study_subject = create_study_subject() + study_subject.voucher_types.add(voucher_type) visit_detail_form = VoucherForm() form_data = { "status": VOUCHER_STATUS_NEW, - "voucher_type": create_voucher_type().id + "voucher_type": voucher_type.id } for key, value in visit_detail_form.initial.items(): form_data[key] = format_form_field(value) diff --git a/smash/web/views/appointment.py b/smash/web/views/appointment.py index 6d64acd468487ffc1a569241661c309d5b12f5ae..fac01b5a1d1aabe6537fb641e13612734b3a6e76 100644 --- a/smash/web/views/appointment.py +++ b/smash/web/views/appointment.py @@ -139,7 +139,6 @@ def appointment_edit(request, id): 'appointment_form': appointment_form, 'study_subject_form': study_subject_form, 'subject_form': subject_form, - 'id': id, 'appointment': the_appointment, 'contact_attempts': contact_attempts, 'mail_templates': MailTemplate.get_appointment_mail_templates(languages) diff --git a/smash/web/views/voucher.py b/smash/web/views/voucher.py index 840e5d34f67df75f44e8e01d9e97838977e6a51c..4ca8361cbfb391c347a85db4a4ad38e1bef77266 100644 --- a/smash/web/views/voucher.py +++ b/smash/web/views/voucher.py @@ -8,7 +8,7 @@ from django.views.generic import ListView from django.views.generic import UpdateView from web.forms import VoucherForm -from web.models import Voucher +from web.models import Voucher, StudySubject from web.models.constants import GLOBAL_STUDY_ID from . import WrappedView @@ -21,6 +21,10 @@ class VoucherListView(ListView, WrappedView): template_name = 'vouchers/list.html' +def voucher_types_for_study_subject(study_subject_id): + return StudySubject.objects.get(id=study_subject_id).voucher_types.all() + + class VoucherCreateView(CreateView, WrappedView): form_class = VoucherForm model = Voucher @@ -39,6 +43,11 @@ class VoucherCreateView(CreateView, WrappedView): # noinspection PyUnresolvedReferences return reverse_lazy('web.views.subject_edit', kwargs={'id': self.request.GET.get("study_subject_id", -1)}) + def get_form_kwargs(self): + kwargs = super(VoucherCreateView, self).get_form_kwargs() + kwargs['voucher_types'] = voucher_types_for_study_subject(self.request.GET.get("study_subject_id", -1)) + return kwargs + class VoucherEditView(SuccessMessageMixin, UpdateView, WrappedView): form_class = VoucherForm @@ -51,5 +60,7 @@ class VoucherEditView(SuccessMessageMixin, UpdateView, WrappedView): def get_success_url(self, **kwargs): # noinspection PyUnresolvedReferences - study_subject_id = Voucher.objects.get(id=self.kwargs['pk']).study_subject.id - return reverse_lazy('web.views.subject_edit', kwargs={'id': study_subject_id}) + return reverse_lazy('web.views.subject_edit', kwargs={'id': self.get_study_subject_id()}) + + def get_study_subject_id(self): + return Voucher.objects.get(id=self.kwargs['pk']).study_subject.id