Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulling in Camerons upload time added #93

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.. module:: app
:platform: Unix
:synopsis: Main flask app
.. 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 the main flask application file.
:copyright: © 2022 University of Leeds.
Expand All @@ -26,12 +26,12 @@
from werkzeug.utils import secure_filename
import simplejson as json
import pandas as pd
from params import indicators
from pathlib import Path
import sys, os, re, glob
file = Path(__file__).resolve()
parent, root = file.parent, file.parents[1] # 1 level up
sys.path.append(str(root))
from params import indicators # Needs to come after sys.path has been amended

# import the about scripts
importdir.do(str(parent) + '/about', globals())
Expand All @@ -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 Expand Up @@ -505,4 +514,4 @@ def data_get(upload_id):
print('to upload files navigate to http://127.0.0.1:57263/upload')
# lets run this on localhost port 4000
# socketio.run(app,host='129.11.78.152',port=57263,debug=True)#,threaded=True)
app.run()
app.run(port=4000)
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
3 changes: 2 additions & 1 deletion app/templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ <h1 class="h3 mb-0 text-gray-800" style='margin-left:10px'>Data Portal</h1>
</p>
<p><b>please use the following file name convention:</b>
<br>
index_name_YYYY_MM.tif (where index_name can take: IIS3, RZSM, SPI01, SPI03, SPI06 and SPI12)
index_name_YYYY_MM.tif (where index_name can take: IIS3, RZSM, SPI01, SPI03, SPI06 and SPI12)<br>
<br>Data is updated daily at 5am UTC.
</p>
</div>

Expand Down
1 change: 1 addition & 0 deletions uolstorage