From 154b23cc7fe703f75b48441bf8e819816a412fb5 Mon Sep 17 00:00:00 2001 From: Bibhas Date: Wed, 21 Jun 2017 09:57:13 +0530 Subject: [PATCH] fixed upload path issue --- imgee/__init__.py | 6 ++++++ imgee/storage.py | 3 ++- imgee/utils.py | 4 ++-- instance/testing.py | 2 +- manage.py | 2 +- runtests.sh | 1 + 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/imgee/__init__.py b/imgee/__init__.py index 4fb30f54..a90e4740 100644 --- a/imgee/__init__.py +++ b/imgee/__init__.py @@ -32,6 +32,12 @@ lastuser.init_usermanager(UserManager(db, models.User)) registry.init_app() +# PYTHONPATH is `pwd` when testing and empty otherwise +# using it to determine the project root +# either get it from environment variable, or it's one level up from this init file +app.project_root = os.environ.get('PYTHONPATH', '') or os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +print app.project_root + @app.errorhandler(403) def error403(error): diff --git a/imgee/storage.py b/imgee/storage.py index 66d13676..9a4c2aa9 100644 --- a/imgee/storage.py +++ b/imgee/storage.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from glob import glob import os.path +import os import re from subprocess import check_call, CalledProcessError import time @@ -292,7 +293,7 @@ def delete(stored_file, commit=True): registry.remove_keys_starting_with(stored_file.name) # remove locally - cache_path = app.config.get('UPLOADED_FILES_DEST') + cache_path = os.path.join(app.project_root, app.config.get('UPLOADED_FILES_DEST')) os.remove(os.path.join(cache_path, '%s' % stored_file.filename)) cached_img_path = os.path.join(cache_path, '%s_*' % stored_file.name) for f in glob(cached_img_path): diff --git a/imgee/utils.py b/imgee/utils.py index 01659da5..7bb0a1da 100644 --- a/imgee/utils.py +++ b/imgee/utils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import re -import os.path +import os from subprocess import check_output, CalledProcessError from urlparse import urljoin @@ -97,7 +97,7 @@ def get_file_url(scheme=None): def path_for(img_name): - return os.path.join(app.config['UPLOADED_FILES_DEST'], img_name) + return os.path.join(app.project_root, app.config['UPLOADED_FILES_DEST'], img_name) # -- mimetypes and content types diff --git a/instance/testing.py b/instance/testing.py index f73a9f51..54a6cf84 100644 --- a/instance/testing.py +++ b/instance/testing.py @@ -1,5 +1,5 @@ from os import environ -UPLOADED_FILES_DEST = 'tests/imgee/static/test_uploads' +UPLOADED_FILES_DEST = 'imgee/static/test_uploads' AWS_FOLDER = 'test/' UNKNOWN_FILE_THUMBNAIL = 'unknown.jpeg' diff --git a/manage.py b/manage.py index f5531500..3c121259 100755 --- a/manage.py +++ b/manage.py @@ -17,6 +17,6 @@ def mkdir_p(dirname): @manager.command def init(): - mkdir_p(app.config['UPLOADED_FILES_DEST']) + mkdir_p(os.path.join(app.project_root, app.config['UPLOADED_FILES_DEST'])) manager.run() diff --git a/runtests.sh b/runtests.sh index c41fd5a4..e105d6c1 100755 --- a/runtests.sh +++ b/runtests.sh @@ -1,6 +1,7 @@ #!/bin/sh set -e export FLASK_ENV="TESTING" +export PYTHONPATH=$(pwd) # setting project root as PYTHONPATH ./manage.py init # creates the test upload directory coverage run `which nosetests` "$@" coverage report -m