Example #1
0
def create_app(db_url):
    app = Flask(__name__)
    (app.db_session, app.db_metadata, app.db_engine) = init_db(db_url)
    app.debug = os.environ.get('DEBUG') == 'True'
    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){1,}')

    @app.teardown_request
    def shutdown_session(exception=None):
        app.db_session.remove()

    @app.template_filter('strftime')
    def _jinja2_filter_datetime(date, in_format='%Y-%m-%d', out_format='%d-%m-%Y'):
        if date:
            date = datetime.datetime.strptime(date, in_format)
            return date.strftime(out_format)

    @app.template_filter('nl2br')
    def _nl2br(value):
        result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') \
                              for p in _paragraph_re.split(escape(value)))
        result = Markup(result)
        return result

    @app.template_filter('sort_vedtak')
    def _jinja2_filter_sort_vedtak(vedtak):
        if len(vedtak) == 0:
            return []
        else:
            id_sorted = sorted(vedtak, key=id)
            s = sorted(id_sorted,
                       reverse=True,
                       key=lambda v: v.get('vedtaksdato')
                       if v.get('vedtaksdato')
                       else datetime.datetime.now().isoformat())
            return s

    create_api(app, API_VERSION)
    create_bouncer(app)

    # support for remote debugging in Intellij and pycharm
    #
    # Set IDEA_SAK_REMOTE_DEBUG_ON to True in your environment
    # prior to starting the application to get remote debugging.
    #
    # Set IDEA_REMOTE_DEBUG_SERVER to the ip/hostname of the machine running the
    # debug server.
    #
    # Set IDEA_SAK_REMOTE_DEBUG_SERVER to the port of the debug server prosess
    #
    # For the remote debugging to work you will also have to make sure
    # the pycharm-debug.egg is on your path (check your environment file).
    if os.environ.get('IDEA_SAK_REMOTE_DEBUG_ON') == 'True':
        server = os.environ.get('IDEA_REMOTE_DEBUG_SERVER')
        port = os.environ.get('IDEA_SAK_REMOTE_DEBUG_PORT')
        app.logger.info("Idea remote debugging is on! Will connect to debug server running on %s:%s" % (server, port))
        import pydevd
        pydevd.settrace(server, port=int(port), suspend=False, stdoutToServer = True, stderrToServer = True)

    return app
Example #2
0
def create_app(db_url):
    app = Flask(__name__)
    (app.db_session, app.db_metadata, app.db_engine) = init_db(db_url)
    app.debug = os.environ.get('DEBUG') == 'True'

    @app.teardown_request
    def shutdown_session(exception=None):
        app.db_session.remove()

    if not app.debug:
        app.logger.addHandler(StreamHandler())

    create_api(app, API_VERSION)
    create_bouncer(app)
    return app
for name, bundle in bundles.iteritems():
    assets.register(name, bundle)

from flod_tilskudd_portal import views, proxy


def check_environment(app):
    if 'AUTH_TOKEN_SECRET' not in os.environ:
        raise EnvironmentError(('Environment variable AUTH_TOKEN_SECRET must '
                                'be set'))


check_environment(app)

create_api(app, api_version)
create_bouncer(app)

# support for remote debugging in Intellij and pycharm
#
# Set IDEA_TILSKUDD_PORTAL_REMOTE_DEBUG_ON to True in your environment
# prior to starting the application to get remote debugging.
#
# Set IDEA_REMOTE_DEBUG_SERVER to the ip/hostname of the machine running the
# debug server.
#
# Set IDEA_TILSKUDD_PORTAL_REMOTE_DEBUG_SERVER to the port of the debug server prosess
#
# For the remote debugging to work you will also have to make sure
# the pycharm-debug.egg is on your path (check your environment file).
if os.environ.get('IDEA_TILSKUDD_PORTAL_REMOTE_DEBUG_ON') == 'True':
    server = os.environ.get('IDEA_REMOTE_DEBUG_SERVER')