diff --git a/smash/web/models/worker.py b/smash/web/models/worker.py index d537b239422e34e5c346f3c79dd01e756ae6ff31..dd9a2205c1f0448c2575259b15910969b077bfd8 100644 --- a/smash/web/models/worker.py +++ b/smash/web/models/worker.py @@ -1,9 +1,12 @@ # coding=utf-8 import datetime +import logging from django.contrib.auth.models import User, AnonymousUser from django.db import models +logger = logging.getLogger(__name__) + class Worker(models.Model): class Meta: @@ -59,6 +62,22 @@ class Worker(models.Model): return True return False + def disable(self): + if self.user is not None: + self.user.is_active = False + self.user.save() + logger.info("'" + self.user.username + "' account has been disabled") + return True + else: + logger.warn("Cannot disable account for user '" + self.first_name + " " + self.last_name + "'") + return False + + def is_active(self): + if self.user is not None: + return self.user.is_active + else: + return False + @staticmethod def get_by_user(the_user): if isinstance(the_user, User): diff --git a/smash/web/templates/doctors/index.html b/smash/web/templates/doctors/index.html index 37c31f6dc2fb793ee1b49438c3f9593edbc00bc6..4dc34ec01d556867dd728b5f51d553f360eceb0c 100644 --- a/smash/web/templates/doctors/index.html +++ b/smash/web/templates/doctors/index.html @@ -35,6 +35,7 @@ <th>Unit</th> <th>Details</th> <th>On leave</th> + <th>Disabled</th> </tr> </thead> <tbody> @@ -60,6 +61,13 @@ <button type="button" class="btn btn-block btn-success">NO</button> {% endif %} </td> + <td> + {% if worker.is_active %} + <a href="{% url 'web.views.doctor_disable' worker.id %}" class="btn btn-block btn-warning">DISABLE</a> + {% else %} + YES + {% endif %} + </td> </tr> {% endfor %} diff --git a/smash/web/tests/test_model_subject.py b/smash/web/tests/models/test_subject.py similarity index 83% rename from smash/web/tests/test_model_subject.py rename to smash/web/tests/models/test_subject.py index 5397e82b5edabfdc58b81dc0993986707d3bfcf0..6266ec03e6eb3eeab698a0732d37f42425527ce5 100644 --- a/smash/web/tests/test_model_subject.py +++ b/smash/web/tests/models/test_subject.py @@ -1,7 +1,9 @@ +from django.contrib.auth.models import User +from django.test import Client from django.test import TestCase -from functions import create_subject, create_appointment -from functions import create_visit +from web.tests.functions import create_subject, create_appointment, create_worker +from web.tests.functions import create_visit from web.models import Appointment from web.models import Visit @@ -31,4 +33,4 @@ class SubjectModelTests(TestCase): self.assertTrue(subject.resigned) self.assertTrue(visit_finished) - self.assertEquals(Appointment.APPOINTMENT_STATUS_CANCELLED, appointment_status) + self.assertEquals(Appointment.APPOINTMENT_STATUS_CANCELLED, appointment_status) \ No newline at end of file diff --git a/smash/web/tests/models/test_worker.py b/smash/web/tests/models/test_worker.py new file mode 100644 index 0000000000000000000000000000000000000000..be91b963bf4c9136f2432abf49f52a8489c1667b --- /dev/null +++ b/smash/web/tests/models/test_worker.py @@ -0,0 +1,31 @@ +from django.contrib.auth.models import User +from django.test import Client +from django.test import TestCase + +from web.tests.functions import create_worker + + +class WorkerModelTests(TestCase): + def test_disable(self): + client = Client() + username = 'piotr' + password = 'top_secret' + user = User.objects.create_user(username=username, email='jacob@bla', password=password) + worker = create_worker(user) + + disable_status = worker.disable() + self.assertTrue(disable_status) + + success = client.login(username=username, password=password) + self.assertFalse(success) + self.assertFalse(user.is_active) + self.assertFalse(worker.is_active()) + + def test_disable_invalid(self): + worker = create_worker() + disable_status = worker.disable() + self.assertFalse(disable_status) + + def test_is_active_for_invalid(self): + worker = create_worker() + self.assertFalse(worker.is_active()) diff --git a/smash/web/urls.py b/smash/web/urls.py index 825d5e003f646ea653a95f897873f870a048e1e5..bc687f2603a8d20c5e863d6dac8c4e9584a812b2 100644 --- a/smash/web/urls.py +++ b/smash/web/urls.py @@ -100,6 +100,7 @@ urlpatterns = [ url(r'^doctors$', views.doctor.doctors, name='web.views.doctors'), url(r'^doctors/add$', views.doctor.doctor_add, name='web.views.doctor_add'), url(r'^doctors/edit/(?P<doctor_id>\d+)$', views.doctor.doctor_edit, name='web.views.doctor_edit'), + url(r'^doctors/disable/(?P<doctor_id>\d+)$', views.doctor.doctor_disable, name='web.views.doctor_disable'), url(r'^doctors/(?P<doctor_id>\d+)/availability/add$', views.doctor.doctor_availability_add, name='views.doctor.doctor_availability_add'), diff --git a/smash/web/views/doctor.py b/smash/web/views/doctor.py index 12e4cace3cccecca92b53f2ac13ce6d468eed054..d6738333ac2225cebac3be65db76e3e85448a403 100644 --- a/smash/web/views/doctor.py +++ b/smash/web/views/doctor.py @@ -49,6 +49,12 @@ def doctor_edit(request, doctor_id): }) +def doctor_disable(request, doctor_id): + the_doctor = get_object_or_404(Worker, id=doctor_id) + the_doctor.disable() + return doctors(request) + + def doctor_availability_delete(request, availability_id): availability = Availability.objects.filter(id=availability_id) doctor_id = availability[0].person.id