Coverage for product_risk_suite/scraper/admin.py: 84%

32 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-16 14:44 +0000

1from django import forms 

2from django.contrib import admin 

3 

4from .models import AssigneeMapping, RemoteCredential, Scraper, ScraperStatusMapping 

5 

6 

7class RemoteCredentialForm(forms.ModelForm): 

8 password = forms.CharField( 

9 required=False, 

10 widget=forms.PasswordInput(render_value=False), 

11 help_text="Leave blank to keep the existing password.", 

12 ) 

13 api_token = forms.CharField( 

14 required=False, 

15 widget=forms.PasswordInput(render_value=False), 

16 help_text="Leave blank to keep the existing API token.", 

17 ) 

18 

19 class Meta: 

20 model = RemoteCredential 

21 fields = ["name", "url", "auth_type", "username", "password", "api_token"] 

22 

23 def _post_clean(self): 

24 # apply new secrets onto the instance *before* ModelForm._post_clean() runs 

25 # instance.full_clean() (and therefore RemoteCredential.clean()), so validation 

26 # sees the values actually being saved rather than the pre-edit state. 

27 if self.cleaned_data.get("password"): 

28 self.instance.password = self.cleaned_data["password"] 

29 if self.cleaned_data.get("api_token"): 

30 self.instance.api_token = self.cleaned_data["api_token"] 

31 super()._post_clean() 

32 

33 

34@admin.register(RemoteCredential) 

35class RemoteCredentialAdmin(admin.ModelAdmin): 

36 form = RemoteCredentialForm 

37 list_display = ["name", "url", "auth_type", "username"] 

38 

39 

40class ScraperStatusMappingInline(admin.TabularInline): 

41 model = ScraperStatusMapping 

42 extra = 1 

43 

44 

45@admin.register(Scraper) 

46class ScraperAdmin(admin.ModelAdmin): 

47 list_display = ["name", "credential", "response_format"] 

48 inlines = [ScraperStatusMappingInline] 

49 

50 

51@admin.register(AssigneeMapping) 

52class AssigneeMappingAdmin(admin.ModelAdmin): 

53 list_display = ["remote_username", "base_url", "user"] 

54 list_filter = ["base_url"] 

55 search_fields = ["remote_username", "base_url", "user__username", "user__email"] 

56 autocomplete_fields = ["user"]