text: from app import app, db
  from flask import render_template, redirect, url_for, flash, get_flashed_messages, request
  from models import Task, User
  from datetime import datetime
  from flask_login import current_user, login_user, logout_user, login_required
  from werkzeug.urls import url_parse
  from emailpy import send_password_reset_email
  
  import forms
  
  # Basic route
  
  
  @app.route('/login', methods=['GET', 'POST'])
  def login():
      if current_user.is_authenticated:
          return redirect(url_for('index'))
      form = forms.LoginForm()
      if form.validate_on_submit():
          user = User.query.filter_by(username=form.username.data).first()
          if user is None or not user.check_password(form.password.data):
              flash('Invalid username or password')
              return redirect(url_for('login'))
          login_user(user, remember=form.remember_me.data)
          next_page = request.args.get('next')
          if not next_page or url_parse(next_page).netloc != '':
              next_page = url_for('index')
          return redirect(next_page)
      return render_template('login.html',title='Sign In', form=form)
  
  @app.route('/logout')
  def logout():
      logout_user()
      return redirect(url_for('index'))
  
  @app.route('/register', methods=['GET','POST'])
  def registration():
      try:
          if current_user.is_authenticated:
              return redirect(url_for('index'))
          form = forms.RegistrationForm()
          if form.validate_on_submit():
              user = User(username=form.username.data, email=form.email.data)
              user.set_password(form.password.data)
              db.session.add(user)
              db.session.commit()
              flash('You are now a registered user')
              return redirect(url_for('login'))
          return render_template('register.html',title='Register',form=form)
      except Exception as e:
          return(str(e))
  
  @app.route('/')
  @app.route('/index')
  @login_required
  def index():
      tasks = Task.query.all()
      return render_template('index.html', title='Tasks', tasks=tasks)
  
  @login_required
  @app.route('/mytasks/<username>',methods=['GET','POST'])
  def mytasks(username):
      usertasks = User.query.filter_by(username=username).all()
      tasks = [{
          
      }]
  
  @app.route('/add', methods=['GET','POST'])
  def add():
      form = forms.AddTaskForm()
      if form.validate_on_submit():
          t = Task(title=form.title.data, date=datetime.utcnow())
          db.session.add(t)
          db.session.commit()
          flash('Task added to the database')
          return redirect(url_for('index'))
      return render_template('add.html', form=form)
  
  @app.route('/edit/<int:task_id>', methods=['GET', 'POST'])
  def edit(task_id):
      task = Task.query.get(task_id)
      # print(task)
      form = forms.AddTaskForm()
  
      if task:
          if form.validate_on_submit():
              task.title = form.title.data
              task.date = datetime.utcnow()
              db.session.commit()
              flash('Task has been updated')
              return redirect(url_for('index'))
  
          form.title.data = task.title
          return render_template('edit.html', form=form, title="Edit Task",task_id= task_id)
      else:
          flash('Task not found')
  
      return redirect(url_for('index'))
  
  @app.route('/delete/<int:task_id>', methods=['GET', 'POST'])
  def delete(task_id):
      task = Task.query.get(task_id)
      # print(task)
      form = forms.DeleteTaskForm()
  
      if task:
          if form.validate_on_submit():
              db.session.delete(task)
              db.session.commit()
              flash('Task has been deleted')
              return redirect(url_for('index'))
  
          return render_template('delete.html', form=form, task_id= task_id, title = task.title)
      else:
          flash('Task not found')
  
      return redirect(url_for('index'))
  
  @app.route('/reset_password_request', methods=['GET', 'POST'])
  def reset_password_request():
      if current_user.is_authenticated:
          return redirect(url_for('index'))
      form = forms.ResetPasswordRequestForm()
      if form.validate_on_submit():
          user = User.query.filter_by(email=form.email.data).first()
          if user:
              send_password_reset_email(user)
          flash('Check your email for the instructions to reset your password')
          return redirect(url_for('login'))
      return render_template('reset_password_request.html',
                             title='Reset Password', form=form)
  
  @app.route('/reset_password/<token>', methods=['GET', 'POST'])
  def reset_password(token):
      if current_user.is_authenticated:
          return redirect(url_for('index'))
      user = User.verify_reset_password_token(token)
      if not user:
          return redirect(url_for('index'))
      form = forms.ResetPasswordForm()
      if form.validate_on_submit():
          user.set_password(form.password.data)
          db.session.commit()
          flash('Your password has been reset.')
          return redirect(url_for('login'))
      return render_template('reset_password.html', form=form)