2020-10-23 08:07:14 +00:00
|
|
|
import os
|
2021-02-19 18:03:01 +00:00
|
|
|
import configparser
|
2020-10-23 08:07:14 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from flask import Flask, render_template, request
|
|
|
|
from flask_dropzone import Dropzone
|
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
import portal.base
|
2021-02-19 18:03:01 +00:00
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
# TODO: Put all the stuff in base.py into a class, so it can have a state of it's own, to avoid passing a bunch of arguments at each function call.
|
2021-02-19 18:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
######################
|
|
|
|
# Load configuration #
|
|
|
|
######################
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('config.ini')
|
|
|
|
config = config['DEFAULT']
|
|
|
|
|
|
|
|
MAIL_HOST = config['MAIL_HOST']
|
|
|
|
MAIL_LOGIN = config['MAIL_LOGIN']
|
|
|
|
MAIL_PASS = config['MAIL_PASS']
|
|
|
|
SMTP_PORT = int(config['SMTP_PORT'])
|
|
|
|
IMAP_PORT = int(config['IMAP_PORT'])
|
|
|
|
MAX_UPLOAD_SIZE = int(config['MAX_UPLOAD_SIZE']) # Bytes
|
|
|
|
CONTRACT_CLIENT_CONTACT = config['CONTRACT_CLIENT_CONTACT']
|
2021-03-10 17:55:24 +00:00
|
|
|
MAIL_SUBJECT = config['MAIL_SUBJECT']
|
|
|
|
MAIL_BODY = config['MAIL_BODY']
|
|
|
|
|
|
|
|
if 'UPLOADS_DIR' in config:
|
|
|
|
UPLOADS_DIR = Path(config['UPLOADS_DIR'])
|
|
|
|
else:
|
|
|
|
UPLOADS_DIR = Path(__file__).resolve().parent / 'uploads'
|
|
|
|
if not UPLOADS_DIR.exists:
|
|
|
|
UPLOADS_DIR.mkdir(parents=True)
|
|
|
|
|
|
|
|
if 'DATA_DIR' in config:
|
|
|
|
DATA_DIR = Path(config['DATA_DIR'])
|
2021-02-19 18:03:01 +00:00
|
|
|
else:
|
2021-03-10 17:55:24 +00:00
|
|
|
DATA_DIR = Path(__file__).resolve().parent / 'data'
|
|
|
|
if not DATA_DIR.exists:
|
|
|
|
DATA_DIR.mkdir(parents=True)
|
2021-02-19 18:03:01 +00:00
|
|
|
|
|
|
|
# Override configs with environment variables, if set
|
|
|
|
if 'PORTALDS4DS1_MAIL_HOST' in os.environ:
|
2021-02-20 19:07:21 +00:00
|
|
|
MAIL_HOST = os.environ['PORTALDS4DS1_MAIL_HOST']
|
2021-02-19 18:03:01 +00:00
|
|
|
if 'PORTALDS4DS1_MAIL_LOGIN' in os.environ:
|
2021-02-20 19:07:21 +00:00
|
|
|
MAIL_LOGIN = os.environ['PORTALDS4DS1_MAIL_LOGIN']
|
2021-02-19 18:03:01 +00:00
|
|
|
if 'PORTALDS4DS1_MAIL_PASS' in os.environ:
|
2021-02-20 19:07:21 +00:00
|
|
|
MAIL_PASS = os.environ['PORTALDS4DS1_MAIL_PASS']
|
2021-02-19 18:03:01 +00:00
|
|
|
if 'PORTALDS4DS1_SMTP_PORT' in os.environ:
|
2021-02-20 19:07:21 +00:00
|
|
|
SMTP_PORT = int(os.environ['PORTALDS4DS1_SMTP_PORT'])
|
2021-02-19 18:03:01 +00:00
|
|
|
if 'PORTALDS4DS1_IMAP_PORT' in os.environ:
|
2021-02-20 19:07:21 +00:00
|
|
|
IMAP_PORT = int(os.environ['PORTALDS4DS1_IMAP_PORT'])
|
|
|
|
if 'PORTALDS4DS1_MAX_UPLOAD_SIZE' in os.environ:
|
|
|
|
MAX_UPLOAD_SIZE = int(os.environ['PORTALDS4DS1_MAX_UPLOAD_SIZE'])
|
|
|
|
if 'PORTALDS4DS1_CONTRACT_CLIENT_CONTACT' in os.environ:
|
|
|
|
CONTRACT_CLIENT_CONTACT = os.environ['PORTALDS4DS1_CONTRACT_CLIENT_CONTACT']
|
2021-03-10 17:55:24 +00:00
|
|
|
if 'PORTALDS4DS1_UPLOADS_DIR' in os.environ:
|
|
|
|
UPLOADS_DIR = os.environ['PORTALDS4DS1_UPLOADS_DIR']
|
|
|
|
if 'PORTALDS4DS1_DATA_DIR' in os.environ:
|
|
|
|
DATA_DIR = os.environ['PORTALDS4DS1_DATA_DIR']
|
|
|
|
if 'PORTALDS4DS1_MAIL_SUBJECT' in os.environ:
|
|
|
|
MAIL_SUBJECT = os.environ['PORTALDS4DS1_MAIL_SUBJECT']
|
|
|
|
if 'PORTALDS4DS1_MAIL_BODY' in os.environ:
|
|
|
|
MAIL_BODY = os.environ['PORTALDS4DS1_MAIL_BODY']
|
|
|
|
|
|
|
|
VALID_CORPUS_NAMES = ['prevodi', 'gigafida', 'solar']
|
2021-02-19 18:03:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
######################
|
2020-10-23 08:07:14 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
app.config.update(
|
2021-03-10 17:55:24 +00:00
|
|
|
UPLOADED_PATH = UPLOADS_DIR,
|
2021-02-19 18:03:01 +00:00
|
|
|
MAX_CONTENT_LENGTH = MAX_UPLOAD_SIZE,
|
2021-02-12 18:36:10 +00:00
|
|
|
TEMPLATES_AUTO_RELOAD = True
|
2020-10-23 08:07:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
dropzone = Dropzone(app)
|
|
|
|
|
|
|
|
|
2021-02-21 14:16:40 +00:00
|
|
|
@app.route(URL_ROOT)
|
2020-10-23 08:07:14 +00:00
|
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
@app.route('/<corpus_name>')
|
|
|
|
def index_corpus(corpus_name):
|
|
|
|
if corpus_name not in VALID_CORPUS_NAMES:
|
|
|
|
return 'Korpus "{}" ne obstaja.'.format(corpus_name), 404
|
|
|
|
if corpus_name == 'prevodi':
|
|
|
|
subtitle = 'KORPUS PARALELNIH BESEDIL ANG-SLO'
|
|
|
|
elif corpus_name == 'gigafida':
|
|
|
|
subtitle = 'KORPUS GIGAFIDA'
|
|
|
|
return render_template('basic.html', subtitle=subtitle, corpus_name=corpus_name)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/<corpus_name>/upload', methods=['POST'])
|
|
|
|
def handle_upload(corpus_name):
|
|
|
|
if corpus_name not in VALID_CORPUS_NAMES:
|
|
|
|
return 404
|
|
|
|
|
2020-10-23 08:07:14 +00:00
|
|
|
files = request.files
|
|
|
|
if len(files) > 20:
|
|
|
|
return 'Naložite lahko do 20 datotek hkrati.', 400
|
|
|
|
elif len(files) < 1:
|
|
|
|
return 'Priložena ni bila nobena datoteka.', 400
|
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
print('one')
|
|
|
|
err = portal.base.check_suffixes(files)
|
2020-10-23 08:07:14 +00:00
|
|
|
if err:
|
|
|
|
return err, 400
|
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
print('two')
|
|
|
|
err = portal.base.check_form(request.form)
|
2020-10-23 08:07:14 +00:00
|
|
|
if err:
|
|
|
|
return err, 400
|
|
|
|
|
2021-03-10 17:55:24 +00:00
|
|
|
print('three')
|
|
|
|
upload_metadata = portal.base.get_upload_metadata(corpus_name, request)
|
|
|
|
contract_file_name = portal.base.generate_contract_pdf(UPLOADS_DIR, upload_metadata, CONTRACT_CLIENT_CONTACT)
|
|
|
|
|
2021-02-19 18:03:01 +00:00
|
|
|
# Add contract_file_name to metadata TODO: move somewhere else
|
|
|
|
upload_metadata['contract'] = contract_file_name
|
2021-03-10 17:55:24 +00:00
|
|
|
portal.base.store_datafiles(UPLOADS_DIR, files, upload_metadata)
|
|
|
|
portal.base.store_metadata(UPLOADS_DIR, upload_metadata)
|
|
|
|
portal.base.send_confirm_mail(
|
|
|
|
subject=MAIL_SUBJECT,
|
|
|
|
body=MAIL_BODY,
|
|
|
|
uploads_path=UPLOADS_DIR,
|
|
|
|
upload_metadata=upload_metadata,
|
|
|
|
mail_host=MAIL_HOST, mail_login=MAIL_LOGIN, mail_pass=MAIL_PASS,
|
|
|
|
imap_port=IMAP_PORT, smtp_port=SMTP_PORT)
|
2021-02-12 18:36:10 +00:00
|
|
|
|
2020-10-23 08:07:14 +00:00
|
|
|
return 'Uspešno ste oddali datotek(e). Št. datotek: {}'.format(len(files))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|