Newer
Older
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from cStringIO import StringIO
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from logging.config import dictConfig
from threading import Thread
import base64
import hashlib
from flask import render_template, send_from_directory
from flask import request, has_request_context
from flask import send_file
from flask import session
from flask import url_for
from flask_compress import Compress
from werkzeug import abort, redirect
from werkzeug.http import http_date
from werkzeug.middleware.proxy_fix import ProxyFix
import pytz
import time
from PiAnoS import caseExistsInDB
from utils.decorator import admin_required, login_required, submission_has_access
from functions import dek_generate, do_encrypt_dek, do_decrypt_dek, dek_check
from functions import do_encrypt_user_session, do_decrypt_user_session
from functions import no_preview_image
################################################################################
from version import __version__, __branch__, __commit__, __commiturl__, __treeurl__
################################################################################
logrequestre = re.compile( "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*\[[^\]]+\]\s(.*)" )
class RequestFormatter( logging.Formatter ):
def format( self, record ):
if has_request_context():
try:
username = session[ "username" ]
except:
username = "-"
record.msg = "{REMOTE_ADDR} (" + username + ") - " + record.msg
record.msg = record.msg.format( **request.headers.environ )
m = logrequestre.match( record.msg )
if m:
record.msg = m.group( 2 )
return super( RequestFormatter, self ).format( record )
class myFilter( object ):
def filter( self, record ):
if "{}/ping".format( config.baseurl ) in record.msg and " 200 " in record.msg:
return 0
else:
return 1
class myStreamHandler( logging.StreamHandler ):
def __init__( self ):
logging.StreamHandler.__init__( self )
self.addFilter( myFilter() )
"version": 1,
"formatters": {
"default": {
"()": "module.RequestFormatter",
"format": "[%(asctime)s] %(levelname)s: \t%(message)s",
"handlers": {
"console": {
"class": "module.myStreamHandler",
"formatter": "default"
"root": {
"level": "INFO",
"handlers": [ "console" ]
}
} )
################################################################################
if config.PROXY:
app.wsgi_app = ProxyFix( app.wsgi_app )
################################################################################
# Overloads
def my_render_template( *args, **kwargs ):
kwargs[ "config.baseurl" ] = config.baseurl
kwargs[ "envtype" ] = config.envtype
kwargs[ "js" ] = config.cdnjs
kwargs[ "css" ] = config.cdncss
kwargs[ "session_timeout" ] = config.session_timeout
kwargs[ "session_security_key" ] = session.get( "session_security_key" )
kwargs[ "account_type" ] = session.get( "account_type", None )
kwargs[ "nist_file_extensions" ] = json.dumps( config.NIST_file_extensions )

Marco De Donno
committed
if session.get( "account_type", False ):
at = account_type_id_name[ session.get( "account_type" ) ]
kwargs[ "navigation" ] = "navigations/{}.html".format( at.lower() )
kwargs[ "pianosendpoint" ] = config.pianosendpoint
return render_template( *args, **kwargs )
################################################################################
# Generic routing
@app.route( config.baseurl + "/ping" )
"""
Ping function to check if the web application is healthy.
"""
@app.route( config.baseurl + "/version" )
"""
Function to report the version of the web app.
The version.py file is re-generated by the CI/CD for production.
"""
"error": False,
"version": __version__,
"branch": __branch__,
"commit": __commit__,
"commiturl": __commiturl__,
"treeurl": __treeurl__
} )
except:
return jsonify( {
"error": True
} )
################################################################################
# CDN serving
@app.route( config.baseurl + "/cdn/<path:subpath>" )
def send_cdn_files( subpath ):
"""
Serve the files from the cdn directory.
"""
return send_from_directory( "cdn", subpath )
################################################################################
# App serving
@app.route( config.baseurl + "/app/<path:subpath>" )
def send_app_files( subpath ):
"""
Serve the file from the app directory (all files related to the ICNML application).
"""
return send_from_directory( "app", subpath )
@app.route( config.baseurl + "/static/<path:subpath>" )
def send_static_files( subpath ):
"""
Serve static files from the static directory.
"""
return send_from_directory( "static", subpath )
Loading
Loading full blame...