import os import configparser from pathlib import Path from flask import Flask, render_template, request from flask_dropzone import Dropzone import portal.base # 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. ###################### # 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'] 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']) else: DATA_DIR = Path(__file__).resolve().parent / 'data' if not DATA_DIR.exists: DATA_DIR.mkdir(parents=True) # Override configs with environment variables, if set if 'PORTALDS4DS1_MAIL_HOST' in os.environ: MAIL_HOST = os.environ['PORTALDS4DS1_MAIL_HOST'] if 'PORTALDS4DS1_MAIL_LOGIN' in os.environ: MAIL_LOGIN = os.environ['PORTALDS4DS1_MAIL_LOGIN'] if 'PORTALDS4DS1_MAIL_PASS' in os.environ: MAIL_PASS = os.environ['PORTALDS4DS1_MAIL_PASS'] if 'PORTALDS4DS1_SMTP_PORT' in os.environ: SMTP_PORT = int(os.environ['PORTALDS4DS1_SMTP_PORT']) if 'PORTALDS4DS1_IMAP_PORT' in os.environ: 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'] 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'] ###################### app = Flask(__name__) app.config.update( UPLOADED_PATH = UPLOADS_DIR, MAX_CONTENT_LENGTH = MAX_UPLOAD_SIZE, TEMPLATES_AUTO_RELOAD = True ) dropzone = Dropzone(app) @app.route(URL_ROOT) def index(): return render_template('index.html') @app.route('/') 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('//upload', methods=['POST']) def handle_upload(corpus_name): if corpus_name not in VALID_CORPUS_NAMES: return 404 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 print('one') err = portal.base.check_suffixes(files) if err: return err, 400 print('two') err = portal.base.check_form(request.form) if err: return err, 400 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) # Add contract_file_name to metadata TODO: move somewhere else upload_metadata['contract'] = contract_file_name 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) return 'Uspešno ste oddali datotek(e). Št. datotek: {}'.format(len(files)) if __name__ == '__main__': app.run(debug=True)