blob: 5d8e640efd6c60ef375a62d688f49db3ebf11bb4 [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
56 address = StringField(_("address"))
57 phone = StringField(_("phone"))
58 institution = StringField(_("institution"))
59 # recaptcha = RecaptchaField()
60
61 def __init__(self, *args, **kwargs):
62 Form.__init__(self, *args, **kwargs)
63
64 def validate(self):
65 if not Form.validate(self):
66 return False
67
68 # user = User.query.filter_by(email = self.email.data.lower()).first()
69 # if user:
70 # self.email.errors.append("That email is already taken")
71 # return False
72 # else:
73 return True
74
75
76class ProfileForm(Form):
77 username = ReadonlyStringField(_("username"))
78 email = StringField(_("email"), [validators.DataRequired(_("enter.email")),
79 validators.Email(_("enter.valid.email"))])
80 firstName = StringField(_("firstName"), [validators.DataRequired(_("enter.firstName"))])
81 lastName = StringField(_("lastName"), [validators.DataRequired(_("enter.lastName"))])
82
83 address = StringField(_("address"))
84 phone = StringField(_("phone"))
85 institution = StringField(_("institution"))
86
87 def __init__(self, *args, **kwargs):
88 Form.__init__(self, *args, **kwargs)
89
90 def validate(self):
91 if not Form.validate(self):
92 return False
93 return True
94
95
96class PasswordReset(Form):
97 pass
98
99
100class PasswordChangeForm(Form):
101 pass
102
103
104class UserEncoder(JSONEncoder):
105 def default(self, obj):
106 if isinstance(obj, User):
107 data = {}
108 if obj.firstName:
109 data['firstName'] = obj.firstName
110 if obj.lastName:
111 data['lastName'] = obj.lastName
112 if obj.email:
113 data['email'] = obj.email
114 if obj.country:
115 data['country'] = obj.country
116 if obj.institution:
117 data['institution'] = obj.institution
118 if obj.address:
119 data['address'] = obj.address
120 if obj.phone:
121 data['phone'] = obj.phone
122 return data
123 return super(UserEncoder, self).default(obj)
124
125
126
127
128class AdminView(BaseView):
129 @expose('/')
130 def index(self):
131 return self.render('admin/index.html')