Skip to content

Commit

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

Signed-off-by: andrepapoti <[email protected]>
  • Loading branch information
andrepapoti committed Apr 1, 2024
1 parent 3bdf140 commit 0f7d391
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
30 changes: 30 additions & 0 deletions patchwork/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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.declare_interest_only()):
errors.append(
"You don't have permissions to edit patch '%s'" % patch.name
)
Expand Down

0 comments on commit 0f7d391

Please sign in to comment.