Daddy PyLint is not happy

This commit is contained in:
Michał 2023-04-01 17:40:42 +00:00
parent ff1af6b888
commit 42375af3c3
4 changed files with 79 additions and 63 deletions

View file

@ -20,33 +20,29 @@ def groups():
""" """
Group overview, shows all image groups Group overview, shows all image groups
""" """
# Get all groups groups = db_session.query(db.Groups).all()
group_list = db_session.query(db.Groups).all()
# For each group, get the 3 most recent images # For each group, get the 3 most recent images
for group_item in group_list: for group in groups:
group_item.author_username = db_session.query(db.Users.username)\ group.author_username = db_session.query(db.Users.username)\
.filter(db.Users.id == group_item.author_id)\ .filter(db.Users.id == group.author_id)\
.first()[0] .first()[0]
# Get the 3 most recent images # Get the 3 most recent images
group_images = db_session.query(db.GroupJunction.post_id)\ images = db_session.query(db.GroupJunction.post_id)\
.filter(db.GroupJunction.group_id == group_item.id)\ .filter(db.GroupJunction.group_id == group.id)\
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())\
.limit(3) .limit(3)
# Add an empty list to the group item
group_item.images = []
# For each image, get the image data and add it to the group item # For each image, get the image data and add it to the group item
for image in group_images: group.images = []
group_item.images.append(db_session.query(db.Posts.file_name, for image in images:
db.Posts.post_alt, group.images.append(db_session.query(db.Posts.file_name, db.Posts.post_alt,
db.Posts.image_colours, db.Posts.image_colours, db.Posts.id)\
db.Posts.id)\ .filter(db.Posts.id == image[0])\
.filter(db.Posts.id == image[0])\ .first())
.first())
return render_template('groups/list.html', groups=group_list) return render_template('groups/list.html', groups=groups)
@blueprint.route('/<int:group_id>') @blueprint.route('/<int:group_id>')
@ -54,34 +50,37 @@ def group(group_id):
""" """
Group view, shows all images in a group Group view, shows all images in a group
""" """
group_item = db_session.query(db.Groups).filter(db.Groups.id == group_id).first() # Get the group, if it doesn't exist, 404
group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
if group_item is None: if group is None:
abort(404, 'Group not found! D:') abort(404, 'Group not found! D:')
group_item.author_username = db_session.query(db.Users.username)\ # Get the group's author username
.filter(db.Users.id == group_item.author_id)\ group.author_username = db_session.query(db.Users.username)\
.filter(db.Users.id == group.author_id)\
.first()[0] .first()[0]
group_images = db_session.query(db.GroupJunction.post_id)\ # Get all images in the group from the junction table
junction = db_session.query(db.GroupJunction.post_id)\
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)\
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())\
.all() .all()
# Get the image data for each image in the group
images = [] images = []
for image in group_images: for image in junction:
image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first() image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
images.append(image) images.append(image)
if images: # Check contrast for the first image in the group for the banner
text_colour = 'rgb(var(--fg-black))'
if images[0]:
text_colour = contrast.contrast(images[0].image_colours[0], text_colour = contrast.contrast(images[0].image_colours[0],
'rgb(var(--fg-black))', 'rgb(var(--fg-black))',
'rgb(var(--fg-white))') 'rgb(var(--fg-white))')
else:
text_colour = 'rgb(var(--fg-black))'
return render_template('groups/group.html', return render_template('groups/group.html',
group=group_item, group=group,
images=images, images=images,
text_colour=text_colour) text_colour=text_colour)
@ -91,40 +90,45 @@ def group_post(group_id, image_id):
""" """
Image view, shows the image and its metadata from a specific group Image view, shows the image and its metadata from a specific group
""" """
img = db_session.query(db.Posts).filter(db.Posts.id == image_id).first() # Get the image, if it doesn't exist, 404
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if img is None: if image is None:
abort(404, 'Image not found') abort(404, 'Image not found')
img.author_username = db_session.query(db.Users.username)\ # Get the image's author username
.filter(db.Users.id == img.author_id)\ image.author_username = db_session.query(db.Users.username)\
.filter(db.Users.id == image.author_id)\
.first()[0] .first()[0]
# Get all groups the image is in
groups = db_session.query(db.GroupJunction.group_id)\ groups = db_session.query(db.GroupJunction.group_id)\
.filter(db.GroupJunction.post_id == image_id)\ .filter(db.GroupJunction.post_id == image_id)\
.all() .all()
img.groups = [] # Get the group data for each group the image is in
image.groups = []
for group in groups: for group in groups:
group = db_session.query(db.Groups).filter(db.Groups.id == group[0]).first() group = db_session.query(db.Groups.id, db.Groups.name)\
img.groups.append(group) .filter(db.Groups.id == group[0])\
.first()
image.groups.append(group)
# Get the next and previous images in the group
next_url = db_session.query(db.GroupJunction.post_id)\ next_url = db_session.query(db.GroupJunction.post_id)\
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)\
.filter(db.GroupJunction.post_id > image_id)\ .filter(db.GroupJunction.post_id > image_id)\
.order_by(db.GroupJunction.date_added.asc())\ .order_by(db.GroupJunction.date_added.asc())\
.first() .first()
prev_url = db_session.query(db.GroupJunction.post_id)\ prev_url = db_session.query(db.GroupJunction.post_id)\
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)\
.filter(db.GroupJunction.post_id < image_id)\ .filter(db.GroupJunction.post_id < image_id)\
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())\
.first() .first()
# If there is a next or previous image, get the URL for it
if next_url is not None: if next_url is not None:
next_url = url_for('group.group_post', group_id=group_id, image_id=next_url[0]) next_url = url_for('group.group_post', group_id=group_id, image_id=next_url[0])
if prev_url is not None: if prev_url is not None:
prev_url = url_for('group.group_post', group_id=group_id, image_id=prev_url[0]) prev_url = url_for('group.group_post', group_id=group_id, image_id=prev_url[0])
return render_template('image.html', image=img, next_url=next_url, prev_url=prev_url) return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)

