Coverage for sites/ptf_tools/comments_moderation/forms.py: 85%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-06-19 08:36 +0000

1from django import forms 

2 

3from ptf_tools.forms import InviteUserForm 

4 

5from .models import CommentModerator 

6 

7 

8class MultipleCollectionForm(forms.Form): 

9 # To be initialized by each instance of the form 

10 # Not required when the user unselect all 

11 collections = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple) 

12 

13 def __init__(self, collection_choices: tuple, *args, **kwargs): 

14 super().__init__(*args, **kwargs) 

15 self.fields["collections"].choices = collection_choices 

16 

17 

18class StaffModeratorForm(MultipleCollectionForm, forms.Form): 

19 """ 

20 Generic form for adding a staff comment moderator. 

21 """ 

22 

23 moderator_id = forms.IntegerField(required=True, widget=forms.HiddenInput) 

24 

25 

26class CommentIdForm(forms.Form): 

27 comment_id = forms.IntegerField(required=True, widget=forms.HiddenInput) 

28 

29 def __init__(self, *args, **kwargs): 

30 super().__init__(*args, **kwargs) 

31 self.fields["comment_id"].widget.attrs["readonly"] = True 

32 

33 

34class AddBaseModeratorForm(CommentIdForm): 

35 """ 

36 Generic form for adding a basic comment moderator. 

37 """ 

38 

39 moderator_id = forms.IntegerField(required=True, widget=forms.HiddenInput) 

40 

41 

42class RemoveBaseModeratorForm(forms.Form): 

43 required_css_class = "required" 

44 

45 moderators = forms.MultipleChoiceField( 

46 label="moderators", widget=forms.CheckboxSelectMultiple, required=True 

47 ) 

48 comment_id = forms.IntegerField(required=True, widget=forms.HiddenInput) 

49 

50 def __init__(self, *args, **kwargs): 

51 moderators = kwargs.pop("moderators") 

52 super().__init__(*args, **kwargs) 

53 self.fields["comment_id"].widget.attrs["readonly"] = True 

54 self.fields["moderators"].choices = moderators 

55 

56 

57class UserCommentModeratorForm(forms.ModelForm): 

58 class Meta: 

59 model = CommentModerator 

60 fields = ["user", "is_moderator", "collections"] 

61 

62 def clean(self) -> dict: 

63 cleaned_data = super().clean() 

64 if cleaned_data["collections"] and not cleaned_data["is_moderator"]: 

65 self.add_error( 

66 "is_moderator", 

67 "Error: the user must be flagged as a moderator to have" "moderating collections.", 

68 ) 

69 return cleaned_data 

70 

71 

72class InviteBaseModeratorForm(CommentIdForm, InviteUserForm): 

73 pass 

74 

75 

76class InviteStaffModeratorForm(MultipleCollectionForm, InviteUserForm): 

77 collections = forms.MultipleChoiceField(required=True, widget=forms.CheckboxSelectMultiple)