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 threading import Thread
import functools
import hashlib
import json
from PIL import Image, ImageDraw, ImageFont
from flask import render_template, send_from_directory
from flask import send_file
from flask import session
from flask import url_for
from flask_compress import Compress
from werkzeug import abort, redirect
import gnupg
import pytz
import time
from PiAnoS import caseExistsInDB
from functions import pbkdf2, AESCipher
from functions import random_data
from functions import render_jinja_html
from functions import rotate_image_upon_exif
from functions import sql_insert_generate
################################################################################
from version import __version__, __branch__, __commit__, __commiturl__, __treeurl__
################################################################################

Marco De Donno
committed
Image.MAX_IMAGE_PIXELS = 1 * 1024 * 1024 * 1024
################################################################################
baseurl = os.environ.get( "BASEURL", "" )
envtype = os.environ.get( "ENVTYPE", "" )
################################################################################
gnupg._parsers.Verify.TRUST_LEVELS[ "ENCRYPTION_COMPLIANCE_MODE" ] = 23
################################################################################

Marco De Donno
committed
# Decorators

Marco De Donno
committed
def session_field_required( field, value ):
def decorator( func ):
@functools.wraps( func )
def wrapper_login_required( *args, **kwargs ):
if not field in session:
return redirect( url_for( "login" ) )
elif not session.get( field ) == value:
return redirect( url_for( "login" ) )
return func( *args, **kwargs )
return wrapper_login_required
return decorator
def login_required( func ):
@functools.wraps( func )
def wrapper_login_required( *args, **kwargs ):
return redirect( url_for( "login" ) )
return func( *args, **kwargs )
return wrapper_login_required
def referer_required( func ):
@functools.wraps( func )
def wrapper_login_required( *args, **kwargs ):
if not request.headers.get( "Referer", False ):
return "referrer needed", 404
return func( *args, **kwargs )
return wrapper_login_required
def admin_required( func ):
@functools.wraps( func )
def wrapper_login_required( *args, **kwargs ):
if not session.get( "logged", False ) or not session.get( "account_type", None ) == 1:
return redirect( url_for( "login" ) )
return func( *args, **kwargs )
return wrapper_login_required
def submitter_required( func ):
@functools.wraps( func )
def wrapper_login_required( *args, **kwargs ):
if not session.get( "logged", False ) or not session.get( "account_type", None ) == 3:
return redirect( url_for( "login" ) )
return func( *args, **kwargs )
return wrapper_login_required

Marco De Donno
committed
def redis_cache( ttl = 3600 ):
def decorator( func ):
@functools.wraps( func )
def wrapper_cache( *args, **kwargs ):
lst = []
lst.append( func.__name__ )
lst.extend( args )
index = "_".join( lst )
index = hashlib.sha256( index ).hexdigest()
d = config.redis_shared.get( index )
if d != None:
buff = StringIO()
buff.write( base64.b64decode( d ) )
buff.seek( 0 )

Marco De Donno
committed
else:
d = func( *args, **kwargs )
buff = StringIO()

Marco De Donno
committed
buff.seek( 0 )
d_cached = base64.b64encode( buff.getvalue() )
config.redis_shared.set( index, d_cached, ex = ttl )
return d
return wrapper_cache
return decorator
################################################################################
# Overloads
def my_render_template( *args, **kwargs ):
kwargs[ "baseurl" ] = baseurl
kwargs[ "envtype" ] = 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() )
return render_template( *args, **kwargs )
################################################################################
# Generic routing
@app.route( "/ping" )
@app.route( baseurl + "/ping" )
"""
Ping function to check if the web application is healthy.
This function need to check all important element of the web application, i.e. the flask app and the postgresql database.
If the application report a error 500, the healthcheck done by the container orchestrator will fail, hence re-scheduling the container as needed.
"""
config.db.connect() # Try to re-connect to the database to try to serve the requests before the rescheduling
Loading
Loading full blame...