whyareyouemailingme

routes.py help 20230325

Mar 25th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.35 KB | Help | 0 0
  1. from flask import render_template, request, flash, redirect, url_for
  2. from app import app, db
  3. from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm
  4. from flask_login import current_user, login_user, logout_user, login_required
  5. from app.models import User
  6. from werkzeug.urls import url_parse
  7. from datetime import datetime
  8.  
  9. @app.before_request
  10. def before_request():
  11.     if current_user.is_authenticated:
  12.         current_user.last_seen = datetime.utcnow()
  13.         db.session.commit()
  14.  
  15. #main page
  16. @app.route('/')
  17. @app.route('/index')
  18. @login_required
  19. def index():
  20.     posts = [
  21.         {
  22.             'author': {'username': 'John'},
  23.             'body': 'Beautiful day in Portland!'
  24.         },
  25.         {
  26.             'author': {'username': 'Susan'},
  27.             'body': 'The Avengers movie was so cool!'
  28.         }
  29.     ]
  30.     return render_template('index.html', title='Home', posts=posts)
  31.  
  32. #login page
  33. @app.route('/login', methods=['GET', 'POST'])
  34. def login():
  35.     if current_user.is_authenticated:
  36.         return redirect(url_for('index'))
  37.     form = LoginForm()
  38.     if form.validate_on_submit():
  39.         user = User.query.filter_by(username=form.username.data).first()
  40.         if user is None or not user.check_password(form.password.data):
  41.             flash('Invalid username or password')
  42.             return redirect(url_for('login'))
  43.         login_user(user, remember=form.remember_me.data)
  44.         next_page = request.args.get('next')
  45.         if not next_page or url_parse(next_page).netloc != '':
  46.             next_page = url_for('index')
  47.         return redirect(next_page)
  48.     return render_template('login.html', title='Sign In', form=form)
  49.  
  50. #registration page
  51. @app.route('/register', methods=['GET', 'POST'])
  52. def register():
  53.     if current_user.is_authenticated:
  54.         return redirect(url_for('index'))
  55.     form = RegistrationForm()
  56.     if form.validate_on_submit():
  57.         user = User(username=form.username.data, email=form.email.data)
  58.         user.set_password(form.password.data)
  59.         db.session.add(user)
  60.         db.session.commit()
  61.         flash('Congratulations, you are now a registered user!')
  62.         return redirect(url_for('login'))
  63.     return render_template('register.html', title='Register', form=form)
  64.  
  65. #logout action
  66. @app.route('/logout')
  67. def logout():
  68.     logout_user()
  69.     return redirect(url_for('index'))
  70.  
  71. #profile pages
  72. @app.route('/user/<username>')
  73. @login_required
  74. def user(username):
  75.     user = User.query.filter_by(username=username).first_or_404()
  76.     posts = [
  77.         {'author': user, 'body': 'Test Post #1'},
  78.         {'author': user, 'body': 'Test Post #2'}
  79.     ]
  80.     form = EmptyForm()
  81.     return render_template('user.html', user=user, posts=posts, form=form)
  82.  
  83. #edit profile page
  84. @app.route('/edit_profile', methods=['GET', 'POST'])
  85. @login_required
  86. def edit_profile():
  87.     form = EditProfileForm(current_user.username)
  88.     if form.validate_on_submit():
  89.         current_user.username = form.username.data
  90.         current_user.about_me = form.about_me.data
  91.         db.session.commit()
  92.         flash('Your changes have been saved.')
  93.         return redirect(url_for('edit_profile'))
  94.     elif request.method == 'GET':
  95.         form.username.data = current_user.username
  96.         form.about_me.data = current_user.about_me
  97.     return render_template('edit_profile.html', title='Edit Profile', form=form)
  98.  
  99. #follow and unfollow
  100. @app.route('/follow/<username>', methods=['POST'])
  101. @login_required
  102. def follow(username):
  103.     form = EmptyForm()
  104.     if form.validate_on_submit():
  105.         user = User.query.filter_by(username=username).first()
  106.         if user is None:
  107.             flash('User {} not found.'.format(username))
  108.             return redirect(url_for('index'))
  109.         if user == current_user:
  110.             flash('You cannot follow yourself!')
  111.             return redirect(url_for('user', username=username))
  112.         current_user.follow(user)
  113.         db.session.commit()
  114.         flash('You are following {}!'.format(username))
  115.         return redirect(url_for('user', username=username))
  116.     else:
  117.         return redirect(url_for('index'))
  118.  
  119. @app.route('/unfollow/<username>', methods=['POST'])
  120. @login_required
  121. def unfollow(username):
  122.     form = EmptyForm()
  123.     if form.validate_on_submit():
  124.         user = User.query.filter_by(username=username).first()
  125.         if user is None:
  126.             flash('User {} not found.'.format(username))
  127.             return redirect(url_for('index'))
  128.         if user == current_user:
  129.             flash('You cannot unfollow yourself!')
  130.             return redirect(url_for('user', username=username))
  131.         current_user.unfollow(user)
  132.         db.session.commit()
  133.         flash('You are not following {}!'.format(username))
  134.         return redirect(url_for('user', username=username))
  135.     else:
  136.         return redirect(url_for('index'))
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment