Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.utils.safestring import mark_safe 

2from django.contrib import admin 

3from django.urls import reverse 

4 

5from wv_dvs.admin import ModelAdmin 

6from report.models import Report 

7from report.forms import ReportAdminForm 

8from .models import Project, District, Municipality 

9 

10 

11class ReportInline(admin.TabularInline): 

12 exclude = ('data',) 

13 show_change_link = True 

14 model = Report 

15 form = ReportAdminForm 

16 extra = 0 

17 

18 def has_delete_permission(self, request, obj=None): 

19 return False 

20 

21 

22@admin.register(Project) 

23class ProjectAdmin(ModelAdmin): 

24 inlines = (ReportInline,) 

25 search_fields = ('name', 'number',) 

26 list_display = ( 

27 'name', 'number', 'long', 'lat', 'recent_report', 'get_district', 

28 ) 

29 list_filter = ('district',) 

30 autocomplete_fields = ('district',) 

31 filter_horizontal = ('municipalities',) 

32 save_on_top = True 

33 

34 def get_queryset(self, request): 

35 return super().get_queryset(request).prefetch_related('reports', 'district') 

36 

37 def get_district(self, instance): 

38 district = instance.district 

39 if district: 

40 link = reverse('admin:project_district_change', args=(district.id,)) 

41 return mark_safe('<a href="%s">%s</a>' % (link, district)) 

42 

43 get_district.short_description = 'District' 

44 

45 def recent_report(self, instance): 

46 report = instance.recent_report 

47 if report: 

48 link = reverse('admin:report_report_change', args=(report.id,)) 

49 return mark_safe('<a href="%s">%s</a>' % (link, report.name)) 

50 

51 recent_report.short_description = 'Recent Report' 

52 

53 

54@admin.register(District) 

55class DistrictAdmin(ModelAdmin): 

56 search_fields = ('name', 'code') 

57 list_display = ('name', 'code') 

58 ordering = ('name', 'code') 

59 

60 

61@admin.register(Municipality) 

62class MunicipalityAdmin(ModelAdmin): 

63 list_display = ('name', 'code', 'get_district') 

64 search_fields = ('name', 'code') 

65 ordering = ('name', 'code') 

66 list_filter = ('district',) 

67 autocomplete_fields = ('district',) 

68 

69 def get_district(self, instance): 

70 district = instance.district 

71 if district: 

72 link = reverse('admin:project_district_change', args=(district.id,)) 

73 return mark_safe('<a href="%s">%s</a>' % (link, district)) 

74 

75 get_district.short_description = 'District'