From 01cf6ba2b514c37bc1b766bb982efd9bd898d0f3 Mon Sep 17 00:00:00 2001 From: Cameron Wilson Date: Thu, 13 Jul 2023 14:34:16 +0100 Subject: [PATCH] amended hard-coded filepaths and uolstorage symlink, to allow to run locally --- app/app.py | 55 +++++++++++++++++++++---------------- app/serverscripts/config.py | 6 ++-- uolstorage | 1 + 3 files changed, 37 insertions(+), 25 deletions(-) create mode 120000 uolstorage diff --git a/app/app.py b/app/app.py index 72aeac1..2339fef 100644 --- a/app/app.py +++ b/app/app.py @@ -42,27 +42,31 @@ tool_uk = parsetext.about(about_tool_text_en_uk) disc_br = parsetext.about(discl_liab_text_pt_br) disc_uk = parsetext.about(discl_liab_text_en_uk) -ini_br = parsetext.about(ini_page_text_pt_br) -ini_en = parsetext.about(ini_page_text_en_uk) +ini_br = parsetext.about(ini_page_text_pt_br) +ini_en = parsetext.about(ini_page_text_en_uk) -rootdir = '/var/www/AgroClimatic-Monitor/app/' -app = Flask('AGROCLIM_SERVER', - static_url_path='', # removes path prefix requirement - static_folder=os.path.abspath( - rootdir + 'templates/static/'), # static file location - template_folder=rootdir + 'templates' # template file location - ) +from flask_sqlalchemy import SQLAlchemy +from flask_statistics import Statistics +from flask_socketio import SocketIO +rootdir = apphome # taken from config settings +# cemaccam: updated path construction to use os.path.join, to ensure slashes in correct places +# cemaccam: added 'app' to paths, to correctly locate 'templates' directory +app=Flask('AGROCLIM_SERVER', + static_url_path='', # removes path prefix requirement + static_folder=os.path.abspath(os.path.join(rootdir,'app','templates/static')),# static file location + template_folder=os.path.abspath(os.path.join(rootdir,'app','templates')) # template file location + ) app.secret_key = app_key - - -app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///var/www/AgroClimatic-Monitor/statslog.sqlite3' +# cemaccam: URI needs three slashes before the directory (so 4 in total for Unix/Mac). +app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///'+os.path.join(rootdir,'statslog.sqlite3') app.config['DATA_LOCATION'] = PROCESSED -app.config['MAX_CONTENT_LENGTH'] = file_mb_max * 1024 * 1024 +app.config['MAX_CONTENT_LENGTH'] = file_mb_max* 1024 * 1024 + +sqlc = Database(db_loc,app_key) -sqlc = Database(db_loc, app_key) db = SQLAlchemy(app) @@ -87,27 +91,29 @@ class Request(db.Model): platform = db.Column(db.String) mimetype = db.Column(db.String) +# cemaccam: added with app.app_context(), in response to runtime error: 'Working outside of application context.' +with app.app_context(): + db.create_all() -db.create_all() statistics = Statistics(app, db, Request) # # Check that the upload folder exists def makedir(dest, upload=True): if upload: - global STAGING - fullpath = '%s%s' % (STAGING, dest) + # cemaccam: update to use more legible f-string syntax + fullpath = f'{STAGING}{dest}' # '%s%s'%(STAGING,dest) else: fullpath = dest print('read full: ', fullpath) if not os.path.isdir(fullpath): - os.mkdir(fullpath) - - + # cemaccam: changed to os.makedirs rather than os.mkdir, to deal with any parent not existing + os.makedirs(fullpath) # try: makedir('') # make uploads folder except (PermissionError, FileNotFoundError) as e: +# except PermissionError: print('ERROR: STORAGE Not Readable by Apache') print( 'PermissionError: [Errno 13] Permission denied: /var/www/AgroClimatic-Monitor/uolstorage/Data/upload') @@ -256,15 +262,18 @@ def getbundle(page): @app.route('/allfiles/') def getallfiles(): print(PROCESSED) - return send_from_directory('%s/' % (PROCESSED), 'allfiles.json', as_attachment=True) + # cemaccam: updated to use f-string syntax + return send_from_directory(f'{PROCESSED}/', 'allfiles.json', as_attachment=True) @app.route('/data///') def getdata(folder, item): + # cemaccam: updated to use f-string syntax + print(f'{PROCESSED}{folder}/',folder,item,'\n\n\n') - print('%s%s/' % (PROCESSED, folder), folder, item, '\n\n\n') + # cemaccam: updated to use f-string syntax + return send_from_directory(f'{PROCESSED}{folder}/', item, as_attachment=True) - return send_from_directory('%s%s/' % (PROCESSED, folder), item, as_attachment=True) ''' diff --git a/app/serverscripts/config.py b/app/serverscripts/config.py index d610847..bf596d3 100644 --- a/app/serverscripts/config.py +++ b/app/serverscripts/config.py @@ -2,7 +2,7 @@ .. module:: server :platform: Unix :synopsis: Flask app server scripts -.. moduleauther: Dan Ellis & Helen Burns @ CEMAC (UoL) +.. moduleauthor: Dan Ellis & Helen Burns @ CEMAC (UoL) .. description: This module was developed by CEMAC as part of the CSSP Brazil Project. This script is a back end web app script :copyright: © 2022 University of Leeds. @@ -11,6 +11,7 @@ https://github.com/cemac/AgroClimatic-Monitor """ import os, glob +from pathlib import Path ''' Global arguments ''' @@ -23,7 +24,8 @@ extensions = set(['txt', 'pdf', 'image/png', 'image/tiff','image/gtiff']) #text/html -apphome = '/var/www/AgroClimatic-Monitor/' +# cemaccam: Updated apphome to reflect wherever application is living +apphome = str(Path(__file__).resolve().parents[2]) + os.sep # '/var/www/dev-AgroClimatic-Monitor/' ### if change set in main/config.py too STORAGE = apphome + 'uolstorage/Data/' #symbolic link in main repo level 1 diff --git a/uolstorage b/uolstorage new file mode 120000 index 0000000..13a9f1b --- /dev/null +++ b/uolstorage @@ -0,0 +1 @@ +/nfs/earcemac/projects/cssp-brazil/uolstorage \ No newline at end of file