View file

@ -35,22 +35,28 @@ def image(image_id):
""" """
Image view, shows the image and its metadata Image view, shows the image and its metadata
""" """
img = db_session.query(db.Posts).filter(db.Posts.id == image_id).first() # Get the image, if it doesn't exist, 404
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if not img: if not image:
abort(404, 'Image not found :<') abort(404, 'Image not found :<')
img.author_username = db_session.query(db.Users.username)\ # Get the image's author username
.filter(db.Users.id == img.author_id).first()[0] 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)\ groups = db_session.query(db.GroupJunction.group_id)\
.filter(db.GroupJunction.post_id == image_id).all() .filter(db.GroupJunction.post_id == image_id).all()
img.groups = [] # For each group, get the group data and add it to the image item
image.groups = []
for group in groups: for group in groups:
group = db_session.query(db.Groups).filter(db.Groups.id == group[0]).first() group = db_session.query(db.Groups.id, db.Groups.name)\
img.groups.append(group) .filter(db.Groups.id == group[0])\
.first()
image.groups.append(group)
# Get the next and previous images
next_url = db_session.query(db.Posts.id)\ next_url = db_session.query(db.Posts.id)\
.filter(db.Posts.id > image_id)\ .filter(db.Posts.id > image_id)\
.order_by(db.Posts.id.asc())\ .order_by(db.Posts.id.asc())\
@ -60,12 +66,13 @@ def image(image_id):
.order_by(db.Posts.id.desc())\ .order_by(db.Posts.id.desc())\
.first() .first()
# If there is a next or previous image, get the url
if next_url: if next_url:
next_url = url_for('gallery.image', image_id=next_url[0]) next_url = url_for('gallery.image', image_id=next_url[0])
if prev_url: if prev_url:
prev_url = url_for('gallery.image', image_id=prev_url[0]) prev_url = url_for('gallery.image', image_id=prev_url[0])
return render_template('image.html', image=img, next_url=next_url, prev_url=prev_url) return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)
@blueprint.route('/profile') @blueprint.route('/profile')

