yurt
This commit is contained in:
1
app/routes/__init__.py
Normal file
1
app/routes/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Routes package
|
||||
33
app/routes/auth.py
Normal file
33
app/routes/auth.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
Authentication routes
|
||||
"""
|
||||
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash
|
||||
from app.utils.auth import check_credentials, login_user, logout_user
|
||||
|
||||
auth_bp = Blueprint('auth', __name__)
|
||||
|
||||
|
||||
@auth_bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
"""Login page and authentication"""
|
||||
if request.method == 'POST':
|
||||
username = request.form.get('username')
|
||||
password = request.form.get('password')
|
||||
|
||||
if check_credentials(username, password):
|
||||
login_user()
|
||||
flash('Successfully logged in!')
|
||||
return redirect(url_for('main.index'))
|
||||
else:
|
||||
flash('Invalid username or password.')
|
||||
|
||||
return render_template('login.html')
|
||||
|
||||
|
||||
@auth_bp.route('/logout')
|
||||
def logout():
|
||||
"""Logout user and redirect to main page"""
|
||||
logout_user()
|
||||
flash('You have been logged out.')
|
||||
return redirect(url_for('main.index'))
|
||||
30
app/routes/main.py
Normal file
30
app/routes/main.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Main application routes
|
||||
"""
|
||||
|
||||
from flask import Blueprint, render_template, request
|
||||
from app.models.database import PetPicture
|
||||
from app.utils.auth import is_authenticated
|
||||
from app.utils.helpers import get_liked_pictures_from_cookie, add_liked_status_to_pictures
|
||||
|
||||
main_bp = Blueprint('main', __name__)
|
||||
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
"""Main gallery page - shows all pictures for admins, posted only for public"""
|
||||
# Get liked pictures from cookie
|
||||
liked_list = get_liked_pictures_from_cookie(request)
|
||||
|
||||
# Get pictures based on authentication status
|
||||
if is_authenticated():
|
||||
pictures = PetPicture.get_all()
|
||||
public_view = False
|
||||
else:
|
||||
pictures = PetPicture.get_posted()
|
||||
public_view = True
|
||||
|
||||
# Add liked status to pictures
|
||||
pictures_with_likes = add_liked_status_to_pictures(pictures, liked_list)
|
||||
|
||||
return render_template("index.html", pictures=pictures_with_likes, public_view=public_view)
|
||||
81
app/routes/pictures.py
Normal file
81
app/routes/pictures.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
Picture management routes
|
||||
"""
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, send_from_directory, make_response, jsonify, current_app
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from app.models.database import PetPicture
|
||||
from app.utils.auth import login_required
|
||||
from app.utils.helpers import allowed_file, handle_like_action
|
||||
|
||||
pictures_bp = Blueprint('pictures', __name__)
|
||||
|
||||
|
||||
@pictures_bp.route('/upload', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def upload():
|
||||
"""Upload new pet picture"""
|
||||
if request.method == 'POST':
|
||||
if 'picture' not in request.files:
|
||||
flash('No file selected')
|
||||
return redirect(request.url)
|
||||
|
||||
file = request.files['picture']
|
||||
subscriber_name = request.form.get('subscriber_name')
|
||||
description = request.form.get('description', '').strip()
|
||||
|
||||
if file.filename == '':
|
||||
flash('No file selected')
|
||||
return redirect(request.url)
|
||||
|
||||
if not subscriber_name:
|
||||
flash('Subscriber name is required')
|
||||
return redirect(request.url)
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_')
|
||||
filename = timestamp + filename
|
||||
|
||||
# Ensure upload directory exists
|
||||
upload_path = current_app.config['UPLOAD_FOLDER']
|
||||
os.makedirs(upload_path, exist_ok=True)
|
||||
|
||||
file.save(os.path.join(upload_path, filename))
|
||||
|
||||
# Save to database
|
||||
PetPicture.create(filename, subscriber_name, description, datetime.now())
|
||||
|
||||
flash('Picture uploaded successfully!')
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
flash('Invalid file type')
|
||||
return redirect(request.url)
|
||||
|
||||
return render_template('upload.html')
|
||||
|
||||
|
||||
@pictures_bp.route('/mark_posted/<int:picture_id>', methods=['POST'])
|
||||
@login_required
|
||||
def mark_posted(picture_id):
|
||||
"""Mark picture as posted"""
|
||||
PetPicture.mark_as_posted(picture_id)
|
||||
flash('Picture marked as posted!')
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
|
||||
@pictures_bp.route('/like/<int:picture_id>', methods=['POST'])
|
||||
def like_picture(picture_id):
|
||||
"""Handle like/unlike actions"""
|
||||
return handle_like_action(picture_id, request)
|
||||
|
||||
|
||||
@pictures_bp.route('/download/<filename>')
|
||||
def download_file(filename):
|
||||
"""Download original picture file"""
|
||||
return send_from_directory(
|
||||
current_app.config['UPLOAD_FOLDER'], filename, as_attachment=True
|
||||
)
|
||||
Reference in New Issue
Block a user