Skip to content

Commit

Permalink
Merge pull request #40 from Segelzwerg/refactor/constants
Browse files Browse the repository at this point in the history
Refactor/constants
  • Loading branch information
Marcel authored Oct 17, 2020
2 parents 9c84a7d + 3f090c1 commit 360ef48
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
17 changes: 13 additions & 4 deletions family_foto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from flask_debugtoolbar import DebugToolbarExtension
from flask_login import LoginManager

UPLOADED_PHOTOS_DEST = 'UPLOADED_PHOTOS_DEST'
UPLOADED_PHOTOS_DEST_RELATIVE = 'UPLOADED_PHOTOS_DEST_RELATIVE'

UPLOADED_VIDEOS_DEST = 'UPLOADED_VIDEOS_DEST'
UPLOADED_VIDEOS_DEST_RELATIVE = 'UPLOADED_VIDEOS_DEST_RELATIVE'

RESIZED_DEST = 'RESIZED_DEST'
RESIZED_DEST_RELATIVE = 'RESIZED_DEST_RELATIVE'

login_manager = LoginManager()


Expand Down Expand Up @@ -38,11 +47,11 @@ def create_app(test_config: dict[str, Any] = None, test_instance_path: str = Non
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') or app.config[
'DATABASE_URL_TEMPLATE'].format(instance_path=app.instance_path)
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(app.instance_path,
app.config['UPLOADED_PHOTOS_DEST_RELATIVE'])
app.config[UPLOADED_PHOTOS_DEST_RELATIVE])
app.config['UPLOADED_VIDEOS_DEST'] = os.path.join(app.instance_path,
app.config['UPLOADED_VIDEOS_DEST_RELATIVE'])
app.config['RESIZED_DEST'] = os.path.join(app.instance_path,
app.config['RESIZED_DEST_RELATIVE'])
app.config[UPLOADED_PHOTOS_DEST_RELATIVE])
app.config[RESIZED_DEST] = os.path.join(app.instance_path,
app.config[RESIZED_DEST_RELATIVE])

# ensure the instance folder exists
try:
Expand Down
3 changes: 2 additions & 1 deletion family_foto/models/photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import current_app
from sqlalchemy import ForeignKey

from family_foto import UPLOADED_PHOTOS_DEST_RELATIVE
from family_foto.models import db
from family_foto.models.file import File
from family_foto.utils.image import resize
Expand Down Expand Up @@ -54,7 +55,7 @@ def path(self):
"""
Returns path to photo file.
"""
return current_app.config['UPLOADED_PHOTOS_DEST_RELATIVE'] + "/" + self.filename
return current_app.config[UPLOADED_PHOTOS_DEST_RELATIVE] + "/" + self.filename

def thumbnail(self, width: int, height: int):
"""
Expand Down
9 changes: 5 additions & 4 deletions family_foto/models/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import current_app
from sqlalchemy import ForeignKey

from family_foto import UPLOADED_VIDEOS_DEST_RELATIVE, RESIZED_DEST
from family_foto.models import db
from family_foto.models.file import File
from family_foto.utils.image import resize
Expand Down Expand Up @@ -56,7 +57,7 @@ def path(self):
"""
Returns path to video file.
"""
return current_app.config['UPLOADED_VIDEOS_DEST_RELATIVE'] + "/" + self.filename
return current_app.config[UPLOADED_VIDEOS_DEST_RELATIVE] + "/" + self.filename

def thumbnail(self, width: int, height: int):
"""
Expand All @@ -70,9 +71,9 @@ def thumbnail(self, width: int, height: int):
video.set(cv2.CAP_PROP_POS_FRAMES, random.randint(0, frame_count))
_, frame = video.read()

path = f'{current_app.config["RESIZED_DEST"]}/{width}_{height}_{self.filename}.jpg'
if not os.path.exists(current_app.config["RESIZED_DEST"]):
os.mkdir(current_app.config["RESIZED_DEST"])
path = f'{current_app.config[RESIZED_DEST]}/{width}_{height}_{self.filename}.jpg'
if not os.path.exists(current_app.config[RESIZED_DEST]):
os.mkdir(current_app.config[RESIZED_DEST])
if not cv2.imwrite(path, frame):
raise IOError(f'could not write {path}')
path = resize(path, self.filename, width, height)
Expand Down
16 changes: 9 additions & 7 deletions family_foto/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from flask_uploads import UploadSet
from werkzeug.datastructures import FileStorage

from family_foto import login_manager
from family_foto import login_manager, RESIZED_DEST, UPLOADED_PHOTOS_DEST, UPLOADED_VIDEOS_DEST
from family_foto.forms.login_form import LoginForm
from family_foto.forms.photo_sharing_form import PhotoSharingForm
from family_foto.forms.upload_form import UploadForm
Expand All @@ -17,6 +17,8 @@
from family_foto.models.user_settings import UserSettings
from family_foto.models.video import Video



web_bp = Blueprint('web', __name__)

VIDEOS = ('mp4',)
Expand Down Expand Up @@ -148,8 +150,8 @@ def uploaded_file(filename):
:param filename: name of the file
"""
log.info(f'{current_user.username} requested '
f'{current_app.config["UPLOADED_PHOTOS_DEST"]}/{filename}')
return send_from_directory(current_app.config["UPLOADED_PHOTOS_DEST"], filename)
f'{current_app.config[UPLOADED_PHOTOS_DEST]}/{filename}')
return send_from_directory(current_app.config[UPLOADED_PHOTOS_DEST], filename)


@web_bp.route('/_uploads/videos/<filename>')
Expand All @@ -161,8 +163,8 @@ def get_video(filename):
:param filename: name of the file
"""
log.info(f'{current_user.username} requested '
f'{current_app.config["UPLOADED_VIDEOS_DEST"]}/{filename}')
return send_from_directory(current_app.config["UPLOADED_VIDEOS_DEST"], filename)
f'{current_app.config[UPLOADED_VIDEOS_DEST]}/{filename}')
return send_from_directory(current_app.config[UPLOADED_VIDEOS_DEST], filename)


@web_bp.route('/resized-images/<filename>')
Expand All @@ -172,8 +174,8 @@ def resized_photo(filename):
Returns the path resized image.
:param filename: name of the resized photo
"""
log.info(f'{current_user.username} requested {current_app.config["RESIZED_DEST"]}/{filename}')
return send_from_directory(current_app.config["RESIZED_DEST"], filename)
log.info(f'{current_user.username} requested {current_app.config[RESIZED_DEST]}/{filename}')
return send_from_directory(current_app.config[RESIZED_DEST], filename)


@web_bp.route('/gallery', methods=['GET'])
Expand Down

0 comments on commit 360ef48

Please sign in to comment.