Coverage for benefits/admin.py: 96%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-21 19:31 +0000

1from django.conf import settings 

2from django.contrib import admin 

3from django.contrib.auth.models import Group 

4from django.template.response import TemplateResponse 

5from benefits.core.models import TransitAgency 

6from benefits.core import session 

7 

8 

9class BenefitsAdminSite(admin.AdminSite): 

10 

11 site_title = "Cal-ITP Benefits Administrator" 

12 site_header = "Administrator" 

13 index_title = "Dashboard" 

14 

15 def index(self, request, extra_context=None): 

16 """ 

17 Display the main admin index page if the user is a superuser or a "staff_group" user. 

18 Display the agency dashboard index page if the user is an agency user. 

19 get_app_list returns a sorted list of all the installed apps that have been 

20 registered in this site. 

21 """ 

22 app_list = self.get_app_list(request) 

23 

24 context = { 

25 **self.each_context(request), 

26 "title": self.index_title, 

27 "subtitle": None, 

28 "app_list": app_list, 

29 **(extra_context or {}), 

30 } 

31 

32 request.current_app = self.name 

33 

34 staff_group = Group.objects.get(name=settings.STAFF_GROUP_NAME) 

35 if request.user.is_superuser or request.user.groups.filter(name=staff_group).exists(): 

36 return TemplateResponse(request, "admin/index.html", context) 

37 else: 

38 agency = TransitAgency.for_user(request.user) 

39 session.update(request, agency=agency) 

40 

41 if agency is not None: 41 ↛ 53line 41 didn't jump to line 53 because the condition on line 41 was always true

42 has_permission_for_in_person = agency.customer_service_group in request.user.groups.all() 

43 transit_processor_portal_url = agency.transit_processor.portal_url 

44 

45 context.update( 

46 { 

47 "has_permission_for_in_person": has_permission_for_in_person, 

48 "transit_processor_portal_url": transit_processor_portal_url, 

49 "title": f"{agency.long_name} | {self.index_title} | {self.site_title}", 

50 } 

51 ) 

52 

53 return TemplateResponse(request, "admin/agency-dashboard-index.html", context)