| | 17 | |
| | 18 | #_____________________________________________________________________________ |
| | 19 | # preferences |
| | 20 | |
| | 21 | class WeightField(forms.CharField): |
| | 22 | def clean(self, value): |
| | 23 | """ |
| | 24 | TODO: Check the data. |
| | 25 | """ |
| | 26 | # Validates max_length and min_length |
| | 27 | value = super(WeightField, self).clean(value) |
| | 28 | |
| | 29 | try: |
| | 30 | data = data_eval(value) |
| | 31 | except DataEvalError, err: |
| | 32 | raise ValidationError("Can't evaluate data: %s" % err) |
| | 33 | |
| | 34 | for section_name, weight in data.iteritems(): |
| | 35 | if not isinstance(section_name, basestring): |
| | 36 | raise ValidationError("Wrong section name '%r'" % section_name) |
| | 37 | if not isinstance(weight, int): |
| | 38 | raise ValidationError("Wrong weight for '%s'" % section_name) |
| | 39 | if weight<-10 or weight>10: |
| | 40 | msg = ( |
| | 41 | "weight for '%s' out of range (min: -10, max: 10)" |
| | 42 | ) % section_name |
| | 43 | raise ValidationError(msg) |
| | 44 | |
| | 45 | return data |
| | 46 | |
| | 47 | |
| | 48 | class PreferencesForm(forms.Form): |
| | 49 | # TODO: Find a better way to create a better sized form |
| | 50 | section_weights = WeightField( |
| | 51 | min_length = 2, |
| | 52 | help_text=_( |
| | 53 | "Weigth for every admin sub menu section entry. (ascending order) " |
| | 54 | ), |
| | 55 | widget=forms.Textarea(attrs={'rows': '10'}), |
| | 56 | initial= ( |
| | 57 | '{' |
| | 58 | ' "page admin": -5,' |
| | 59 | ' "edit look": 0,' |
| | 60 | ' "user management": 3,' |
| | 61 | ' "miscellaneous": 6,' |
| | 62 | ' "setup": 8,' |
| | 63 | '}' |
| | 64 | ) |
| | 65 | ) |