Coverage for src/comments_moderation/forms.py: 85%
39 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 12:11 +0000
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-03 12:11 +0000
1from django import forms
3from ptf_tools.forms import InviteUserForm
5from .models import CommentModerator
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)
13 def __init__(self, collection_choices: tuple, *args, **kwargs):
14 super().__init__(*args, **kwargs)
15 self.fields["collections"].choices = collection_choices
18class StaffModeratorForm(MultipleCollectionForm, forms.Form):
19 """
20 Generic form for adding a staff comment moderator.
21 """
23 moderator_id = forms.IntegerField(required=True, widget=forms.HiddenInput)
26class CommentIdForm(forms.Form):
27 comment_id = forms.IntegerField(required=True, widget=forms.HiddenInput)
29 def __init__(self, *args, **kwargs):
30 super().__init__(*args, **kwargs)
31 self.fields["comment_id"].widget.attrs["readonly"] = True
34class AddBaseModeratorForm(CommentIdForm):
35 """
36 Generic form for adding a basic comment moderator.
37 """
39 moderator_id = forms.IntegerField(required=True, widget=forms.HiddenInput)
42class RemoveBaseModeratorForm(forms.Form):
43 required_css_class = "required"
45 moderators = forms.MultipleChoiceField(
46 label="moderators", widget=forms.CheckboxSelectMultiple, required=True
47 )
48 comment_id = forms.IntegerField(required=True, widget=forms.HiddenInput)
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
57class UserCommentModeratorForm(forms.ModelForm):
58 class Meta:
59 model = CommentModerator
60 fields = ["user", "is_moderator", "collections"]
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
72class InviteBaseModeratorForm(CommentIdForm, InviteUserForm):
73 pass
76class InviteStaffModeratorForm(MultipleCollectionForm, InviteUserForm):
77 collections = forms.MultipleChoiceField(required=True, widget=forms.CheckboxSelectMultiple)