Skip to content

Commit 4e5e858

Browse files
committed
forms: update MuliplePatchForm to acocmodate for the new 'interested_users' field
Signed-off-by: andrepapoti <[email protected]>
1 parent 3bdf140 commit 4e5e858

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

patchwork/forms.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,27 @@ class MultiplePatchForm(forms.Form):
190190
empty_value='*',
191191
)
192192

193-
def __init__(self, project, *args, **kwargs):
193+
def __init__(self, project, user=None, *args, **kwargs):
194194
super(MultiplePatchForm, self).__init__(*args, **kwargs)
195195
self.fields['delegate'] = OptionalModelChoiceField(
196196
queryset=_get_delegate_qs(project=project), required=False
197197
)
198198
self.fields['state'] = OptionalModelChoiceField(
199199
queryset=State.objects.all()
200200
)
201+
self.user = user
202+
if self.user:
203+
self.fields['declare_interest'] = OptionalBooleanField(
204+
choices=[
205+
('*', 'no change'),
206+
('True', 'Interested'),
207+
('False', 'Not interested'),
208+
],
209+
coerce=lambda x: x == 'True',
210+
empty_value='*',
211+
required=False,
212+
initial='*',
213+
)
201214

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

235+
if f.name == 'declare_interest':
236+
if data[f.name]:
237+
self.instance.interested_users.add(self.user)
238+
else:
239+
self.instance.interested_users.remove(self.user)
240+
continue
241+
222242
setattr(instance, f.name, data[f.name])
223243

224244
if commit:
225245
instance.save()
226246
return instance
247+
248+
def declare_interest_only(self):
249+
interest_only = True
250+
field_names = set(self.fields.keys())
251+
field_names.discard({'declare_interest', 'action'})
252+
253+
for field_name in field_names:
254+
data = self.data.get(field_name, '*')
255+
if data != '*':
256+
interest_only = False
257+
258+
return interest_only

patchwork/models.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def save(self, *args, **kwargs):
582582

583583
self.refresh_tag_counts()
584584

585-
def is_editable(self, user):
585+
def is_editable(self, user, declare_interest_only=False):
586586
if not user.is_authenticated:
587587
return False
588588

@@ -593,7 +593,8 @@ def is_editable(self, user):
593593
if self.project.is_editable(user):
594594
self._edited_by = user
595595
return True
596-
return False
596+
597+
return declare_interest_only
597598

598599
@staticmethod
599600
def filter_unique_checks(checks):

patchwork/views/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ def generic_list(
240240
if data and data.get('form', '') == 'patchlistform':
241241
data_tmp = data
242242

243-
properties_form = MultiplePatchForm(project, data=data_tmp)
243+
properties_form = MultiplePatchForm(
244+
project, data=data_tmp, user=request.user
245+
)
244246

245247
if request.method == 'POST' and data.get('form') == 'patchlistform':
246248
action = data.get('action', '').lower()
@@ -340,7 +342,7 @@ def process_multiplepatch_form(request, form, action, patches, context):
340342

341343
changed_patches = 0
342344
for patch in patches:
343-
if not patch.is_editable(request.user):
345+
if not patch.is_editable(request.user, form.declare_interest_only()):
344346
errors.append(
345347
"You don't have permissions to edit patch '%s'" % patch.name
346348
)

0 commit comments

Comments
 (0)