View file

@ -15,43 +15,46 @@ def compile_theme(theme_name, app_path):
print(f"Loading '{theme_name}' theme...") print(f"Loading '{theme_name}' theme...")
# Set Paths # Set Paths
THEME_SRC = os.path.join(app_path, 'themes', theme_name) theme_source = os.path.join(app_path, 'themes', theme_name)
THEME_DEST = os.path.join(app_path, 'static', 'theme') theme_destination = os.path.join(app_path, 'static', 'theme')
# If the theme doesn't exist, exit # If the theme doesn't exist, exit
if not os.path.exists(THEME_SRC): if not os.path.exists(theme_source):
print("Theme does not exist!") print("Theme does not exist!")
sys.exit(1) sys.exit(1)
# If the destination folder doesn't exist, create it # If the destination folder doesn't exist, create it
if not os.path.exists(THEME_DEST): if not os.path.exists(theme_destination):
os.makedirs(THEME_DEST) os.makedirs(theme_destination)
# Theme source file doesn't exist, exit # Theme source file doesn't exist, exit
if not os.path.join(THEME_SRC, 'style.sass'): if not os.path.join(theme_source, 'style.sass'):
print("No sass file found!") print("No sass file found!")
sys.exit(1) sys.exit(1)
# Compile the theme # Compile the theme
with open(os.path.join(THEME_DEST, 'style.css'), encoding='utf-8', mode='w+') as file: with open(os.path.join(theme_destination, 'style.css'),
encoding='utf-8', mode='w+') as file:
try: try:
file.write(sass.compile(filename=os.path.join(THEME_SRC, 'style.sass'),output_style='compressed')) file.write(sass.compile(filename=os.path.join(theme_source, 'style.sass'),
output_style='compressed'))
except sass.CompileError as err: except sass.CompileError as err:
print("Failed to compile!\n", err) print("Failed to compile!\n", err)
sys.exit(1) sys.exit(1)
print("Compiled successfully!") print("Compiled successfully!")
# If the destination folder exists, remove it # If the destination folder exists, remove it
if os.path.exists(os.path.join(THEME_DEST, 'fonts')): if os.path.exists(os.path.join(theme_destination, 'fonts')):
try: try:
shutil.rmtree(os.path.join(THEME_DEST, 'fonts')) shutil.rmtree(os.path.join(theme_destination, 'fonts'))
except Exception as err: except Exception as err:
print("Failed to remove old fonts!\n", err) print("Failed to remove old fonts!\n", err)
sys.exit(1) sys.exit(1)
# Copy the fonts # Copy the fonts
try: try:
shutil.copytree(os.path.join(THEME_SRC, 'fonts'), os.path.join(THEME_DEST, 'fonts')) shutil.copytree(os.path.join(theme_source, 'fonts'),
os.path.join(theme_destination, 'fonts'))
print("Fonts copied successfully!") print("Fonts copied successfully!")
except Exception as err: except Exception as err:
print("Failed to copy fonts!\n", err) print("Failed to copy fonts!\n", err)

View file

@ -29,4 +29,6 @@ build-backend = "poetry.core.masonry.api"
[tool.pylint.messages_control] [tool.pylint.messages_control]
# C0415: Flask uses it to register blueprints # C0415: Flask uses it to register blueprints
# W0718: Exception are logged so we don't need to raise them # W0718: Exception are logged so we don't need to raise them
disable = "C0415, W0718" # W0621: Flask deals with this fine, so I dont care about it lol
# R0801: Duplicate code will be dealt with later
disable = "C0415, W0718, W0621, R0801"