Skip to content

Commit

Permalink
forms: update MuliplePatchForm to acocmodate for the new 'planning_to…
Browse files Browse the repository at this point in the history
…_review' field

Signed-off-by: andrepapoti <[email protected]>
  • Loading branch information
andrepapoti committed Apr 16, 2024
1 parent 81ffed2 commit 6005c30
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
34 changes: 33 additions & 1 deletion patchwork/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,27 @@ class MultiplePatchForm(forms.Form):
empty_value='*',
)

def __init__(self, project, *args, **kwargs):
def __init__(self, project, user=None, *args, **kwargs):
super(MultiplePatchForm, self).__init__(*args, **kwargs)
self.fields['delegate'] = OptionalModelChoiceField(
queryset=_get_delegate_qs(project=project), required=False
)
self.fields['state'] = OptionalModelChoiceField(
queryset=State.objects.all()
)
self.user = user
if self.user:
self.fields['review_status'] = OptionalBooleanField(
choices=[
('*', 'no change'),
('True', 'Planning to review'),
('False', 'Not planning to review'),
],
coerce=lambda x: x == 'True',
empty_value='*',
required=False,
initial='*',
)

def save(self, instance, commit=True):
opts = instance.__class__._meta
Expand All @@ -219,8 +232,27 @@ def save(self, instance, commit=True):
if field.is_no_change(data[f.name]):
continue

if f.name == 'review_status':
if data[f.name]:
self.instance.planning_to_review.add(self.user)
else:
self.instance.planning_to_review.remove(self.user)
continue

setattr(instance, f.name, data[f.name])

if commit:
instance.save()
return instance

def review_status_only(self):
review_status_only = True
field_names = set(self.fields.keys())
field_names.discard({'review_status', 'action'})

for field_name in field_names:
data = self.data.get(field_name, '*')
if data != '*':
review_status_only = False

return review_status_only
5 changes: 3 additions & 2 deletions patchwork/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions patchwork/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.review_status_only()):
errors.append(
"You don't have permissions to edit patch '%s'" % patch.name
)
Expand Down

0 comments on commit 6005c30

Please sign in to comment.