accounts.admin

 1from django.contrib import admin
 2from .models import User, Hospital
 3
 4"""
 5Custom admin classes for Hospital and User models.
 6
 7- Shows only hospital-related data to non-superuser admin users.
 8- Superusers see all records.
 9"""
10
11class HospitalAdmin(admin.ModelAdmin):
12    """
13    Admin view for Hospital model.
14
15    - get_queryset: Limits displayed hospitals to those associated with the admin user unless user is superuser.
16    """
17    def get_queryset(self, request):
18        if request.user.is_superuser:
19            return super().get_queryset(request)
20        # Show only hospitals where the current user is associated
21        return super().get_queryset(request).filter(users=request.user)
22
23admin.site.register(Hospital, HospitalAdmin)
24
25class UserAdmin(admin.ModelAdmin):
26    """
27    Admin view for User model.
28
29    - get_queryset: Limits displayed users to those belonging to the admin's hospital unless user is superuser.
30    """
31    def get_queryset(self, request):
32        if request.user.is_superuser:
33            return super().get_queryset(request)
34        # Show only users from the current user's hospital
35        return super().get_queryset(request).filter(hospital=request.user.hospital)
36
37admin.site.register(User, UserAdmin)
class HospitalAdmin(django.contrib.admin.options.ModelAdmin):
12class HospitalAdmin(admin.ModelAdmin):
13    """
14    Admin view for Hospital model.
15
16    - get_queryset: Limits displayed hospitals to those associated with the admin user unless user is superuser.
17    """
18    def get_queryset(self, request):
19        if request.user.is_superuser:
20            return super().get_queryset(request)
21        # Show only hospitals where the current user is associated
22        return super().get_queryset(request).filter(users=request.user)

Admin view for Hospital model.

  • get_queryset: Limits displayed hospitals to those associated with the admin user unless user is superuser.
def get_queryset(self, request):
18    def get_queryset(self, request):
19        if request.user.is_superuser:
20            return super().get_queryset(request)
21        # Show only hospitals where the current user is associated
22        return super().get_queryset(request).filter(users=request.user)

Return a QuerySet of all model instances that can be edited by the admin site. This is used by changelist_view.

media
242    def _media(self):
243        # Get the media property of the superclass, if it exists
244        sup_cls = super(cls, self)
245        try:
246            base = sup_cls.media
247        except AttributeError:
248            base = Media()
249
250        # Get the media definition for this class
251        definition = getattr(cls, "Media", None)
252        if definition:
253            extend = getattr(definition, "extend", True)
254            if extend:
255                if extend is True:
256                    m = base
257                else:
258                    m = Media()
259                    for medium in extend:
260                        m += base[medium]
261                return m + Media(definition)
262            return Media(definition)
263        return base
class UserAdmin(django.contrib.admin.options.ModelAdmin):
26class UserAdmin(admin.ModelAdmin):
27    """
28    Admin view for User model.
29
30    - get_queryset: Limits displayed users to those belonging to the admin's hospital unless user is superuser.
31    """
32    def get_queryset(self, request):
33        if request.user.is_superuser:
34            return super().get_queryset(request)
35        # Show only users from the current user's hospital
36        return super().get_queryset(request).filter(hospital=request.user.hospital)

Admin view for User model.

  • get_queryset: Limits displayed users to those belonging to the admin's hospital unless user is superuser.
def get_queryset(self, request):
32    def get_queryset(self, request):
33        if request.user.is_superuser:
34            return super().get_queryset(request)
35        # Show only users from the current user's hospital
36        return super().get_queryset(request).filter(hospital=request.user.hospital)

Return a QuerySet of all model instances that can be edited by the admin site. This is used by changelist_view.

media
242    def _media(self):
243        # Get the media property of the superclass, if it exists
244        sup_cls = super(cls, self)
245        try:
246            base = sup_cls.media
247        except AttributeError:
248            base = Media()
249
250        # Get the media definition for this class
251        definition = getattr(cls, "Media", None)
252        if definition:
253            extend = getattr(definition, "extend", True)
254            if extend:
255                if extend is True:
256                    m = base
257                else:
258                    m = Media()
259                    for medium in extend:
260                        m += base[medium]
261                return m + Media(definition)
262            return Media(definition)
263        return base