hglbbvdvvsscvsv / routes.py
Echo-ai
Update routes.py
346dbbb verified
from flask import Blueprint, render_template, request, redirect, url_for, flash
from flask_login import login_required, login_user, logout_user, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from database import JSONDatabase
import os
import math
main = Blueprint('main', __name__)
db = JSONDatabase(os.path.join(os.path.dirname(__file__), 'data'))
@main.route('/')
def home():
gangs = db.get_all_gangs()[:3] # Get latest 3 gangs
blogs = db.get_all_blogs()[:3] # Get latest 3 blogs
return render_template('home.html', gangs=gangs, blogs=blogs)
@main.route('/gangs')
def gangs():
page = request.args.get('page', 1, type=int)
per_page = 6 # Number of gangs per page
all_gangs = db.get_all_gangs()
total_gangs = len(all_gangs)
total_pages = math.ceil(total_gangs / per_page)
# Calculate start and end indices for the current page
start_idx = (page - 1) * per_page
end_idx = start_idx + per_page
# Get gangs for current page
gangs_page = all_gangs[start_idx:end_idx]
pagination = {
'page': page,
'per_page': per_page,
'total': total_gangs,
'pages': total_pages,
'has_prev': page > 1,
'has_next': page < total_pages
}
return render_template('gangs.html', gangs=gangs_page, pagination=pagination)
@main.route('/gang/<int:gang_id>')
def gang_detail(gang_id):
gang = db.get_gang(gang_id)
if gang is None:
flash('Gang not found!', 'error')
return redirect(url_for('main.gangs'))
return render_template('gang_detail.html', gang=gang)
@main.route('/blogs')
def blogs():
page = request.args.get('page', 1, type=int)
per_page = 6 # Number of blogs per page
all_blogs = db.get_all_blogs()
total_blogs = len(all_blogs)
total_pages = math.ceil(total_blogs / per_page)
# Calculate start and end indices for the current page
start_idx = (page - 1) * per_page
end_idx = start_idx + per_page
# Get blogs for current page
blogs_page = all_blogs[start_idx:end_idx]
pagination = {
'page': page,
'per_page': per_page,
'total': total_blogs,
'pages': total_pages,
'has_prev': page > 1,
'has_next': page < total_pages
}
return render_template('blogs.html', blogs=blogs_page, pagination=pagination)
@main.route('/blog/<int:blog_id>')
def blog_detail(blog_id):
blog = db.get_blog(blog_id)
if blog is None:
flash('Blog not found!', 'error')
return redirect(url_for('main.blogs'))
return render_template('blog_detail.html', blog=blog)
@main.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('admin.index'))
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
# Check against environment variables
if username == os.getenv('ADMIN_USERNAME') and password == os.getenv('ADMIN_PASSWORD'):
user = db.get_user(username)
login_user(user)
flash('Logged in successfully.')
return redirect(url_for('admin.index'))
flash('Invalid username or password')
return render_template('login.html')
@main.route('/logout')
@login_required
def logout():
logout_user()
flash('Logged out successfully.')
return redirect(url_for('main.home'))