# Path: pttrack/views.py
# class NoteFormView(FormView):
#     note_type = None
# 
#     def get_context_data(self, **kwargs):
#         '''Inject self.note_type and patient into the context.'''
# 
#         if self.note_type is None:
#             raise ImproperlyConfigured("NoteCreate view must have" +
#                                        "'note_type' variable set.")
# 
#         context = super(NoteFormView, self).get_context_data(**kwargs)
#         context['note_type'] = self.note_type
# 
#         if 'pt_id' in self.kwargs:
#             context['patient'] = mymodels.Patient.objects. \
#                 get(pk=self.kwargs['pt_id'])
# 
#         return context
# 
# class NoteUpdate(UpdateView):
#     note_type = None
# 
#     def get_context_data(self, **kwargs):
#         '''Inject self.note_type as the note type.'''
# 
#         if self.note_type is None:
#             raise ImproperlyConfigured("NoteUpdate view must have" +
#                                        "'note_type' variable set.")
# 
#         context = super(NoteUpdate, self).get_context_data(**kwargs)
#         context['note_type'] = self.note_type
# 
#         return context
# 
#     # TODO: add shared form_valid code here from all subclasses.
# 
# def get_current_provider_type(request):
#     '''
#     Given the request, produce the ProviderType of the logged in user. This is
#     done using session data.
#     '''
#     return get_object_or_404(mymodels.ProviderType,
#                              pk=request.session['clintype_pk'])
# 
# Path: appointment/models.py
# class Appointment(Note):
# 
#     class Meta(object):
#         ordering = ["-clindate", "-clintime"]
# 
#     PSYCH_NIGHT = 'PSYCH_NIGHT'
#     ACUTE_FOLLOWUP = 'ACUTE_FOLLOWUP'
#     CHRONIC_CARE = 'CHRONIC_CARE'
#     VACCINE = 'VACCINE'
#     APPOINTMENT_TYPES = (
#         (PSYCH_NIGHT, 'Psych Night'),
#         (ACUTE_FOLLOWUP, 'Acute Followup'),
#         (CHRONIC_CARE, 'Chronic Care'),
#         (VACCINE, "Vaccine Followup")
#     )
# 
#     clindate = models.DateField(verbose_name="Appointment Date")
#     clintime = models.TimeField(
#         verbose_name="Time of Appointment",
#         default=generate_default_appointment_time)
#     appointment_type = models.CharField(
#         max_length=15, choices=APPOINTMENT_TYPES,
#         verbose_name='Appointment Type', default=CHRONIC_CARE)
#     comment = models.TextField(
#         help_text="What should happen at this appointment?")
# 
#     pt_showed = models.NullBooleanField(
#         verbose_name="Patient Showed",
#         blank=True, help_text="Did the patient come to this appointment?")
# 
#     history = HistoricalRecords()
# 
#     def __str__(self):
#         return "Appointment ({type}) for {name} on {date}".format(
#             type=self.verbose_appointment_type(),
#             name=self.patient.name(),
#             date=str(self.clindate))
# 
#     def verbose_appointment_type(self):
#         appointment_type_index = list(zip(*self.APPOINTMENT_TYPES))[0].index(
#             self.appointment_type)
# 
#         return self.APPOINTMENT_TYPES[appointment_type_index][1]
# 
#     def clean(self):
#         ids = Appointment.objects.filter(clindate=self.clindate).\
#             values_list('id', flat=True)
# 
#         if (ids.count() >= settings.OSLER_MAX_APPOINTMENTS and
#                 self.id not in ids):
#             raise ValidationError(
#                 "Osler is configured only to allow %s appointments per day" %
#                 settings.OSLER_MAX_APPOINTMENTS)
# 
# Path: appointment/forms.py
# class AppointmentForm(ModelForm):
# 
#     class Meta(object):
#         model = Appointment
#         fields = ['clindate', 'clintime', 'appointment_type', 'comment',
#                   'patient']
# 
#         widgets = {
#             'clindate': DateTimePicker(options={"format": "YYYY-MM-DD"}),
#             'clintime': TimeInput(format='%H:%M')}
# 
#     def __init__(self, *args, **kwargs):
#         super(AppointmentForm, self).__init__(*args, **kwargs)
#         self.helper = FormHelper(self)
#         self.helper.add_input(Submit('submit', 'Submit'))


import collections
from django.core.urlresolvers import reverse
from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from django.utils.timezone import now
from pttrack.views import NoteFormView, NoteUpdate, get_current_provider_type
from pttrack.models import Patient
from .models import Appointment
from .forms import AppointmentForm
    apt = get_object_or_404(Appointment, pk=pk)
    apt.pt_showed = False
    apt.save()

    return HttpResponseRedirect(reverse("appointment-list"))


def mark_arrived(request, pk):
    """Mark a patient as having arrived to an appointment
    """

    apt = get_object_or_404(Appointment, pk=pk)
    apt.pt_showed = True
    apt.save()

    return HttpResponseRedirect(reverse("patient-update", args=(apt.patient.pk,)))



class AppointmentUpdate(NoteUpdate):
    template_name = "pttrack/form-update.html"
    model = Appointment
    form_class = AppointmentForm
    note_type = "Appointment"
    success_url = "/appointment/list"


class AppointmentCreate(NoteFormView):
    template_name = 'appointment/form_submission.html'
    note_type = "Appointment"

