# 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'])


from builtins import str
from django.shortcuts import get_object_or_404, render
from django.http import (HttpResponseRedirect, HttpResponseServerError,
                         HttpResponse)
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template.loader import get_template
from django.utils.timezone import now
from django.views.generic.edit import FormView
from django.conf import settings
from pttrack.views import NoteFormView, NoteUpdate, get_current_provider_type
from pttrack.models import Patient, ProviderType
from xhtml2pdf import pisa
from . import models
from . import forms
from tempfile import TemporaryFile
    def dispatch(self, *args, **kwargs):
        '''
        Intercept dispatch for NoteUpdate and verify that the user has
        permission to modify this Workup.
        '''
        current_user_type = get_current_provider_type(self.request)
        wu = get_object_or_404(models.Workup, pk=kwargs['pk'])

        # if it's an attending, we allow updates.
        if current_user_type.signs_charts or not wu.signed():
            return super(WorkupUpdate, self).dispatch(*args, **kwargs)
        else:
            return HttpResponseRedirect(reverse('workup',
                                        args=(kwargs['pk'],)))

    def get_success_url(self):
        return reverse('workup', args=(self.object.id,))


class ProgressNoteUpdate(NoteUpdate):
    template_name = "pttrack/form-update.html"
    model = models.ProgressNote
    form_class = forms.ProgressNoteForm
    note_type = 'Clinical Psychology Note'

    def get_success_url(self):
        pnote = self.object
        return reverse("progress-note-detail", args=(pnote.id, ))



# 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: workup/views.py
from builtins import str
from django.shortcuts import get_object_or_404, render
from django.http import (HttpResponseRedirect, HttpResponseServerError,
                         HttpResponse)
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template.loader import get_template
from django.utils.timezone import now
from django.views.generic.edit import FormView
from django.conf import settings
from pttrack.views import NoteFormView, NoteUpdate, get_current_provider_type
from pttrack.models import Patient, ProviderType
from xhtml2pdf import pisa
from . import models
from . import forms
from tempfile import TemporaryFile

    def dispatch(self, *args, **kwargs):
        '''
        Intercept dispatch for NoteUpdate and verify that the user has
        permission to modify this Workup.
        '''
        current_user_type = get_current_provider_type(self.request)
        wu = get_object_or_404(models.Workup, pk=kwargs['pk'])

        # if it's an attending, we allow updates.
        if current_user_type.signs_charts or not wu.signed():
            return super(WorkupUpdate, self).dispatch(*args, **kwargs)
        else:
            return HttpResponseRedirect(reverse('workup',
                                        args=(kwargs['pk'],)))

    def get_success_url(self):
        return reverse('workup', args=(self.object.id,))


class ProgressNoteUpdate(NoteUpdate):
    template_name = "pttrack/form-update.html"
    model = models.ProgressNote
    form_class = forms.ProgressNoteForm
    note_type = 'Clinical Psychology Note'

    def get_success_url(self):
        pnote = self.object
        return reverse("progress-note-detail", args=(pnote.id, ))



class ProgressNoteCreate(NoteFormView):