Move views/routes to seperate files

This commit is contained in:
Michał 2023-04-06 16:22:56 +00:00
parent 7eb4355b52
commit f33842ead3
13 changed files with 150 additions and 116 deletions

View file

@ -114,11 +114,16 @@ def create_app(test_config=None):
from gallery import auth
app.register_blueprint(auth.blueprint)
# Load the different routes
from gallery.views import api, groups, routing, settings
# Load the API
from gallery import api
app.register_blueprint(api.blueprint)
app.register_blueprint(groups.blueprint)
app.register_blueprint(routing.blueprint)
# Load the different views
from gallery.views import index, image, group, settings, profile
app.register_blueprint(index.blueprint)
app.register_blueprint(image.blueprint)
app.register_blueprint(group.blueprint)
app.register_blueprint(profile.blueprint)
app.register_blueprint(settings.blueprint)
# Log to file that the app has started

View file

@ -185,11 +185,13 @@
}
.banner-filter {
background: linear-gradient(90deg, rgb({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}), rgba({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}, 0.3)) !important;
background: linear-gradient(90deg, rgb({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}),
rgba({{ images.0.colours.1.0 }}, {{ images.0.colours.1.1 }}, {{ images.0.colours.1.2 }}, 0.3)) !important;
}
@media (max-width: 800px) {
.banner-filter {
background: linear-gradient(180deg, rgba({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}, 0.8), rgba({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}, 0.5)) !important;
background: linear-gradient(180deg, rgba({{ images.0.colours.0.0 }}, {{ images.0.colours.0.1 }}, {{ images.0.colours.0.2 }}, 0.8),
rgba({{ images.0.colours.1.0 }}, {{ images.0.colours.1.1 }}, {{ images.0.colours.1.2 }}, 0.5)) !important;
}
}
@ -212,7 +214,7 @@
<img src="{{ url_for('api.file', file_name=images.0.filename ) }}?r=prev" onload="imgFade(this)" style="opacity:0;"/>
<span class="banner-filter"></span>
<div class="banner-content">
<p class="banner-info">By <a href="{{ url_for('gallery.profile', id=group.author_id) }}" class="link">{{ group.author_username }}</a> - {{ images|length }} Images</p>
<p class="banner-info">By <a href="{{ url_for('profile.profile', id=group.author_id) }}" class="link">{{ group.author_username }}</a> - {{ images|length }} Images</p>
<h1 class="banner-header">{{ group.name }}</h1>
<p class="banner-subtitle">{{ group.description }}</p>
<div class="pill-row">

View file

@ -80,7 +80,8 @@
<style>
.background span {
background-image: linear-gradient(to top, rgba({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }}, 1), transparent);
background-image: linear-gradient(to top, rgba({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }}, 0.8),
rgba({{ image.colours.1.0 }}, {{ image.colours.1.1 }}, {{ image.colours.1.2 }}, 0.2));
}
</style>
{% endblock %}
@ -202,7 +203,7 @@
<table>
<tr>
<td>Author</td>
<td><a href="{{ url_for('gallery.profile', id=image.author_id) }}" class="link">{{ image.author_username }}</a></td>
<td><a href="{{ url_for('profile.profile', id=image.author_id) }}" class="link">{{ image.author_username }}</a></td>
</tr>
<tr>
<td>Upload date</td>

View file

@ -16,7 +16,7 @@
{% if images %}
<div class="gallery-grid">
{% for image in images %}
<a id="image-{{ image.id }}" class="gallery-item" href="{{ url_for('gallery.image', image_id=image.id) }}" style="background-color: rgb({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }})">
<a id="image-{{ image.id }}" class="gallery-item" href="{{ url_for('image.image', image_id=image.id) }}" style="background-color: rgb({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }})">
<div class="image-filter">
<p class="image-subtitle"></p>
<p class="image-title"><span class="time">{{ image.created_at }}</span></p>

View file

@ -93,7 +93,7 @@
<span class="navigation-spacer"></span>
{% if current_user.is_authenticated %}
<a href="{{url_for('gallery.profile')}}" class="navigation-item {% block nav_profile %}{% endblock %}">
<a href="{{url_for('profile.profile')}}" class="navigation-item {% block nav_profile %}{% endblock %}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 256 256"><path d="M231.73,221.94A8,8,0,0,1,224,232H160A8,8,0,0,1,152.27,222a40,40,0,0,1,17.11-23.33,32,32,0,1,1,45.24,0A40,40,0,0,1,231.73,221.94ZM216,72H130.67L102.93,51.2a16.12,16.12,0,0,0-9.6-3.2H40A16,16,0,0,0,24,64V200a16,16,0,0,0,16,16h80a8,8,0,0,0,0-16H40V64H93.33l27.74,20.8a16.12,16.12,0,0,0,9.6,3.2H216v32a8,8,0,0,0,16,0V88A16,16,0,0,0,216,72Z"></path></svg>
<span class="tool-tip">
Profile

View file

@ -11,7 +11,7 @@
{% if images %}
<div class="gallery-grid">
{% for image in images %}
<a id="image-{{ image.id }}" class="gallery-item" href="{{ url_for('gallery.image', image_id=image.id) }}" style="background-color: rgb({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }})">
<a id="image-{{ image.id }}" class="gallery-item" href="{{ url_for('image.image', image_id=image.id) }}" style="background-color: rgb({{ image.colours.0.0 }}, {{ image.colours.0.1 }}, {{ image.colours.0.2 }})">
<div class="image-filter">
<p class="image-subtitle"></p>
<p class="image-title"><span class="time">{{ image.created_at }}</span></p>

