DeepSource Fixing Antipatterns

This commit is contained in:
Michał Gdula 2023-04-02 16:50:52 +00:00
parent 1152856f2a
commit 7b97b8e0ef
11 changed files with 177 additions and 185 deletions

View file

@ -66,16 +66,14 @@ def create_app(test_config=None):
js_scripts = Bundle('js/*.js', output='gen/packed.js')
assets.register('js_all', js_scripts)
# Error handlers
# Error handlers, if the error is not a HTTP error, return 500
@app.errorhandler(Exception)
def error_page(err):
# If the error is not an HTTPException, return a 500 error
def error_page(err): # noqa
if not isinstance(err, HTTPException):
abort(500)
error = err.code
msg = err.description
return render_template('error.html', error=error, msg=msg), err.code
return render_template('error.html',
error=err.code,
msg=err.description), err.code
# Load login, registration and logout manager
from gallery import auth

View file

@ -14,8 +14,6 @@ DB_PATH = os.path.join(USER_DIR, 'gallery.sqlite')
# In the future, I want to add support for other databases
# engine = create_engine('postgresql://username:password@host:port/database_name', echo=False)
# engine = create_engine('mysql://username:password@host:port/database_name', echo=False)
engine = create_engine(f'sqlite:///{DB_PATH}', echo=False)
base = declarative_base()

View file

@ -75,31 +75,27 @@ def upload():
# Save file
try:
form_file.save(img_path)
except Exception as err:
logging.error('Could not save file: %s', err)
except OSError as err:
logging.info('Error saving file %s because of %s', img_path, err)
abort(500)
img_exif = mt.Metadata(img_path).yoink() # Get EXIF data
img_colors = ColorThief(img_path).get_palette(color_count=3) # Get color palette
img_colors = ColorThief(img_path).get_palette(color_count=3) # Get color palette
# Save to database
try:
query = db.Posts(author_id=g.user.id,
created_at=dt.utcnow(),
file_name=img_name+'.'+img_ext,
file_type=img_ext,
image_exif=img_exif,
image_colours=img_colors,
post_description=form['description'],
post_alt=form['alt'])
query = db.Posts(author_id=g.user.id,
created_at=dt.utcnow(),
file_name=img_name+'.'+img_ext,
file_type=img_ext,
image_exif=img_exif,
image_colours=img_colors,
post_description=form['description'],
post_alt=form['alt'])
db_session.add(query)
db_session.commit()
except Exception as err:
logging.error('Could not save to database: %s', err)
abort(500)
db_session.add(query)
db_session.commit()
return 'Gwa Gwa' # Return something so the browser doesn't show an error
return 'Gwa Gwa' # Return something so the browser doesn't show an error
@blueprint.route('/delete/<int:image_id>', methods=['POST'])

View file

@ -64,7 +64,7 @@ def generate_thumbnail(file_name, resolution, ext=None):
# Save image to cache directory
try:
image.save(os.path.join(CACHE_PATH,f'{file_name}_{res_x}x{res_y}.{ext}'),
image.save(os.path.join(CACHE_PATH, f'{file_name}_{res_x}x{res_y}.{ext}'),
icc_profile=image_icc)
except OSError:
# This usually happens when saving a JPEG with an ICC profile,

View file

@ -279,7 +279,7 @@ def lens_specification(value):
"""
try:
return str(value[0] / value[1]) + 'mm - ' + str(value[2] / value[3]) + 'mm'
except Exception:
except ValueError:
return None

View file

@ -45,19 +45,11 @@ def compile_theme(theme_name, app_path):
# If the destination folder exists, remove it
if os.path.exists(os.path.join(theme_destination, 'fonts')):
try:
shutil.rmtree(os.path.join(theme_destination, 'fonts'))
except Exception as err:
print("Failed to remove old fonts!\n", err)
sys.exit(1)
shutil.rmtree(os.path.join(theme_destination, 'fonts'))
# Copy the fonts
try:
shutil.copytree(os.path.join(theme_source, 'fonts'),
os.path.join(theme_destination, 'fonts'))
print("Fonts copied successfully!")
except Exception as err:
print("Failed to copy fonts!\n", err)
sys.exit(1)
shutil.copytree(os.path.join(theme_source, 'fonts'),
os.path.join(theme_destination, 'fonts'))
print("Fonts copied successfully!")
print(f"{datetime.now().hour}:{datetime.now().minute}:{datetime.now().second} - Done!\n")