# Path: pttrack/views.py
# class NoteFormView(FormView):
#     note_type = None
# 
#     def get_context_data(self, **kwargs):
#         '''Inject self.note_type and patient into the context.'''
# 
#         if self.note_type is None:
#             raise ImproperlyConfigured("NoteCreate view must have" +
#                                        "'note_type' variable set.")
# 
#         context = super(NoteFormView, self).get_context_data(**kwargs)
#         context['note_type'] = self.note_type
# 
#         if 'pt_id' in self.kwargs:
#             context['patient'] = mymodels.Patient.objects. \
#                 get(pk=self.kwargs['pt_id'])
# 
#         return context
# 
# class NoteUpdate(UpdateView):
#     note_type = None
# 
#     def get_context_data(self, **kwargs):
#         '''Inject self.note_type as the note type.'''
# 
#         if self.note_type is None:
#             raise ImproperlyConfigured("NoteUpdate view must have" +
#                                        "'note_type' variable set.")
# 
#         context = super(NoteUpdate, self).get_context_data(**kwargs)
#         context['note_type'] = self.note_type
# 
#         return context
# 
#     # TODO: add shared form_valid code here from all subclasses.
# 
# def get_current_provider_type(request):
#     '''
#     Given the request, produce the ProviderType of the logged in user. This is
#     done using session data.
#     '''
#     return get_object_or_404(mymodels.ProviderType,
#                              pk=request.session['clintype_pk'])
# 
# Path: appointment/models.py
# class Appointment(Note):
# 
#     class Meta(object):
#         ordering = ["-clindate", "-clintime"]
# 
#     PSYCH_NIGHT = 'PSYCH_NIGHT'
#     ACUTE_FOLLOWUP = 'ACUTE_FOLLOWUP'
#     CHRONIC_CARE = 'CHRONIC_CARE'
#     VACCINE = 'VACCINE'
#     APPOINTMENT_TYPES = (
#         (PSYCH_NIGHT, 'Psych Night'),
#         (ACUTE_FOLLOWUP, 'Acute Followup'),
#         (CHRONIC_CARE, 'Chronic Care'),
#         (VACCINE, "Vaccine Followup")
#     )
# 
#     clindate = models.DateField(verbose_name="Appointment Date")
#     clintime = models.TimeField(
#         verbose_name="Time of Appointment",
#         default=generate_default_appointment_time)
#     appointment_type = models.CharField(
#         max_length=15, choices=APPOINTMENT_TYPES,
#         verbose_name='Appointment Type', default=CHRONIC_CARE)
#     comment = models.TextField(
#         help_text="What should happen at this appointment?")
# 
#     pt_showed = models.NullBooleanField(
#         verbose_name="Patient Showed",
#         blank=True, help_text="Did the patient come to this appointment?")
# 
#     history = HistoricalRecords()
# 
#     def __str__(self):
#         return "Appointment ({type}) for {name} on {date}".format(
#             type=self.verbose_appointment_type(),
#             name=self.patient.name(),
#             date=str(self.clindate))
# 
#     def verbose_appointment_type(self):
#         appointment_type_index = list(zip(*self.APPOINTMENT_TYPES))[0].index(
#             self.appointment_type)
# 
#         return self.APPOINTMENT_TYPES[appointment_type_index][1]
# 
#     def clean(self):
#         ids = Appointment.objects.filter(clindate=self.clindate).\
#             values_list('id', flat=True)
# 
#         if (ids.count() >= settings.OSLER_MAX_APPOINTMENTS and
#                 self.id not in ids):
#             raise ValidationError(
#                 "Osler is configured only to allow %s appointments per day" %
#                 settings.OSLER_MAX_APPOINTMENTS)
# 
# Path: appointment/forms.py
# class AppointmentForm(ModelForm):
# 
#     class Meta(object):
#         model = Appointment
#         fields = ['clindate', 'clintime', 'appointment_type', 'comment',
#                   'patient']
# 
#         widgets = {
#             'clindate': DateTimePicker(options={"format": "YYYY-MM-DD"}),
#             'clintime': TimeInput(format='%H:%M')}
# 
#     def __init__(self, *args, **kwargs):
#         super(AppointmentForm, self).__init__(*args, **kwargs)
#         self.helper = FormHelper(self)
#         self.helper.add_input(Submit('submit', 'Submit'))


# Path: appointment/views.py
import collections
from django.core.urlresolvers import reverse
from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from django.utils.timezone import now
from pttrack.views import NoteFormView, NoteUpdate, get_current_provider_type
from pttrack.models import Patient
from .models import Appointment
from .forms import AppointmentForm

    apt = get_object_or_404(Appointment, pk=pk)
    apt.pt_showed = False
    apt.save()

    return HttpResponseRedirect(reverse("appointment-list"))


def mark_arrived(request, pk):
    """Mark a patient as having arrived to an appointment
    """

    apt = get_object_or_404(Appointment, pk=pk)
    apt.pt_showed = True
    apt.save()

    return HttpResponseRedirect(reverse("patient-update", args=(apt.patient.pk,)))



class AppointmentUpdate(NoteUpdate):
    template_name = "pttrack/form-update.html"
    model = Appointment
    form_class = AppointmentForm
    note_type = "Appointment"
    success_url = "/appointment/list"


class AppointmentCreate(NoteFormView):
    template_name = 'appointment/form_submission.html'
    note_type = "Appointment"

    form_class = AppointmentForm