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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# JARR - A Web based news aggregator.
# Copyright (C) 2010-2016 Cédric Bonhomme - https://www.cedricbonhomme.org
#
# For more information : http://github.com/JARR/JARR
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.3 $"
__date__ = "$Date: 2013/11/05 $"
__revision__ = "$Date: 2015/05/06 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"
from flask import flash, url_for, redirect
from flask_wtf import Form
from flask_babel import lazy_gettext
from werkzeug.exceptions import NotFound
from wtforms import TextField, TextAreaField, PasswordField, BooleanField, \
SubmitField, IntegerField, SelectField, validators, HiddenField
from flask_wtf.html5 import EmailField, URLField
from web.lib import misc_utils
from web.controllers import UserController
from web.models import User
class SignupForm(Form):
"""
Sign up form (registration to jarr).
"""
nickname = TextField(lazy_gettext("Nickname"),
[validators.Required(lazy_gettext("Please enter your nickname."))])
email = EmailField(lazy_gettext("Email"),
[validators.Length(min=6, max=35),
validators.Required(
lazy_gettext("Please enter your email address."))])
password = PasswordField(lazy_gettext("Password"),
[validators.Required(lazy_gettext("Please enter a password.")),
validators.Length(min=6, max=100)])
submit = SubmitField(lazy_gettext("Sign up"))
def validate(self):
ucontr = UserController()
validated = super().validate()
if ucontr.read(nickname=self.nickname.data).count():
self.nickname.errors.append('Nickname already taken')
validated = False
if ucontr.read(email=self.email.data).count():
self.email.errors.append('Email already taken')
validated = False
return validated
class RedirectForm(Form):
"""
Secure back redirects with WTForms.
"""
next = HiddenField()
def __init__(self, *args, **kwargs):
Form.__init__(self, *args, **kwargs)
if not self.next.data:
self.next.data = misc_utils.get_redirect_target() or ''
def redirect(self, endpoint='home', **values):
if misc_utils.is_safe_url(self.next.data):
return redirect(self.next.data)
target = misc_utils.get_redirect_target()
return redirect(target or url_for(endpoint, **values))
class SigninForm(RedirectForm):
"""
Sign in form (connection to jarr).
"""
email_or_nickmane = TextField("Email or nickname",
[validators.Length(min=3, max=35),
validators.Required(
lazy_gettext("Please enter your email address or nickname."))])
password = PasswordField(lazy_gettext('Password'),
[validators.Required(lazy_gettext("Please enter a password.")),
validators.Length(min=6, max=100)])
submit = SubmitField(lazy_gettext("Log In"))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = None
def validate(self):
validated = super().validate()
ucontr = UserController()
try:
user = ucontr.get(**{'__or__':
{'email': self.email_or_nickmane.data,
'nickname': self.email_or_nickmane.data}})
except NotFound:
self.email_or_nickmane.errors.append(
'Wrong email address or nickname')
validated = False
else:
if not user.is_active:
self.email_or_nickmane.errors.append('User is desactivated')
validated = False
if not ucontr.check_password(user, self.password.data):
self.password.errors.append('Wrong password')
validated = False
self.user = user
return validated
class UserForm(Form):
"""
Create or edit a user (for the administrator).
"""
nickname = TextField(lazy_gettext("Nickname"),
[validators.Required(lazy_gettext("Please enter your nickname."))])
email = EmailField(lazy_gettext("Email"),
[validators.Length(min=6, max=35),
validators.Required(lazy_gettext("Please enter your email."))])
password = PasswordField(lazy_gettext("Password"))
automatic_crawling = BooleanField(lazy_gettext("Automatic crawling"),
default=True)
submit = SubmitField(lazy_gettext("Save"))
def validate(self):
validated = super(UserForm, self).validate()
if self.nickname.data != User.make_valid_nickname(self.nickname.data):
self.nickname.errors.append(lazy_gettext(
'This nickname has invalid characters. '
'Please use letters, numbers, dots and underscores only.'))
validated = False
return validated
class ProfileForm(Form):
"""
Edit user information.
"""
nickname = TextField(lazy_gettext("Nickname"),
[validators.Required(lazy_gettext("Please enter your nickname."))])
email = EmailField(lazy_gettext("Email"),
[validators.Length(min=6, max=35),
validators.Required(lazy_gettext("Please enter your email."))])
password = PasswordField(lazy_gettext("Password"))
password_conf = PasswordField(lazy_gettext("Password Confirmation"))
automatic_crawling = BooleanField(lazy_gettext("Automatic crawling"),
default=True)
webpage = URLField(lazy_gettext("Webpage"))
twitter = URLField(lazy_gettext("Twitter"))
is_public_profile = BooleanField(lazy_gettext("Public profile"),
default=True)
submit = SubmitField(lazy_gettext("Save"))
def validate(self):
validated = super(ProfileForm, self).validate()
if self.password.data != self.password_conf.data:
message = lazy_gettext("Passwords aren't the same.")
self.password.errors.append(message)
self.password_conf.errors.append(message)
validated = False
if self.nickname.data != User.make_valid_nickname(self.nickname.data):
self.nickname.errors.append(lazy_gettext('This nickname has '
'invalid characters. Please use letters, numbers, dots and'
' underscores only.'))
validated = False
return validated
class AddFeedForm(Form):
title = TextField(lazy_gettext("Title"), [validators.Optional()])
link = TextField(lazy_gettext("Feed link"),
[validators.Required(lazy_gettext("Please enter the URL."))])
site_link = TextField(lazy_gettext("Site link"), [validators.Optional()])
enabled = BooleanField(lazy_gettext("Check for updates"), default=True)
submit = SubmitField(lazy_gettext("Save"))
category_id = SelectField(lazy_gettext("Category of the feed"),
[validators.Optional()])
def set_category_choices(self, categories):
self.category_id.choices = [('0', 'No Category')]
self.category_id.choices += [(str(cat.id), cat.name)
for cat in categories]
class CategoryForm(Form):
name = TextField(lazy_gettext("Category name"))
submit = SubmitField(lazy_gettext("Save"))
class InformationMessageForm(Form):
subject = TextField(lazy_gettext("Subject"),
[validators.Required(lazy_gettext("Please enter a subject."))])
message = TextAreaField(lazy_gettext("Message"),
[validators.Required(lazy_gettext("Please enter a content."))])
submit = SubmitField(lazy_gettext("Send"))
class RecoverPasswordForm(Form):
email = EmailField(lazy_gettext("Email"),
[validators.Length(min=6, max=35),
validators.Required(
lazy_gettext("Please enter your email address."))])
submit = SubmitField(lazy_gettext("Recover"))
def validate(self):
if not super(RecoverPasswordForm, self).validate():
return False
user = User.query.filter(User.email == self.email.data).first()
if user and user.enabled:
return True
elif user and not user.enabled:
flash(lazy_gettext('Account not confirmed.'), 'danger')
return False
else:
flash(lazy_gettext('Invalid email.'), 'danger')
return False
|