Skip to content

Commit 7326b26

Browse files
committed
Added the ability to create timed announcements on the admin site
1 parent 73b2275 commit 7326b26

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

tmh_registry/registry/admin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Zone,
1616
Region,
1717
HospitalRegionMapping,
18-
RegionZoneMapping,
18+
RegionZoneMapping, Announcement,
1919
)
2020

2121

@@ -67,7 +67,9 @@ class HospitalRegionMappingAdmin(ExportMixin, admin.ModelAdmin):
6767
class RegionZoneMappingAdmin(ExportMixin, admin.ModelAdmin):
6868
model = RegionZoneMapping
6969

70-
70+
@admin.register(Announcement)
71+
class AnnouncementAdmin(ExportMixin, admin.ModelAdmin):
72+
model = Announcement
7173
"""
7274
To allow CSV export on User model
7375
"""

tmh_registry/registry/api/serializers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Hospital,
1515
Patient,
1616
PatientHospitalMapping,
17-
PreferredHospital,
17+
PreferredHospital, Announcement,
1818
)
1919

2020

@@ -712,3 +712,8 @@ def get_patient_hospital_id(self, obj):
712712
).first()
713713

714714
return mapping.patient_hospital_id if mapping else None
715+
716+
class AnnouncementSerializer(ModelSerializer):
717+
class Meta:
718+
model = Announcement
719+
fields = ["id", "announcement_text", "display_from", "display_until", "created_at"]

tmh_registry/registry/api/viewsets.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
1818
from rest_framework.permissions import IsAuthenticated
1919
from rest_framework.response import Response
20-
from rest_framework.viewsets import GenericViewSet
20+
from rest_framework.viewsets import GenericViewSet, ReadOnlyModelViewSet
21+
from django.utils.timezone import now
2122

2223
from ..models import (
2324
Discharge,
@@ -27,6 +28,7 @@
2728
Patient,
2829
PatientHospitalMapping,
2930
PreferredHospital,
31+
Announcement,
3032
)
3133
from .serializers import (
3234
CreatePatientSerializer,
@@ -41,7 +43,7 @@
4143
PatientHospitalMappingWriteSerializer,
4244
PreferredHospitalReadSerializer,
4345
ReadPatientSerializer,
44-
SurgeonEpisodeSummarySerializer, OwnedEpisodeSerializer, UnlinkedPatientSerializer,
46+
SurgeonEpisodeSummarySerializer, OwnedEpisodeSerializer, UnlinkedPatientSerializer, AnnouncementSerializer,
4547
)
4648
from ...users.models import MedicalPersonnel
4749

@@ -401,3 +403,13 @@ def get_serializer_context(self):
401403
context = super().get_serializer_context()
402404
context['request'] = self.request
403405
return context
406+
407+
class AnnouncementViewSet(ReadOnlyModelViewSet):
408+
serializer_class = AnnouncementSerializer
409+
410+
def get_queryset(self):
411+
current_time = now()
412+
return Announcement.objects.filter(
413+
Q(display_from__lte=current_time) | Q(display_from__isnull=True),
414+
Q(display_until__gte=current_time) | Q(display_until__isnull=True)
415+
).order_by("-created_at")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 3.1.3 on 2025-05-27 07:26
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('registry', '0038_auto_20250519_0903'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='Announcement',
15+
fields=[
16+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17+
('announcement_text', models.TextField()),
18+
('display_from', models.DateTimeField(blank=True, null=True)),
19+
('display_until', models.DateTimeField(blank=True, null=True)),
20+
('created_at', models.DateTimeField(auto_now_add=True)),
21+
],
22+
),
23+
]

tmh_registry/registry/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,11 @@ def __str__(self):
273273
class Meta:
274274
verbose_name_plural = "Region-Zone Mappings"
275275

276+
class Announcement(Model):
277+
announcement_text = TextField()
278+
display_from = DateTimeField(null=True, blank=True)
279+
display_until = DateTimeField(null=True, blank=True)
280+
created_at = DateTimeField(auto_now_add=True)
281+
282+
def __str__(self):
283+
return f"Announcement ({self.created_at}): {self.announcement_text[:50]}"

tmh_registry/registry/urls.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
PatientViewSet,
1111
PreferredHospitalViewSet,
1212
SurgeonEpisodeSummaryViewSet,
13-
OwnedEpisodesViewSet, UnlinkedPatientsViewSet,
13+
OwnedEpisodesViewSet,
14+
UnlinkedPatientsViewSet,
15+
AnnouncementViewSet
1416
)
1517

1618
router = DefaultRouter()
@@ -24,7 +26,7 @@
2426
router.register(r'surgeon-episode-summary', SurgeonEpisodeSummaryViewSet, basename='surgeon-episode-summary')
2527
router.register(r"owned-episodes", OwnedEpisodesViewSet, basename='owned-episodes')
2628
router.register(r'unlinked-patients', UnlinkedPatientsViewSet, basename='unlinked-patients')
27-
29+
router.register(r"announcements", AnnouncementViewSet, basename="announcements")
2830

2931
urlpatterns = [
3032
path("", include(router.urls)),

0 commit comments

Comments
 (0)