Skip to content

Commit

Permalink
amended hard-coded filepaths and uolstorage symlink, to allow to run …
Browse files Browse the repository at this point in the history
…locally
  • Loading branch information
cemaccam committed Jul 13, 2023
1 parent 04b187f commit 01cf6ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
55 changes: 32 additions & 23 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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/<folder>/<item>/')
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)


'''
Expand Down
6 changes: 4 additions & 2 deletions app/serverscripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -11,6 +11,7 @@
https://github.com/cemac/AgroClimatic-Monitor
"""
import os, glob
from pathlib import Path
'''
Global arguments
'''
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions uolstorage

0 comments on commit 01cf6ba

Please sign in to comment.