From 0f7d39179162e36d51f90454e486925aa37ae8e2 Mon Sep 17 00:00:00 2001 From: andrepapoti Date: Mon, 1 Apr 2024 10:46:26 -0300 Subject: [PATCH] forms: update MuliplePatchForm to acocmodate for the new 'interested_users' field Signed-off-by: andrepapoti --- patchwork/forms.py | 30 ++++++++++++++++++++++++++++++ patchwork/models.py | 5 +++-- patchwork/views/__init__.py | 6 ++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/patchwork/forms.py b/patchwork/forms.py index ed06d0d1..999ca9a5 100644 --- a/patchwork/forms.py +++ b/patchwork/forms.py @@ -198,6 +198,17 @@ def __init__(self, project, *args, **kwargs): self.fields['state'] = OptionalModelChoiceField( queryset=State.objects.all() ) + self.user = kwargs.get('user') + if self.user: + self.fields['declare_interest'] = OptionalBooleanField( + choices=[ + ('*', 'no change'), + ('True', 'Interested'), + ('False', 'Not interested'), + ], + coerce=lambda x: x == 'True', + empty_value='*', + ) def save(self, instance, commit=True): opts = instance.__class__._meta @@ -219,8 +230,27 @@ def save(self, instance, commit=True): if field.is_no_change(data[f.name]): continue + if f.name == 'declare_interest': + if data[f.name]: + self.instance.interested_users.add(self.user) + else: + self.instance.interested_users.remove(self.user) + continue + setattr(instance, f.name, data[f.name]) if commit: instance.save() return instance + + def declare_interest_only(self): + interest_only = True + field_names = set(self.fields.keys()) + field_names.remove({'declare_interest', 'action'}) + + for field_name in field_names: + data = self.data.get(field_name, '*') + if data != '*': + interest_only = False + + return interest_only diff --git a/patchwork/models.py b/patchwork/models.py index 01b2e7ec..24e87756 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -582,7 +582,7 @@ def save(self, *args, **kwargs): self.refresh_tag_counts() - def is_editable(self, user): + def is_editable(self, user, declare_interest_only=False): if not user.is_authenticated: return False @@ -593,7 +593,8 @@ def is_editable(self, user): if self.project.is_editable(user): self._edited_by = user return True - return False + + return declare_interest_only @staticmethod def filter_unique_checks(checks): diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index 704ab815..5ddbd480 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -240,7 +240,9 @@ def generic_list( if data and data.get('form', '') == 'patchlistform': data_tmp = data - properties_form = MultiplePatchForm(project, data=data_tmp) + properties_form = MultiplePatchForm( + project, data=data_tmp, user=request.user + ) if request.method == 'POST' and data.get('form') == 'patchlistform': action = data.get('action', '').lower() @@ -340,7 +342,7 @@ def process_multiplepatch_form(request, form, action, patches, context): changed_patches = 0 for patch in patches: - if not patch.is_editable(request.user): + if not patch.is_editable(request.user, form.declare_interest_only()): errors.append( "You don't have permissions to edit patch '%s'" % patch.name )