125 lines
4.0 KiB
Python
125 lines
4.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
biomon - Signs monitoring and patient management application
|
|
|
|
Copyright (C) 2015 Entr'ouvert
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
'''
|
|
|
|
|
|
from django.views.generic import (TemplateView, FormView, View, ListView,
|
|
CreateView, UpdateView, DeleteView)
|
|
from django.contrib.auth import authenticate, login, logout
|
|
from django.contrib.auth import forms as auth_forms
|
|
from django.http import HttpResponseRedirect
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django.contrib import messages
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from . import cbv
|
|
from . import models
|
|
from . import forms
|
|
|
|
LAST_PATIENT_COOKIE = 'last-patient'
|
|
|
|
|
|
class HomepageView(TemplateView):
|
|
template_name="biomon/homepage.html"
|
|
|
|
|
|
class LoginView(FormView):
|
|
form_class = auth_forms.AuthenticationForm
|
|
template_name = 'biomon/login.html'
|
|
|
|
def form_valid(self, form):
|
|
username = self.request.POST.get('username')
|
|
password = self.request.POST.get('password')
|
|
user = authenticate(username=username, password=password)
|
|
if user is not None:
|
|
if user.is_active:
|
|
login(self.request, user)
|
|
return super(LoginView, self).form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
next_url = self.request.POST.get('next', None)
|
|
if next_url:
|
|
return "%s" % (next_url)
|
|
return reverse_lazy('patient_list')
|
|
|
|
|
|
class LogoutView(View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
logout(request)
|
|
return HttpResponseRedirect(reverse_lazy('login'))
|
|
|
|
|
|
class PatientList(ListView):
|
|
model = models.Patient
|
|
|
|
def get_queryset(self):
|
|
return models.Patient.objects.order_by('display_name')
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(PatientList, self).get_context_data(**kwargs)
|
|
try:
|
|
context['lastpatient'] = models.Patient.objects.get(
|
|
pk=int(self.request.COOKIES.get(LAST_PATIENT_COOKIE)))
|
|
except:
|
|
pass
|
|
return context
|
|
|
|
|
|
class PatientCreate(CreateView):
|
|
model = models.Patient
|
|
fields = ['firstname', 'lastname', 'sex', 'birthdate', 'monitoring_place',
|
|
'medical_comment', 'enabled']
|
|
|
|
def form_valid(self, form):
|
|
messages.add_message(self.request, messages.INFO, u'Patient créé.')
|
|
return super(PatientCreate, self).form_valid(form)
|
|
|
|
|
|
class PatientUpdate(UpdateView):
|
|
model = models.Patient
|
|
fields = ['firstname', 'lastname', 'sex', 'birthdate', 'monitoring_place',
|
|
'medical_comment', 'enabled']
|
|
|
|
|
|
class PatientDetail(cbv.MultiUpdateView):
|
|
model = models.Patient
|
|
forms_classes = {
|
|
'main': forms.MainForm,
|
|
'simple_alert_profile' : forms.SimpleAlertProfileForm,
|
|
}
|
|
template_name = "biomon/patient_detail.html"
|
|
success_url = '.'
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
result = super(PatientDetail, self).dispatch(*args, **kwargs)
|
|
result.set_cookie(LAST_PATIENT_COOKIE, str(self.object.pk),
|
|
max_age=3600*24*365, httponly=True)
|
|
return result
|
|
|
|
def form_valid(self, form):
|
|
messages.add_message(self.request, messages.INFO, _(u'Patient record updated.'))
|
|
return super(PatientDetail, self).form_valid(form)
|
|
|
|
|
|
class PatientDelete(DeleteView):
|
|
model = models.Patient
|
|
success_url = reverse_lazy('patient_list')
|