management.admin

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

Admin view for Blocks model.

  • get_queryset: Limits displayed blocks to those belonging to the admin's hospital 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        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
class ShiftAdmin(django.contrib.admin.options.ModelAdmin):
25class ShiftAdmin(admin.ModelAdmin):
26    """
27    Admin view for Shift model.
28
29    - get_queryset: Limits displayed shifts 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        return super().get_queryset(request).filter(hospital=request.user.hospital)

Admin view for Shift model.

  • get_queryset: Limits displayed shifts to those belonging to the admin's hospital unless user is superuser.
def get_queryset(self, request):
31    def get_queryset(self, request):
32        if request.user.is_superuser:
33            return super().get_queryset(request)
34        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