blob: f024eb94d06b8fb9c342a6b84549d71984fb7b2f [file] [log] [blame]
Akronca9bd982016-12-06 16:59:57 +01001from flask.json import JSONEncoder
2from flask_admin import BaseView, expose
3from flask_babel import lazy_gettext as _
4
5from providers import User
6
7
8__author__ = 'hanl'
9
10from flask_wtf import Form
11from wtforms import PasswordField, StringField, validators, RadioField
12
13CHOICES = [('male', _('male')), ('female', _('female'))]
14
15
16class ReadonlyStringField(StringField):
17 def __call__(self, *args, **kwargs):
18 kwargs.setdefault('readonly', True)
19 # c = kwargs.pop('class', '') or kwargs.pop('class_', '')
20 #kwargs['class'] = u'%s %s' % ('form-control-static', c)
21 return super(ReadonlyStringField, self).__call__(*args, **kwargs)
22
23
24class SigninForm(Form):
25 username = StringField(_("username"), [validators.DataRequired(_("enter.username"))])
26 password = PasswordField(_('password'), [validators.DataRequired(_("enter.password"))])
27 # submit = SubmitField("Sign in")
28
29 def __init__(self, *args, **kwargs):
30 Form.__init__(self, *args, **kwargs)
31
32 def validate(self):
33 if not Form.validate(self):
34 return False
35 # user = User.query.filter_by(email = self.email.data.lower()).first()
36 # if user and user.check_password(self.password.data):
37 return True
38 # else:
39 # self.email.errors.append("Invalid e-mail or password")
40 # return False
41
42
43class SignupForm(Form):
44 gender = RadioField(_("gender"), validators=[validators.DataRequired()], choices=CHOICES)
45 username = StringField(_("username"), [validators.DataRequired(_("enter.username"))])
46 password = PasswordField(_("password"), [validators.DataRequired(_("enter.password")),
47 validators.EqualTo("confirm", message=_("password.match"))])
48 confirm = PasswordField(_("confirm"))
49 firstName = StringField(_("firstName"), [validators.DataRequired(_("enter.firstName"))])
50 lastName = StringField(_("lastName"), [validators.DataRequired(_("enter.lastName"))])
51
52 email = StringField(_("email"), [validators.DataRequired(_("enter.email")),
53 validators.Email(_("enter.valid.email"))])
54
55 address = StringField(_("address"))
56 phone = StringField(_("phone"))
57 institution = StringField(_("institution"))
58 # recaptcha = RecaptchaField()
59
60 def __init__(self, *args, **kwargs):
61 Form.__init__(self, *args, **kwargs)
62
63 def validate(self):
64 if not Form.validate(self):
65 return False
66
67 # user = User.query.filter_by(email = self.email.data.lower()).first()
68 # if user:
69 # self.email.errors.append("That email is already taken")
70 # return False
71 # else:
72 return True
73
74
75class ProfileForm(Form):
76 username = ReadonlyStringField(_("username"))
77 email = StringField(_("email"), [validators.DataRequired(_("enter.email")),
78 validators.Email(_("enter.valid.email"))])
79 firstName = StringField(_("firstName"), [validators.DataRequired(_("enter.firstName"))])
80 lastName = StringField(_("lastName"), [validators.DataRequired(_("enter.lastName"))])
81
82 address = StringField(_("address"))
83 phone = StringField(_("phone"))
84 institution = StringField(_("institution"))
85
86 def __init__(self, *args, **kwargs):
87 Form.__init__(self, *args, **kwargs)
88
89 def validate(self):
90 if not Form.validate(self):
91 return False
92 return True
93
94
95class PasswordReset(Form):
96 username = ReadonlyStringField(_("username"))
97 email = StringField(_("email"), [validators.DataRequired(_("enter.email")),
98 validators.Email(_("enter.valid.email"))])
99
100
101class PasswordChangeForm(Form):
102 password = PasswordField(_("password"), [validators.DataRequired(_("enter.password")),
103 validators.EqualTo("confirm", message=_("password.match"))])
104 confirm = PasswordField(_("confirm"))
105 pass
106
107
108class UserEncoder(JSONEncoder):
109 def default(self, obj):
110 if isinstance(obj, User):
111 data = {}
112 if obj.username:
113 data['username'] = obj.username
114 if obj.firstName:
115 data['firstName'] = obj.firstName
116 if obj.lastName:
117 data['lastName'] = obj.lastName
118 if obj.email:
119 data['email'] = obj.email
120 if obj.country:
121 data['country'] = obj.country
122 if obj.institution:
123 data['institution'] = obj.institution
124 if obj.address:
125 data['address'] = obj.address
126 if obj.phone:
127 data['phone'] = obj.phone
128 return data
129 return super(UserEncoder, self).default(obj)
130
131
132class AdminView(BaseView):
133
134 @expose('/')
135 def index(self):
136 print "rendering an admin view"
137 return self.render('index.html')
138
139 @expose('/test')
140 def test(self):
141 print "rendering an admin view"
142 return self.render('index.html')