Coverage for src/django_resume/admin.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-13 13:17 +0200

1from django.contrib import admin 

2 

3from .models import Resume 

4from .plugins import plugin_registry 

5 

6 

7class ResumeAdmin(admin.ModelAdmin): 

8 # fields = ("name", "slug", "plugin_data") 

9 

10 def get_urls(self): 

11 urls = super().get_urls() 

12 custom_urls = [] 

13 for plugin in plugin_registry.get_all_plugins(): 

14 custom_urls.extend(plugin.get_admin_urls(self.admin_site.admin_view)) 

15 return custom_urls + urls 

16 

17 def add_plugin_method(self, plugin): 

18 """ 

19 Add a method to the admin class that will return a link to the plugin admin view. 

20 This is used to have the plugins show up as readonly fields in the resume change view. 

21 """ 

22 

23 def plugin_method(_self, obj): 

24 admin_link = plugin.get_admin_link(obj.id) 

25 return admin_link 

26 

27 plugin_method.__name__ = plugin.name 

28 setattr(self.__class__, plugin.name, plugin_method) 

29 

30 def get_readonly_fields(self, request, obj=None): 

31 """Add a readonly field for each plugin.""" 

32 print("get_readonly_fields called!") 

33 readonly_fields = list(super().get_readonly_fields(request, obj)) 

34 # Filter out all plugins already in readonly_fields - somehow this method is getting called multiple times 

35 readonly_fields_lookup = set(readonly_fields) 

36 new_plugins = [ 

37 p 

38 for p in plugin_registry.get_all_plugins() 

39 if p.name not in readonly_fields_lookup 

40 ] 

41 for plugin in new_plugins: 

42 readonly_fields.append(plugin.name) 

43 self.add_plugin_method(plugin) 

44 return readonly_fields 

45 

46 

47admin.site.register(Resume, ResumeAdmin)