Merge lp:~rvb/maas/bad-msg-bug-1301465 into lp:~maas-committers/maas/trunk

Proposed by Raphaël Badin
Status: Merged
Approved by: Raphaël Badin
Approved revision: no longer in the source branch.
Merged at revision: 2365
Proposed branch: lp:~rvb/maas/bad-msg-bug-1301465
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 44 lines (+15/-5)
1 file modified
src/maasserver/forms.py (+15/-5)
To merge this branch: bzr merge lp:~rvb/maas/bad-msg-bug-1301465
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) Approve
Review via email: mp+220938@code.launchpad.net

Commit message

Fix NodeForm's is_valid() method so that it uses Django's way of setting errors on forms instead of putting text in self.errors['architecture'].

To post a comment you must log in.
Revision history for this message
Jeroen T. Vermeulen (jtv) wrote :

Nice touch, thanks.

There's one change I'd like to ask for: error_key looks mysterious, as if you're giving up on encapsulation there and punting responsibility for explanation to Django. Would it be better called field_name?

I suppose there would be no point in checking for pre-existing errors on the same field, and appending instead of setting — although Django's API for this does look as if it was designed that way, sort of.

review: Approve
Revision history for this message
Raphaël Badin (rvb) wrote :

> Nice touch, thanks.
>
> There's one change I'd like to ask for: error_key looks mysterious, as if
> you're giving up on encapsulation there and punting responsibility for
> explanation to Django. Would it be better called field_name?

Good point, done.

> I suppose there would be no point in checking for pre-existing errors on the
> same field, and appending instead of setting — although Django's API for this
> does look as if it was designed that way, sort of.

Not really, this is meant to "highjack" the error message. I've improved the docstring to be really clear about this.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/forms.py'
2--- src/maasserver/forms.py 2014-05-20 14:16:58 +0000
3+++ src/maasserver/forms.py 2014-05-26 14:15:15 +0000
4@@ -122,6 +122,17 @@
5 BLANK_CHOICE = ('', '-------')
6
7
8+def set_form_error(form, field_name, error_value):
9+ """Set an error on a form's field.
10+
11+ This utility method encapsulates Django's arguably awkward way
12+ of settings errors inside a form's clean()/is_valid() method. This
13+ method will override any previously-registered error for 'field_name'.
14+ """
15+ # Hey Django devs, this is a crap API to set errors.
16+ form.errors[field_name] = form.error_class([error_value])
17+
18+
19 def remove_None_values(data):
20 """Return a new dictionary without the keys corresponding to None values.
21 """
22@@ -253,8 +264,8 @@
23 def is_valid(self):
24 is_valid = super(NodeForm, self).is_valid()
25 if len(list_all_usable_architectures()) == 0:
26- self.errors['architecture'] = (
27- [NO_ARCHITECTURES_AVAILABLE])
28+ set_form_error(
29+ self, "architecture", NO_ARCHITECTURES_AVAILABLE)
30 is_valid = False
31 return is_valid
32
33@@ -424,9 +435,8 @@
34 try:
35 get_power_types([self._get_nodegroup()])
36 except ClusterUnavailable as e:
37- # Hey Django devs, this is a crap API to set errors.
38- self._errors["power_type"] = self.error_class(
39- [CLUSTER_NOT_AVAILABLE + e.args[0]])
40+ set_form_error(
41+ self, "power_type", CLUSTER_NOT_AVAILABLE + e.args[0])
42 # If power_type is not set and power_parameters_skip_check is not
43 # on, reset power_parameters (set it to the empty string).
44 no_power_type = cleaned_data.get('power_type', '') == ''