From 1feab6e102913c8564f41589866b17092d7bc65f Mon Sep 17 00:00:00 2001 From: ANKIT Date: Fri, 15 Mar 2024 13:47:33 +0530 Subject: [PATCH] added Hostel history functionality --- .../migrations/0016_merge_20240314_2250.py | 15 +++++ .../migrations/0017_auto_20240314_2252.py | 18 ++++++ .../applications/hostel_management/admin.py | 1 + .../migrations/0018_hostelhistory.py | 27 +++++++++ .../applications/hostel_management/models.py | 12 +++- .../applications/hostel_management/views.py | 59 ++++++++++++++++++- .../templates/hostelmanagement/hostel.html | 18 ++++++ .../hostelmanagement/hostel_history.html | 33 +++++++++++ .../hostelmanagement/hostel_history_data.html | 36 +++++++++++ 9 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 FusionIIIT/applications/globals/migrations/0016_merge_20240314_2250.py create mode 100644 FusionIIIT/applications/globals/migrations/0017_auto_20240314_2252.py create mode 100644 FusionIIIT/applications/hostel_management/migrations/0018_hostelhistory.py create mode 100644 FusionIIIT/templates/hostelmanagement/hostel_history.html create mode 100644 FusionIIIT/templates/hostelmanagement/hostel_history_data.html diff --git a/FusionIIIT/applications/globals/migrations/0016_merge_20240314_2250.py b/FusionIIIT/applications/globals/migrations/0016_merge_20240314_2250.py new file mode 100644 index 000000000..fe6197c3d --- /dev/null +++ b/FusionIIIT/applications/globals/migrations/0016_merge_20240314_2250.py @@ -0,0 +1,15 @@ +# Generated by Django 3.1.5 on 2024-03-14 22:50 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('globals', '0014_auto_20240312_1930'), + ('globals', '0014_auto_20240312_1902'), + ('globals', '0015_auto_20240312_2326'), + ] + + operations = [ + ] diff --git a/FusionIIIT/applications/globals/migrations/0017_auto_20240314_2252.py b/FusionIIIT/applications/globals/migrations/0017_auto_20240314_2252.py new file mode 100644 index 000000000..1ba4a1b03 --- /dev/null +++ b/FusionIIIT/applications/globals/migrations/0017_auto_20240314_2252.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.5 on 2024-03-14 22:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('globals', '0016_merge_20240314_2250'), + ] + + operations = [ + migrations.AlterField( + model_name='extrainfo', + name='user_status', + field=models.CharField(choices=[('NEW', 'NEW'), ('PRESENT', 'PRESENT')], default='PRESENT', max_length=50), + ), + ] diff --git a/FusionIIIT/applications/hostel_management/admin.py b/FusionIIIT/applications/hostel_management/admin.py index 99498f3f4..8e8917269 100644 --- a/FusionIIIT/applications/hostel_management/admin.py +++ b/FusionIIIT/applications/hostel_management/admin.py @@ -20,4 +20,5 @@ admin.site.register(HostelAllotment) admin.site.register(GuestRoom) admin.site.register(HostelTransactionHistory) +admin.site.register(HostelHistory) diff --git a/FusionIIIT/applications/hostel_management/migrations/0018_hostelhistory.py b/FusionIIIT/applications/hostel_management/migrations/0018_hostelhistory.py new file mode 100644 index 000000000..e11d2315d --- /dev/null +++ b/FusionIIIT/applications/hostel_management/migrations/0018_hostelhistory.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1.5 on 2024-03-14 22:52 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('globals', '0017_auto_20240314_2252'), + ('hostel_management', '0017_hall_type_of_seater'), + ] + + operations = [ + migrations.CreateModel( + name='HostelHistory', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('timestamp', models.DateTimeField(default=django.utils.timezone.now)), + ('batch', models.CharField(max_length=50, null=True)), + ('caretaker', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='caretaker_history', to='globals.staff')), + ('hall', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='hostel_management.hall')), + ('warden', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='warden_history', to='globals.faculty')), + ], + ), + ] diff --git a/FusionIIIT/applications/hostel_management/models.py b/FusionIIIT/applications/hostel_management/models.py index e75feb5c0..df18d5959 100644 --- a/FusionIIIT/applications/hostel_management/models.py +++ b/FusionIIIT/applications/hostel_management/models.py @@ -345,4 +345,14 @@ class HostelTransactionHistory(models.Model): timestamp = models.DateTimeField(auto_now_add=True) def __str__(self): - return f"{self.change_type} change in {self.hall} at {self.timestamp}" \ No newline at end of file + return f"{self.change_type} change in {self.hall} at {self.timestamp}" + +class HostelHistory(models.Model): + hall = models.ForeignKey(Hall, on_delete=models.CASCADE) + timestamp = models.DateTimeField(default=timezone.now) + caretaker = models.ForeignKey(Staff, on_delete=models.SET_NULL, null=True, related_name='caretaker_history') + batch = models.CharField(max_length=50, null=True) + warden = models.ForeignKey(Faculty, on_delete=models.SET_NULL, null=True, related_name='warden_history') + + def __str__(self): + return f"History for {self.hall.hall_name} - {self.timestamp}" \ No newline at end of file diff --git a/FusionIIIT/applications/hostel_management/views.py b/FusionIIIT/applications/hostel_management/views.py index 3a3b571f5..48fe23805 100644 --- a/FusionIIIT/applications/hostel_management/views.py +++ b/FusionIIIT/applications/hostel_management/views.py @@ -311,6 +311,9 @@ def hostel_view(request, context={}): context['student_fines'] = student_fines hostel_transactions = HostelTransactionHistory.objects.order_by('-timestamp') + + # Retrieve all hostel history entries + hostel_history = HostelHistory.objects.order_by('-timestamp') context = { 'all_hall': all_hall, @@ -345,6 +348,7 @@ def hostel_view(request, context={}): 'staff_fine_caretaker': staff_fine_caretaker, 'students': students, 'hostel_transactions':hostel_transactions, + 'hostel_history':hostel_history, **context } @@ -883,7 +887,8 @@ def post(self, request, *args, **kwargs): hostel_allotment.assignedCaretaker = caretaker_staff hostel_allotment.save() - + # Retrieve the current warden for the hall + current_warden = HallWarden.objects.filter(hall=hall).first() print("Before creating HostelTransactionHistory") try: @@ -897,6 +902,18 @@ def post(self, request, *args, **kwargs): except Exception as e: print("Error creating HostelTransactionHistory:", e) + + # Create hostel history + try: + HostelHistory.objects.create( + hall=hall, + caretaker=caretaker_staff, + batch=hall.assigned_batch, + warden=current_warden.faculty if( current_warden and current_warden.faculty) else None + ) + print("hostel hostory created succeessfully") + except Exception as e: + print ("Error creating history",e) return Response({'message': f'Caretaker {caretaker_username} assigned to Hall {hall_id} successfully'}, status=status.HTTP_201_CREATED) except Hall.DoesNotExist: @@ -933,6 +950,10 @@ def post(self, request, *args, **kwargs): for room_allotment in room_allotments: room_allotment.assignedBatch = hall.assigned_batch room_allotment.save() + + # retrieve the current caretaker and current warden for the hall + current_caretaker =HallCaretaker.objects.filter(hall=hall).first() + current_warden = HallWarden.objects.filter(hall=hall).first() # Record the transaction history HostelTransactionHistory.objects.create( @@ -942,6 +963,20 @@ def post(self, request, *args, **kwargs): new_value=hall.assigned_batch ) + # Create hostel history + try: + HostelHistory.objects.create( + hall=hall, + caretaker=current_caretaker.staff if (current_caretaker and current_caretaker.staff) else None, + + batch=hall.assigned_batch, + warden=current_warden.faculty if( current_warden and current_warden.faculty) else None + + ) + print("hostel hostory created succeessfully") + except Exception as e: + print ("Error creating history",e) + return JsonResponse({'status': 'success', 'message': 'Batch assigned successfully'}, status=200) except Hall.DoesNotExist: @@ -970,7 +1005,7 @@ def post(self, request, *args, **kwargs): # Retrieve the previous caretaker for the hall, if any prev_hall_warden = HallWarden.objects.filter(hall=hall).first() - print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + # Delete any previous assignments of the warden in Hallwarden table HallWarden.objects.filter(faculty=warden).delete() @@ -983,6 +1018,11 @@ def post(self, request, *args, **kwargs): # Assign the new warden to the hall in Hallwarden table hall_warden = HallWarden.objects.create(hall=hall, faculty=warden) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + #current caretker + current_caretaker =HallCaretaker.objects.filter(hall=hall).first() + print(current_caretaker) + # Update the assigned warden in Hostelallottment table hostel_allotments = HostelAllotment.objects.filter(hall=hall) for hostel_allotment in hostel_allotments: @@ -1001,6 +1041,21 @@ def post(self, request, *args, **kwargs): except Exception as e: print("Error creating HostelTransactionHistory:", e) + + # Create hostel history + try: + HostelHistory.objects.create( + hall=hall, + caretaker=current_caretaker.staff if (current_caretaker and current_caretaker.staff) else None, + + batch=hall.assigned_batch, + warden=warden + ) + print("hostel hostory created succeessfully") + except Exception as e: + print ("Error creating history",e) + + return Response({'message': f'Warden {warden_id} assigned to Hall {hall_id} successfully'}, status=status.HTTP_201_CREATED) except Hall.DoesNotExist: diff --git a/FusionIIIT/templates/hostelmanagement/hostel.html b/FusionIIIT/templates/hostelmanagement/hostel.html index 2438f41ac..eb1dd8bd6 100644 --- a/FusionIIIT/templates/hostelmanagement/hostel.html +++ b/FusionIIIT/templates/hostelmanagement/hostel.html @@ -253,6 +253,14 @@ {% endif %} + {% if is_superuser %} + + Hostel History + + + {% endif %} + + {% if user in hall_caretaker%} Inventory list @@ -406,6 +414,14 @@ {% endif %} + {% if is_superuser %} +
+ {% block Hostel_history %} + {% include 'hostelmanagement/hostel_history.html' %} + {% endblock %} +
+ {% endif %} + {% if is_superuser %}
{% block View_Hostels %} @@ -416,6 +432,8 @@ + + {% if user in hall_caretaker%}
diff --git a/FusionIIIT/templates/hostelmanagement/hostel_history.html b/FusionIIIT/templates/hostelmanagement/hostel_history.html new file mode 100644 index 000000000..0781f46ff --- /dev/null +++ b/FusionIIIT/templates/hostelmanagement/hostel_history.html @@ -0,0 +1,33 @@ + +{% load static %} +{% block Hostel_history %} + + + +{% for hall in all_hall %} +
+
+ {% block {{hall.hall_id}} %} + {% include 'hostelmanagement/hostel_history_data.html' with hall_id=hall.hall_id %} + {% endblock %} +
+
+{% endfor %} + +{% endblock %} + + + + \ No newline at end of file diff --git a/FusionIIIT/templates/hostelmanagement/hostel_history_data.html b/FusionIIIT/templates/hostelmanagement/hostel_history_data.html new file mode 100644 index 000000000..76cfb011e --- /dev/null +++ b/FusionIIIT/templates/hostelmanagement/hostel_history_data.html @@ -0,0 +1,36 @@ +{% load custom_tags %} + +{% block {{hall.hall_id}} %} +

{{hall.hall_id}} History

+ + + + + + + + + + + + + + + + {% for entry in hostel_history %} + + {% if entry.hall == hall %} + + + + + + + + + {% endif %} + {% endfor %} + +
HallBatchCaretakerWardenDate
{{entry.hall}}{{ entry.batch }}{{ entry.caretaker }}{{ entry.warden }}{{ entry.timestamp|date:"Y-m-d" }}
+ + {% endblock %} \ No newline at end of file