View file

@ -42,7 +42,7 @@ def groups():
.filter(db.Posts.id == image[0])
.first())
return render_template('groups/list.html', groups=groups)
return render_template('list.html', groups=groups)
@blueprint.route('/<int:group_id>')
@ -83,7 +83,7 @@ def group(group_id):
'rgb(var(--fg-black))',
'rgb(var(--fg-white))')
return render_template('groups/group.html',
return render_template('group.html',
group=group,
images=images,
text_colour=text_colour)

59
gallery/views/image.py Normal file
View file

@ -0,0 +1,59 @@
"""
Onlylegs - Image View
"""
from flask import Blueprint, abort, render_template, url_for
from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('image', __name__, url_prefix='/image')
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/<int:image_id>')
def image(image_id):
"""
Image view, shows the image and its metadata
"""
# Get the image, if it doesn't exist, 404
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if not image:
abort(404, 'Image not found :<')
# Get the image's author username
image.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id)
.first()[0])
# Get the image's groups
groups = (db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id)
.all())
# For each group, get the group data and add it to the image item
image.groups = []
for group in groups:
image.groups.append(db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0])
.first())
# Get the next and previous images
# Check if there is a group ID set
next_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id > image_id)
.order_by(db.Posts.id.asc())
.first())
prev_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id < image_id)
.order_by(db.Posts.id.desc())
.first())
# If there is a next or previous image, get the url
if next_url:
next_url = url_for('image.image', image_id=next_url[0])
if prev_url:
prev_url = url_for('image.image', image_id=prev_url[0])
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)

30
gallery/views/index.py Normal file
View file

@ -0,0 +1,30 @@
"""
Onlylegs Gallery - Index view
"""
from flask import Blueprint, render_template, request
from werkzeug.exceptions import abort
from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('gallery', __name__)
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/')
def index():
"""
Home page of the website, shows the feed of the latest images
"""
images = db_session.query(db.Posts.filename,
db.Posts.alt,
db.Posts.colours,
db.Posts.created_at,
db.Posts.id).order_by(db.Posts.id.desc()).all()
if request.args.get('coffee') == 'please':
abort(418)
return render_template('index.html', images=images)

39
gallery/views/profile.py Normal file
View file

@ -0,0 +1,39 @@
"""
Onlylegs Gallery - Profile view
"""
from flask import Blueprint, render_template, request
from werkzeug.exceptions import abort
from flask_login import current_user
from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('profile', __name__, url_prefix='/profile')
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/profile')
def profile():
"""
Profile overview, shows all profiles on the onlylegs gallery
"""
user_id = request.args.get('id', default=None, type=int)
# If there is no userID set, check if the user is logged in and display their profile
if not user_id:
if current_user.is_authenticated:
user_id = current_user.id
else:
abort(404, 'You must be logged in to view your own profile!')
# Get the user's data
user = db_session.query(db.Users).filter(db.Users.id == user_id).first()
if not user:
abort(404, 'User not found :c')
images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all()
return render_template('profile.html', user=user, images=images)

View file

@ -1,102 +0,0 @@
"""
Onlylegs Gallery - Routing
"""
from flask import Blueprint, render_template, url_for, request
from werkzeug.exceptions import abort
from flask_login import current_user
from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('gallery', __name__)
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/')
def index():
"""
Home page of the website, shows the feed of the latest images
"""
images = db_session.query(db.Posts.filename,
db.Posts.alt,
db.Posts.colours,
db.Posts.created_at,
db.Posts.id).order_by(db.Posts.id.desc()).all()
if request.args.get('coffee') == 'please':
abort(418)
return render_template('index.html', images=images)
@blueprint.route('/image/<int:image_id>')
def image(image_id):
"""
Image view, shows the image and its metadata
"""
# Get the image, if it doesn't exist, 404
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if not image:
abort(404, 'Image not found :<')
# Get the image's author username
image.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id)
.first()[0])
# Get the image's groups
groups = (db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id)
.all())
# For each group, get the group data and add it to the image item
image.groups = []
for group in groups:
image.groups.append(db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0])
.first())
# Get the next and previous images
next_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id > image_id)
.order_by(db.Posts.id.asc())
.first())
prev_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id < image_id)
.order_by(db.Posts.id.desc())
.first())
# If there is a next or previous image, get the url
if next_url:
next_url = url_for('gallery.image', image_id=next_url[0])
if prev_url:
prev_url = url_for('gallery.image', image_id=prev_url[0])
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)
@blueprint.route('/profile')
def profile():
"""
Profile overview, shows all profiles on the onlylegs gallery
"""
user_id = request.args.get('id', default=None, type=int)
# If there is no userID set, check if the user is logged in and display their profile
if not user_id:
if current_user.is_authenticated:
user_id = current_user.id
else:
abort(404, 'You must be logged in to view your own profile!')
# Get the user's data
user = db_session.query(db.Users).filter(db.Users.id == user_id).first()
if not user:
abort(404, 'User not found :c')
images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all()
return render_template('profile.html', user=user, images=images)