Mercurial > personas_backend
annotate PersonasBackend/personas/forms.py @ 95:b42e3997fe58
Refactored and simplified validation logic.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 27 Mar 2008 20:17:22 -0700 |
parents | e1bfeffce0f0 |
children | 305fb3a1e0ca |
rev | line source |
---|---|
94
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
1 import django.newforms as forms |
27
4439a83c5dc6
Added a trivial new/edit form using the django.newforms system.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
2 from PersonasBackend.personas import models |
4439a83c5dc6
Added a trivial new/edit form using the django.newforms system.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
3 |
94
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
4 class PersonaForm( forms.ModelForm ): |
64
5f21f8d3334b
Only users who have the personas.can_publish permission or who own a persona can edit them now; added more workflow logic. I'm not entirely happy with the way the workflow logic is structured right now, but this seems to work okay for the time being.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
5 """ |
5f21f8d3334b
Only users who have the personas.can_publish permission or who own a persona can edit them now; added more workflow logic. I'm not entirely happy with the way the workflow logic is structured right now, but this seems to work okay for the time being.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
6 Form given to normal users who don't have the permission to |
5f21f8d3334b
Only users who have the personas.can_publish permission or who own a persona can edit them now; added more workflow logic. I'm not entirely happy with the way the workflow logic is structured right now, but this seems to work okay for the time being.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
7 publish Personas. |
5f21f8d3334b
Only users who have the personas.can_publish permission or who own a persona can edit them now; added more workflow logic. I'm not entirely happy with the way the workflow logic is structured right now, but this seems to work okay for the time being.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
8 """ |
5f21f8d3334b
Only users who have the personas.can_publish permission or who own a persona can edit them now; added more workflow logic. I'm not entirely happy with the way the workflow logic is structured right now, but this seems to work okay for the time being.
Atul Varma <varmaa@toolness.com>
parents:
63
diff
changeset
|
9 |
27
4439a83c5dc6
Added a trivial new/edit form using the django.newforms system.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
10 class Meta: |
4439a83c5dc6
Added a trivial new/edit form using the django.newforms system.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
11 model = models.Persona |
87
663f0410ff39
Added a 'date_published' field to the Persona model, which allows us to
Atul Varma <varmaa@toolness.com>
parents:
84
diff
changeset
|
12 exclude = ["owner", "date_published", "popularity", "status"] |
74
ec926b106c68
Consolidated validating of header/footer URL/image stuff so that it's done at both the model and form level without too much DRY violation. I'm not sure how pleased I am with this, but it'll work for now.
Atul Varma <varmaa@toolness.com>
parents:
72
diff
changeset
|
13 |
94
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
14 agree_to_terms = forms.BooleanField( |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
15 label = "I agree to the terms of use.", |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
16 # TODO: setting required to True doesn't seem to have any effect, |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
17 # but the Django docs say it should ensure that the checkbox |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
18 # is filled out. |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
19 required = True, |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
20 help_text = ("Terms of use: I agree that Mozilla is providing " |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
21 "a service by hosting the " |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
22 "content I am submitting, and that they are in no " |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
23 "way responsible for any damages that occur as " |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
24 "a result of hosting said content.") |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
25 ) |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
26 |
92
c5d76a735410
Fixed a validation bug w/ colors
Atul Varma <varmaa@toolness.com>
parents:
91
diff
changeset
|
27 def _color_cleaner( self, field ): |
95
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
28 models.ensure_color_is_valid( |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
29 self.cleaned_data[field], |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
30 error_class = forms.ValidationError |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
31 ) |
92
c5d76a735410
Fixed a validation bug w/ colors
Atul Varma <varmaa@toolness.com>
parents:
91
diff
changeset
|
32 return self.cleaned_data[field] |
91
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
33 |
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
34 def clean_text_color( self ): |
92
c5d76a735410
Fixed a validation bug w/ colors
Atul Varma <varmaa@toolness.com>
parents:
91
diff
changeset
|
35 return self._color_cleaner( "text_color" ) |
91
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
36 |
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
37 def clean_accent_color( self ): |
92
c5d76a735410
Fixed a validation bug w/ colors
Atul Varma <varmaa@toolness.com>
parents:
91
diff
changeset
|
38 return self._color_cleaner( "accent_color" ) |
91
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
39 |
74
ec926b106c68
Consolidated validating of header/footer URL/image stuff so that it's done at both the model and form level without too much DRY violation. I'm not sure how pleased I am with this, but it'll work for now.
Atul Varma <varmaa@toolness.com>
parents:
72
diff
changeset
|
40 def clean( self ): |
91
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
41 form_data = dict( self.cleaned_data ) |
cad201e72d73
Added validation for the text_color and accent_color fields.
Atul Varma <varmaa@toolness.com>
parents:
87
diff
changeset
|
42 form_data.update( self.files ) |
95
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
43 models.ensure_header_and_footer_are_valid( |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
44 form_data, |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
45 error_class = forms.ValidationError |
b42e3997fe58
Refactored and simplified validation logic.
Atul Varma <varmaa@toolness.com>
parents:
94
diff
changeset
|
46 ) |
94
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
47 if not self.cleaned_data["agree_to_terms"]: |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
48 raise forms.ValidationError( |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
49 "You must agree to the terms of service to " |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
50 "submit your Persona." |
e1bfeffce0f0
Added an 'I accept the terms of use' checkbox to Persona submission.
Atul Varma <varmaa@toolness.com>
parents:
92
diff
changeset
|
51 ) |
74
ec926b106c68
Consolidated validating of header/footer URL/image stuff so that it's done at both the model and form level without too much DRY violation. I'm not sure how pleased I am with this, but it'll work for now.
Atul Varma <varmaa@toolness.com>
parents:
72
diff
changeset
|
52 |
ec926b106c68
Consolidated validating of header/footer URL/image stuff so that it's done at both the model and form level without too much DRY violation. I'm not sure how pleased I am with this, but it'll work for now.
Atul Varma <varmaa@toolness.com>
parents:
72
diff
changeset
|
53 return self.cleaned_data |