-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
forms.py
91 lines (67 loc) · 2.49 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""Forms for core app."""
from __future__ import (
absolute_import, division, print_function, unicode_literals)
import logging
from builtins import object
from django import forms
from django.contrib.auth.models import User
from django.forms.fields import CharField
from django.utils.translation import ugettext_lazy as _
from .models import UserProfile
log = logging.getLogger(__name__)
class UserProfileForm(forms.ModelForm):
first_name = CharField(label=_('First name'), required=False)
last_name = CharField(label=_('Last name'), required=False)
class Meta(object):
model = UserProfile
# Don't allow users edit someone else's user page
fields = ['first_name', 'last_name', 'homepage']
def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
try:
self.fields['first_name'].initial = self.instance.user.first_name
self.fields['last_name'].initial = self.instance.user.last_name
except AttributeError:
pass
def save(self, commit=True):
first_name = self.cleaned_data.pop('first_name', None)
last_name = self.cleaned_data.pop('last_name', None)
profile = super(UserProfileForm, self).save(commit=commit)
if commit:
user = profile.user
user.first_name = first_name
user.last_name = last_name
user.save()
return profile
class UserDeleteForm(forms.ModelForm):
username = CharField(
label=_('Username'),
help_text=_('Please type your username to confirm.'),
)
class Meta(object):
model = User
fields = ['username']
def clean_username(self):
data = self.cleaned_data['username']
if self.instance.username != data:
raise forms.ValidationError(_('Username does not match!'))
return data
class UserAdvertisingForm(forms.ModelForm):
class Meta(object):
model = UserProfile
fields = ['allow_ads']
class FacetField(forms.MultipleChoiceField):
"""
For filtering searches on a facet.
Has validation for the format of facet values.
"""
def valid_value(self, value):
"""
Although this is a choice field, no choices need to be supplied.
Instead, we just validate that the value is in the correct format for
facet filtering (facet_name:value)
"""
if ':' not in value:
return False
return True