class TaskForm(FlaskForm):
description = TextAreaField('New Task', validators=[DataRequired()])
start_date = DateField()
submit = SubmitField('Add')
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64), Email()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Keep me logged in')
submit = SubmitField('Log In')
class RegistrationForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Length(1, 64), Email()])
username = StringField('Username', validators=[DataRequired(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
'Usernames must have only letters, ''numbers, dots or underscores')])
password = PasswordField('Password', validators=[DataRequired(), EqualTo('password2', message='Passwords must match.')])
password2 = PasswordField('Confirm password', validators=[DataRequired()])
submit = SubmitField('Register')
def validate_email(self, field):
#app.logger.debug('validate_email')
if User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered.')
def validate_username(self, field):
#app.logger.debug('validate_username')
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/', methods=['GET','POST'])
def index():
tasks = []
form = TaskForm()
if form.validate_on_submit():
job = Task(description=form.description.data, start_date = form.start_date.data)
job.user = current_user
db.session.add(job)
db.session.commit()
if current_user.is_authenticated:
tasks = current_user.tasks
return render_template('index.html',form=form, tasks = tasks)
@app.route('/update/')
@login_required
def update(task_id):
return 'update task'
@app.route('/delete/')
@login_required
def delete(task_id):
form = TaskForm()
user = current_user._get_current_object()
task = Task.query.filter_by(id = task_id).first()
if task.user == user:
db.session.delete(task)
db.session.commit()
if current_user.is_authenticated:
tasks = current_user.tasks
return redirect(request.args.get('next') or url_for('index'))
@app.route('/login', methods=['GET','POST'])
def login():
form = LoginForm()
app.logger.debug('login')
if form.validate_on_submit():
app.logger.debug('validate_on_submit success')
user = User.query.filter_by(email = form.email.data).first()
app.logger.debug(user)
app.logger.debug(form.password.data)
if user is not None and user.verify_password(form.password.data):
login_user(user, form.remember_me.data)
return redirect(request.args.get('next') or url_for('index'))
else:
flash('invalid password or username')
return render_template('login.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out.')
return redirect(url_for('index'))
@app.route('/task',methods=['GET','POST'])
def task():
return 'tasks'
@app.route('/register', methods=['GET','POST'])
def register():
form = RegistrationForm()
app.logger.debug('register')
if form.validate_on_submit():
app.logger.debug('validate_on_submit success')
user = User(email=form.email.data,username=form.username.data,password=form.password.data)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
#http_server = HTTPServer(WSGIContainer(app))
#http_server.listen(5000)
#print('run...')
#IOLoop.current().start()
Register.html