Skip to content
Snippets Groups Projects
Commit be8cfc1e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

separation of the calendars between units added

parent 4584e305
No related branches found
No related tags found
1 merge request!2Appointments dev
......@@ -14,6 +14,7 @@ admin.site.register(Item)
admin.site.register(Room)
admin.site.register(AppointmentType)
admin.site.register(Language, LanguageAdmin)
admin.site.register(Location)
admin.site.register(Worker)
admin.site.register(FlyingTeam)
admin.site.register(Avaibility)
......
......@@ -10,6 +10,15 @@ from datetime import timedelta
def get_current_year():
return datetime.datetime.now().year
class Location (models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return "%s" % (self.name)
def __unicode__(self):
return "%s" % (self.name)
class Language (models.Model):
name = models.CharField(max_length=20)
image = models.ImageField()
......@@ -35,13 +44,6 @@ class Subject(models.Model):
(SEX_CHOICES_FEMALE,'Female'),
)
LOCATION_CHOICES_LIH = 'L'
LOCATION_CHOICES = (
(LOCATION_CHOICES_LIH,'LIH'),
('P','PRC'),
('F','FLYING TEAM'),
)
SUBJECT_TYPE_CHOICES_CONTROL = 'C'
SUBJECT_TYPE_CHOICES = (
(SUBJECT_TYPE_CHOICES_CONTROL,'CONTROL'),
......@@ -75,9 +77,8 @@ class Subject(models.Model):
default= False,
editable=False
)
default_appointment_location = models.CharField(max_length=1,
choices=LOCATION_CHOICES,
verbose_name='Default appointment location'
default_location = models.ForeignKey(Location,
verbose_name='Default appointment location',
)
first_name = models.CharField(max_length=50,
verbose_name='First name'
......@@ -91,7 +92,7 @@ class Subject(models.Model):
)
phone_number = models.CharField(max_length=20,
null=True,
blank=True,
blank=True,
verbose_name='Phone number'
)
phone_number_2 = models.CharField(max_length=20,
......@@ -332,6 +333,9 @@ class Worker (models.Model):
languages = models.ManyToManyField(Language,
verbose_name='Known languages'
)
locations = models.ManyToManyField(Location,
verbose_name='Locations'
)
appointments = models.ManyToManyField('Appointment', blank=True,
verbose_name='Appointments'
)
......@@ -491,6 +495,9 @@ class Appointment(models.Model):
null=True,
blank=True
)
location = models.ForeignKey(Location,
verbose_name='Location',
)
visit = models.ForeignKey(Visit,
verbose_name='Visit ID'
)
......
......@@ -3,8 +3,35 @@ from datetime import timedelta
from web.models import *
from web.views import *
def create_location(name="test"):
return Location.objects.create(name=name)
def get_test_location():
locations = Location.objects.filter(name = "test");
if len(locations)>0:
return locations[0]
else:
return create_location()
def create_subject():
return Subject.objects.create(first_name="Piotr", last_name="Gawron", sex= Subject.SEX_CHOICES_MALE)
return Subject.objects.create(
first_name="Piotr",
last_name="Gawron",
default_location = get_test_location(),
sex= Subject.SEX_CHOICES_MALE)
def create_user():
return User.objects.create_user(
username='piotr',
email='jacob@bla',
password='top_secret')
def create_worker():
return Worker.objects.create(
first_name='piotr',
last_name="gawron",
email='jacob@bla',
)
def create_visit(subject):
return Visit.objects.create(datetime_begin=get_today_midnight_date()+datetime.timedelta(days=-31),
......@@ -13,4 +40,7 @@ def create_visit(subject):
is_finished = False)
def create_appointment(visit):
return Appointment.objects.create(visit = visit, length = 30)
return Appointment.objects.create(
visit = visit,
length = 30,
location = get_test_location())
......@@ -2,13 +2,16 @@ from django.test import TestCase
from web.forms import SubjectAddForm
from web.models import Subject
from web.tests.functions import *
class SubjectAddFormTests(TestCase):
def setUp(self):
location = get_test_location()
self.sample_data = {'first_name': 'name',
'last_name': 'name',
'sex' : Subject.SEX_CHOICES_MALE,
'type' : Subject.SUBJECT_TYPE_CHOICES_CONTROL,
'default_appointment_location' : Subject.LOCATION_CHOICES_LIH,
'default_location' : location.id,
'country' : 'Luxembourg'
}
def test_validation(self):
......
......@@ -3,14 +3,16 @@ from web.forms import SubjectAddForm
from web.forms import SubjectEditForm
from web.models import Subject
from web.tests.functions import *
class SubjectEditFormTests(TestCase):
def setUp(self):
location = get_test_location()
self.sample_data = {'first_name': 'name',
'last_name': 'name',
'sex' : Subject.SEX_CHOICES_MALE,
'type' : Subject.SUBJECT_TYPE_CHOICES_CONTROL,
'default_appointment_location' : Subject.LOCATION_CHOICES_LIH,
'default_location' : location.id,
'country' : 'Luxembourg',
'screening_number' : '123',
'nd_number' : 'nd_123'
......
......@@ -4,13 +4,16 @@ from web.forms import VisitAddForm
from web.models import Subject
from web.models import Visit
from web.tests.functions import *
class SubjectAddFormTests(TestCase):
def setUp(self):
location = get_test_location()
subject_data = {'first_name': 'name',
'last_name': 'name',
'sex' : Subject.SEX_CHOICES_MALE,
'type' : Subject.SUBJECT_TYPE_CHOICES_CONTROL,
'default_appointment_location' : Subject.LOCATION_CHOICES_LIH,
'default_location' : location.id,
'country' : 'Luxembourg',
}
self.subject = SubjectAddForm(data=subject_data).save()
......
from django.test import TestCase
from web.forms import SubjectAddForm
from web.forms import VisitAddForm
from web.models import Subject
from web.models import Visit
from web.tests.functions import *
class ViewFunctionsTests(TestCase):
def setUp(self):
create_location("tescik")
return
def test_locations_for_user(self):
user = create_user()
self.assertEquals(Location.objects.all().count(), len(get_filter_locations(user)))
def test_locations_for_worker(self):
worker = create_worker()
self.assertEquals(Location.objects.all().count(), len(get_filter_locations(worker)))
def test_locations_for_worker_with_location(self):
worker = create_worker()
worker.locations = [get_test_location()]
worker.save()
create_location()
self.assertEquals(1, len(get_filter_locations(worker)))
......@@ -19,7 +19,7 @@ class NotificationViewTests(TestCase):
username='piotr', email='jacob@bla', password='top_secret')
def test_get_exceeded_visit_notifications_count(self):
original_notification = get_visits_without_appointments_count()
original_notification = get_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
......@@ -27,11 +27,11 @@ class NotificationViewTests(TestCase):
visit.save()
appointment = create_appointment(visit)
notification = get_exceeded_visit_notifications_count()
notification = get_exceeded_visit_notifications_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_exceeded_visit_notifications_count_2(self):
original_notification = get_visits_without_appointments_count()
original_notification = get_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
......@@ -40,20 +40,20 @@ class NotificationViewTests(TestCase):
visit.save()
appointment = create_appointment(visit)
notification = get_exceeded_visit_notifications_count()
notification = get_exceeded_visit_notifications_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_visits_without_appointments_count(self):
original_notification = get_visits_without_appointments_count()
original_notification = get_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
notification = get_visits_without_appointments_count()
notification = get_visits_without_appointments_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_visits_without_appointments_count_3(self):
original_notification = get_visits_without_appointments_count()
original_notification = get_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
appointment = create_appointment(visit)
......@@ -61,32 +61,32 @@ class NotificationViewTests(TestCase):
appointment.status = Appointment.APPOINTMENT_STATUS_CANCELLED
appointment.save()
notification = get_visits_without_appointments_count()
notification = get_visits_without_appointments_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_approaching_visits_without_appointments_count(self):
original_notification = get_approaching_visits_without_appointments_count()
original_notification = get_approaching_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
visit.datetime_begin = get_today_midnight_date()+datetime.timedelta(days=2)
visit.save()
notification = get_approaching_visits_without_appointments_count()
notification = get_approaching_visits_without_appointments_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_approaching_visits_without_appointments_count_2(self):
original_notification = get_approaching_visits_without_appointments_count()
original_notification = get_approaching_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
visit.datetime_begin = get_today_midnight_date()+datetime.timedelta(days=2)
visit.save()
appointment = create_appointment(visit)
notification = get_approaching_visits_without_appointments_count()
notification = get_approaching_visits_without_appointments_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_approaching_visits_without_appointments_count_3(self):
original_notification = get_approaching_visits_without_appointments_count()
original_notification = get_approaching_visits_without_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
visit.datetime_begin = get_today_midnight_date()+datetime.timedelta(days=2)
......@@ -96,54 +96,56 @@ class NotificationViewTests(TestCase):
appointment.status = Appointment.APPOINTMENT_STATUS_CANCELLED
appointment.save()
notification = get_approaching_visits_without_appointments_count()
notification = get_approaching_visits_without_appointments_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_subject_with_no_visit_notifications_count(self):
original_notification = get_subject_with_no_visit_notifications_count()
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
notification = get_subject_with_no_visit_notifications_count()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_subject_with_no_visit_notifications_count_2(self):
original_notification = get_subject_with_no_visit_notifications_count()
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
visit = create_visit(subject)
visit.is_finished=True
visit.save()
notification = get_subject_with_no_visit_notifications_count()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_subject_with_no_visit_notifications_count_3(self):
original_notification = get_subject_with_no_visit_notifications_count()
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
visit = create_visit(subject)
notification = get_subject_with_no_visit_notifications_count()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_subject_with_no_visit_notifications_count_4(self):
original_notification = get_subject_with_no_visit_notifications_count()
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
subject.dead = True
subject.save()
notification = get_subject_with_no_visit_notifications_count()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_subject_with_no_visit_notifications_count_5(self):
original_notification = get_subject_with_no_visit_notifications_count()
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
subject.resigned = True
subject.save()
notification = get_subject_with_no_visit_notifications_count()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_unfinished_appointments_count(self):
original_notification = get_unfinished_appointments_count()
original_notification = get_unfinished_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
appointment = create_appointment(visit)
......@@ -151,11 +153,11 @@ class NotificationViewTests(TestCase):
appointment.status = Appointment.APPOINTMENT_STATUS_SCHEDULED
appointment.save()
notification = get_unfinished_appointments_count()
notification = get_unfinished_appointments_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_unfinished_appointments_count_2(self):
original_notification = get_unfinished_appointments_count()
original_notification = get_unfinished_appointments_count(self.user)
subject = create_subject()
visit = create_visit(subject)
appointment = create_appointment(visit)
......@@ -163,5 +165,34 @@ class NotificationViewTests(TestCase):
appointment.status = Appointment.APPOINTMENT_STATUS_CANCELLED
appointment.save()
notification = get_unfinished_appointments_count()
notification = get_unfinished_appointments_count(self.user)
self.assertEquals(original_notification.count, notification.count)
def test_get_subject_with_no_visit_notifications_count_for_many_locations(self):
create_location("l1")
create_location("l2")
original_notification = get_subject_with_no_visit_notifications_count(self.user)
subject = create_subject()
notification = get_subject_with_no_visit_notifications_count(self.user)
self.assertEquals(original_notification.count + 1, notification.count)
def test_get_subject_with_no_visit_notifications_count_for_invalid_location(self):
worker = create_worker()
worker.locations= [create_location("l2")]
worker.save()
original_notification = get_subject_with_no_visit_notifications_count(worker)
subject = create_subject()
subject.default_location=create_location("l1")
subject.save()
notification = get_subject_with_no_visit_notifications_count(worker)
self.assertEquals(original_notification.count, notification.count)
worker.locations= Location.objects.filter(name="l1")
worker.save()
notification = get_subject_with_no_visit_notifications_count(worker)
self.assertEquals(original_notification.count +1, notification.count)
......@@ -74,66 +74,103 @@ class NotificationCount(object):
self.style = style
self.type = type
def get_exceeded_visits():
return Visit.objects.filter(datetime_end__lt = get_today_midnight_date(), is_finished = False).order_by('datetime_begin')
def get_filter_locations(user):
worker = None
if isinstance(user, User):
workers = Worker.objects.filter(user=user)
if len(workers)>0:
worker = workers[0]
else:
worker = user
if worker==None or worker.locations.count() == 0:
return Location.objects.all()
else:
return worker.locations.all()
def get_exceeded_visit_notifications_count():
def get_exceeded_visits(user):
return Visit.objects.filter(datetime_end__lt = get_today_midnight_date(),
is_finished = False,
subject__default_location__in = get_filter_locations(user)
).order_by('datetime_begin')
def get_exceeded_visit_notifications_count(user):
notification = NotificationCount(
title = "exceeded visit time",
count = get_exceeded_visits().count(),
count = get_exceeded_visits(user).count(),
style = "fa fa-thermometer-4 text-red",
type = 'web.views.exceeded_visits')
return notification
def get_subjects_with_no_visit():
return Subject.objects.annotate(my_count=Count(Case(When(visit__is_finished=False, then=1)))).filter(dead=False, resigned=False, my_count=0)
def get_subjects_with_no_visit(user):
result = Subject.objects.annotate(my_count=Count(Case(When(visit__is_finished=False, then=1)))).filter(
dead=False,
resigned=False,
my_count=0,
default_location__in = get_filter_locations(user)
)
return result
def get_subject_with_no_visit_notifications_count():
def get_subject_with_no_visit_notifications_count(user):
notification = NotificationCount(
title = "subject without visit",
count = get_subjects_with_no_visit().count(),
count = get_subjects_with_no_visit(user).count(),
style = "fa fa-users text-aqua",
type = 'web.views.subject_no_visits')
return notification
def get_visits_without_appointments_count():
def get_visits_without_appointments_count(user):
notification = NotificationCount(
title = "unfinished visits ",
count = get_unfinished_visits().count(),
count = get_unfinished_visits(user).count(),
style = "fa fa-user-times text-yellow",
type = 'web.views.unfinished_visits')
return notification
def get_unfinished_visits():
def get_unfinished_visits(user):
today = get_today_midnight_date()
#list visits that have no scheduled appointments and taking place now
return Visit.objects.annotate(my_count=Count(Case(When(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED, then=1)))).filter(datetime_begin__lt = today, datetime_end__gt = today, is_finished = False, my_count=0)
def get_approaching_visits_without_appointments_count():
return Visit.objects.annotate(my_count=Count(Case(When(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED, then=1)))).filter(
datetime_begin__lt = today,
datetime_end__gt = today,
is_finished = False,
subject__default_location__in = get_filter_locations(user),
my_count=0)
def get_approaching_visits_without_appointments_count(user):
notification = NotificationCount(
title = "approaching visits ",
count = get_approaching_visits_without_appointments().count(),
count = get_approaching_visits_without_appointments(user).count(),
style = "fa fa-users text-aqua",
type = 'web.views.approaching_visits_without_appointments')
return notification
def get_approaching_visits_without_appointments():
def get_approaching_visits_without_appointments(user):
today = get_today_midnight_date()
today_plus_two_months =today+datetime.timedelta(days=62)
return Visit.objects.annotate(my_count=Count(Case(When(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED, then=1)))).filter(datetime_begin__gt = today, datetime_begin__lt = today_plus_two_months, is_finished = False, my_count=0)
return Visit.objects.annotate(my_count=Count(Case(When(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED, then=1)))).filter(
datetime_begin__gt = today,
datetime_begin__lt = today_plus_two_months,
is_finished = False,
subject__default_location__in = get_filter_locations(user),
my_count=0)
def get_unfinished_appointments_count():
def get_unfinished_appointments_count(user):
return NotificationCount(
title = "unfinished appointments ",
count = get_unfinished_appointments().count(),
count = get_unfinished_appointments(user).count(),
style = "fa fa-history text-yellow",
type = 'web.views.unfinished_appointments')
def get_unfinished_appointments():
return Appointment.objects.filter(datetime_when__lt = get_today_midnight_date(), status = Appointment.APPOINTMENT_STATUS_SCHEDULED)
def get_unfinished_appointments(user):
return Appointment.objects.filter(
datetime_when__lt = get_today_midnight_date(),
status = Appointment.APPOINTMENT_STATUS_SCHEDULED,
location__in = get_filter_locations(user),
)
def get_notifications(the_user):
workers = Worker.objects.filter(user=the_user)
......@@ -142,11 +179,11 @@ def get_notifications(the_user):
if len(workers)>0:
person = workers[0]
if person.role == Worker.ROLE_CHOICES_SECRETARY:
notifications.append(get_exceeded_visit_notifications_count())
notifications.append(get_visits_without_appointments_count())
notifications.append(get_approaching_visits_without_appointments_count())
notifications.append(get_unfinished_appointments_count())
notifications.append(get_subject_with_no_visit_notifications_count())
notifications.append(get_exceeded_visit_notifications_count(person))
notifications.append(get_visits_without_appointments_count(person))
notifications.append(get_approaching_visits_without_appointments_count(person))
notifications.append(get_unfinished_appointments_count(person))
notifications.append(get_subject_with_no_visit_notifications_count(person))
for notification in notifications:
count += notification.count
return (count, notifications)
......@@ -186,20 +223,20 @@ def visits(request):
def exceeded_visits(request):
context = {
'visit_list': get_exceeded_visits()
'visit_list': get_exceeded_visits(request.user)
}
return wrap_response(request, 'visits/index.html', context)
def unfinished_visits(request):
context = {
'visit_list': get_unfinished_visits()
'visit_list': get_unfinished_visits(request.user)
}
return wrap_response(request, 'visits/index.html', context)
def approaching_visits_without_appointments(request):
context = {
'visit_list': get_approaching_visits_without_appointments()
'visit_list': get_approaching_visits_without_appointments(request.user)
}
return wrap_response(request, 'visits/index.html', context)
......@@ -268,7 +305,7 @@ def subject_add(request):
return wrap_response(request, 'subjects/add.html', {'form': form})
def subject_no_visits(request):
subjects_list = get_subjects_with_no_visit().order_by('-last_name')
subjects_list = get_subjects_with_no_visit(request.user).order_by('-last_name')
context = {
'subjects_list': subjects_list
}
......@@ -337,8 +374,6 @@ def subject_visit_details(request, id):
visform = VisitDetailForm(instance=vis)
endlist.append((visform,assign,finished,visid,visit_title))
#print len(endlist)
#print endlist[0]
return wrap_response(request, 'subjects/visitdetails.html', {'display': endlist, "id":id})
def doctors(request):
......@@ -454,21 +489,24 @@ def get_today_midnight_date():
today_midnight = datetime.datetime(today.year,today.month,today.day)
return today_midnight
def appointments(request):
futureDate = datetime.datetime.now() + datetime.timedelta(days=93)
planning_list = Appointment.objects.filter(datetime_when__isnull=True, visit__datetime_begin__lt = futureDate)
def get_calendar_full_appointments(user):
month_ago = get_today_midnight_date() + datetime.timedelta(days=-31)
return Appointment.objects.filter(
datetime_when__gt = month_ago,
location__in = get_filter_locations(user),
).order_by('datetime_when')
today_midnight = get_today_midnight_date()
month_ago = today_midnight + datetime.timedelta(days=-31)
approaching_list = Appointment.objects.filter(datetime_when__gt = today_midnight, status = Appointment.APPOINTMENT_STATUS_SCHEDULED).order_by('datetime_when')
full_list = Appointment.objects.filter(datetime_when__gt = month_ago).order_by('datetime_when')
def appointments(request):
approaching_list = Appointment.objects.filter(
datetime_when__gt = get_today_midnight_date(),
location__in = get_filter_locations(request.user),
status = Appointment.APPOINTMENT_STATUS_SCHEDULED
).order_by('datetime_when')
for plan in planning_list:
plan.datetime_when = plan.visit.datetime_begin
full_list = get_calendar_full_appointments(request.user)
context = {
'planning_list': planning_list,
'approaching_list': approaching_list,
'full_list': full_list
}
......@@ -476,7 +514,7 @@ def appointments(request):
return wrap_response(request, "appointments/index.html",context)
def unfinished_appointments(request):
appointments = get_unfinished_appointments()
appointments = get_unfinished_appointments(request.user)
context = {
'appointment_list': appointments,
}
......@@ -490,10 +528,7 @@ def appointment_details(request, id):
def appointment_add(request, id):
today = datetime.datetime.now()
today_midnight = datetime.datetime(today.year,today.month,today.day)
month_ago = today +datetime.timedelta(days=-31)
full_list = Appointment.objects.filter(datetime_when__gt = month_ago).order_by('datetime_when')
full_list = get_calendar_full_appointments(request.user)
if request.method == 'POST':
form = AppointmentAddForm(request.POST, request.FILES)
form.fields['visit'].widget = forms.HiddenInput()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment