# -*- coding: utf-8 -*-
"""
Module containing a view for adding/editing appointments
:subtitle:`Class definitions:`
"""
from datetime import date
from django.template import loader
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from apps.utils.utils import send_sms_to_patient
from apps.questionnaire.models import QuestionnaireRequest
from apps.appointment.forms import AppointmentAddEditForm
from apps.rcmessages.models import RCMessage
from django.utils.decorators import method_decorator
from core.views import FormView
from apps.healthperson.patient.views import PatientBaseView
from apps.healthperson.utils import is_allowed_patient_admins, login_url
from django.contrib.auth.decorators import user_passes_test
[docs]class AppointmentEdit(PatientBaseView, FormView):
'''
Class based view for adding/editing appointment
information by a secretary
'''
template_name = 'appointment/appointment_edit.html'
form_class = AppointmentAddEditForm
[docs] def render_sms_and_template(self, appointment, message_template,
sms_template):
'''
Renders both sms and template at once
since the context is the same.
Args:
- appointment: the appointment instance
- message_template: the template_name to load for the message
- sms_template: the template_name to load for the sms
Returns:
[message_template, sms_template]
'''
current_date = date.today()
context = {'patient': self.patient,
'questionnaire_request': self.questionnaire_request,
'current_date': current_date,
'secretary': self.secretary,
'appointment': appointment,
'is_male': self.patient.user.gender == 'male'}
message_template = loader.get_template(message_template)
sms_template = loader.get_template(sms_template)
return [message_template.render(context, self.request),
sms_template.render(context, self.request)]
[docs] def get_default_urgent_message_and_sms(self, appointment):
'''
Renders both sms and template at once
for urgent controls
Args:
- appointment: the appointment instance
Returns:
[message_template, sms_template]
'''
message_template = 'appointment/message/default_urgent_response.html'
sms_template = 'appointment/sms/default_urgent_response.html'
return self.render_sms_and_template(
appointment, message_template, sms_template)
[docs] def get_default_message_and_sms(self, appointment):
'''
Renders both sms and template at once
for non-urgent controls
Args:
- appointment: the appointment instance
Returns:
[message_template, sms_template]
'''
message_template = 'appointment/message/default_response.html'
sms_template = 'appointment/sms/default_response.html'
return self.render_sms_and_template(
appointment, message_template, sms_template)
@method_decorator(user_passes_test(
is_allowed_patient_admins, login_url=login_url))
def dispatch(self, *args, **kwargs):
self.questionnaire_request = get_object_or_404(
QuestionnaireRequest, id=self.kwargs.get(
'questionnaire_request_id'))
self.success_url = reverse('index')
self.show_warning = False
self.message_content = None
self.sms_content = None
self.secretary = self.request.user.healthperson
return super(AppointmentEdit, self).dispatch(*args, **kwargs)
def get_form_kwargs(self):
kwargs = super(AppointmentEdit, self).get_form_kwargs()
kwargs.update({'questionnaire_request': self.questionnaire_request,
'user': self.request.user})
return kwargs
def get_context_data(self, **kwargs):
context = super(AppointmentEdit, self).get_context_data(**kwargs)
context.update({'show_warning': self.show_warning,
'questionnaire_request': self.questionnaire_request,
'message_content': self.message_content,
'sms_content': self.sms_content})
return context