Ejemplo n.º 1
0
def create_application(tg):
    tasks = {l: t for l, t in tg.tasks.items() if t.kind in SUPPORTED_KINDS}
    sections = [s.get_context(tasks) for s in SECTIONS]
    context = {
        "tasks": {l: t.attributes
                  for l, t in tasks.items()},
        "sections": sections,
    }

    app = Flask(__name__)
    app.env = "development"
    app.tasks = []

    @app.route("/", methods=["GET", "POST"])
    def chooser():
        if request.method == "GET":
            return render_template("chooser.html", **context)

        if request.form["action"] == "Push":
            labels = request.form["selected-tasks"].splitlines()
            app.tasks.extend(labels)

        shutdown = request.environ.get("werkzeug.server.shutdown")
        if shutdown:
            shutdown()
        return render_template("close.html")

    return app
Ejemplo n.º 2
0
def create_application(tg):
    tasks = {l: t for l, t in tg.tasks.items() if t.kind in SUPPORTED_KINDS}
    sections = [s.get_context(tasks) for s in SECTIONS]
    context = {
        'tasks': {l: t.attributes
                  for l, t in tasks.items()},
        'sections': sections,
    }

    app = Flask(__name__)
    app.env = 'development'
    app.tasks = []

    @app.route('/', methods=['GET', 'POST'])
    def chooser():
        if request.method == 'GET':
            return render_template('chooser.html', **context)

        if request.form['action'] == 'Push':
            labels = request.form['selected-tasks'].splitlines()
            app.tasks.extend(labels)

        shutdown = request.environ.get('werkzeug.server.shutdown')
        shutdown()
        return render_template('close.html')

    return app
Ejemplo n.º 3
0
def create_app() -> Flask:
    adapters = Adapters()
    adapters.config.from_ini('config.ini')

    tasks = Tasks(adapters=adapters)
    tasks.wire([
        category_api_module,
        employee_api_module,
        product_api_module,
        supplier_api_module,
        user_api_module,
        color_api_module,
        car_api_module,
    ])

    # creating the Flask application
    rest_app = Flask(__name__, static_folder=None)
    rest_app.config['SECRET_KEY'] = adapters.config()['jwt']['secret_key']
    rest_app.tasks = tasks

    adapters.bcrypt().init_app(rest_app)

    # register the REST API endpoints
    rest_app.register_blueprint(category_api_module.category_api)
    rest_app.register_blueprint(employee_api_module.employee_api)
    rest_app.register_blueprint(product_api_module.product_api)
    rest_app.register_blueprint(supplier_api_module.supplier_api)
    rest_app.register_blueprint(user_api_module.user_api)
    rest_app.register_blueprint(color_api_module.color_api)
    rest_app.register_blueprint(car_api_module.car_api)

    return rest_app
Ejemplo n.º 4
0
    poolclass=app.config["SQLALCHEMY_POOLCLASS"],
    pool_size=20,
    pool_recycle=3600,  # Recycle connections every 1 hr
    max_overflow=0,
    echo=False,
)

formatter = logging.Formatter(app.config["LOG_FORMAT"])
handler = RotatingFileHandler(app.config["LOG_FILE"])
handler.setFormatter(formatter)
handler.setLevel(app.config["LOG_LEVEL"])

app.logger.addHandler(handler)

# Add application logic to flask app (?)
app.tasks = TaskLogic(db_engine=engine)
app.register_blueprint(mod_tasks, url_prefix="/tasks")


@app.route("/")
def index():
    app.logger.info("Index called!")
    return "<h1>Ping!</h1><br /><h3>Welcome to SchedHTTP</h3>"


if __name__ == "__main__":
    try:
        app.run(host="0.0.0.0", port=6500, debug=True)
    except Exception:
        app.logger.exception("Failed")
Ejemplo n.º 5
0
import json
import time
import collections

from flask import Flask, render_template

# huey stuff
from config import huey
import tasks
import download_helper

app = Flask(__name__, static_folder='static')
app.tasks = collections.OrderedDict()


def json_response(data=None, status="ok"):
    return json.dumps({
        "status": status,
        "timestamp": time.time(),
        "data": data
    })


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/fetch/<sceneid>')
def fetch(sceneid):
    # start the background job
Ejemplo n.º 6
0
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import datetime
from redis import Redis
import rq
import time
import re
import stripe
from flask_cors import CORS
import json

load_dotenv()

app = Flask(__name__)
app.redis = Redis.from_url(os.getenv('REDIS_URL'))
app.tasks = rq.Queue('emailer-tasks', connection=app.redis)
CORS(app)

@app.route('/')
def status_page():
    return(render_template('main.html', content='The <strong>emailer</strong> microservice is running.'))

@app.route('/api/v1/form', methods=['GET','POST'])
def api_v1_form():
    if request.method == 'GET':
        return render_template('main.html', content='<strong>Access denied.</strong><br /><br />Your IP address has been logged and this incident has been reported to the authorities.')
    if not request.json and 'sourceForm' not in request.json:
        abort(400);
    # if not request.headers.get('Authorization') == ('Bearer ' + os.getenv('AUTH')):
        # abort(403);
    form_validates = False
Ejemplo n.º 7
0
def create_app():
    # get current configuration, or default to 'production' for safety
    config_name = os.environ.get('FLASK_ENV') or 'production'

    # load configuration files from 'instance' folder
    instance_dir = (Path(__file__).parent / "instance").absolute()
    app = Flask(__name__, instance_relative_config=True, instance_path=str(instance_dir))

    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('secrets.py')
    app.config.from_pyfile('mail.py')
    app.config.from_pyfile('rollbar.py')
    # app.config.from_pyfile('scout.py')
    app.config.from_pyfile('local.py')

    # create a long-lived Redis connection
    app.config['REDIS_SESSION'] = redis.Redis.from_url(url=app.config['CACHE_REDIS_URL'])

    # create long-lived Mongo connection for Flask-Sessionstore
    app.config['SESSION_MONGODB'] = MongoClient(host=app.config['SESSION_MONGO_URL'])

    # we have two proxies -- we're behind both waitress and nginx
    app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2)

    if app.config.get('PROFILE_MEMORY', False):
        app.wsgi_app = Dozer(app.wsgi_app)

    db.init_app(app)

    migrate = Migrate(app, db)
    bootstrap = Bootstrap(app)
    mail = Mail(app)
    bleach = Bleach(app)
    md = Markdown(app, extensions=[makeExtension(configs={'entities': 'named'})])
    rb = Rollbar(app)
    qr = QRcode(app)
    bbl = Babel(app)

    session_store = Session(app)

    cache.init_app(app)

    # add endpoint profiler and rate limiter in production mode
    # also add handler to direct Waitress logging output to the console
    if config_name == 'production':
        profiler = Profiler(app)

        # set up Flask-Limiter
        limiter.init_app(app)

    # add debug toolbar if in debug mode
    if config_name == 'development':
        toolbar = DebugToolbarExtension(app)
        # api_toolbar = DebugAPIExtension(app)

        # panels = list(app.config['DEBUG_TB_PANELS'])
        # panels.append('flask_debug_api.BrowseAPIPanel')
        # panels.append('flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel')
        # app.config['DEBUG_TB_PANELS'] = panels

    # set up CSS and javascript assets
    env = Environment(app)

    if not app.debug:
        from logging import INFO, Formatter, basicConfig
        from logging.handlers import RotatingFileHandler

        basicConfig(level=INFO)

        file_handler = RotatingFileHandler(app.config['LOG_FILE'], 'a', 1 * 1024 * 1024, 10)
        file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
        app.logger.setLevel(INFO)
        file_handler.setLevel(INFO)
        app.logger.addHandler(file_handler)
        app.logger.info('MPS Project Manager starting')

    # use Werkzeug built-in profiler if profile-to-disk is enabled
    if app.config.get('PROFILE_TO_DISK', False):
        app.config['PROFILE'] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, profile_dir=app.config.get('PROFILE_DIRECTORY'))

        app.logger.info('Profiling to disk enabled')

    # configure behaviour for uploaded files
    asset_folder = Path(app.config.get('ASSETS_FOLDER'))
    uploaded_subfolder = Path(app.config.get('ASSETS_UPLOADED_SUBFOLDER'))
    submitted_subfolder = Path(app.config.get('ASSETS_SUBMITTED_SUBFOLDER'))

    abs_uploaded_path = asset_folder / uploaded_subfolder
    abs_uploaded_path.mkdir(parents=True, exist_ok=True)

    abs_submissions_path = asset_folder / submitted_subfolder
    abs_submissions_path.mkdir(parents=True, exist_ok=True)

    app.config['UPLOADED_SOLUTIONS_DEST'] = abs_uploaded_path
    app.config['UPLOADED_BATCHUSERLIST_DEST'] = abs_uploaded_path
    app.config['UPLOADED_SUBMISSIONS_DEST'] = abs_submissions_path
    configure_uploads(app, [solution_files, batch_user_files, submitted_files])

    # configure Flask-Security, which needs access to the database models for User and Role
    from app import models

    user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role)

    # patch Flask-Security's login form to include some descriptive text on the email field
    security = Security(app, user_datastore, login_form=PatchedLoginForm, mail_util_cls=PatchedMailUtil)
    if config_name == 'production':
        # set up more stringent limits for login view and forgot-password view
        # add to a particular view function.
        login = app.view_functions['security.login']
        forgot = app.view_functions['security.forgot_password']
        limiter.limit("50/day;5/minute")(login)
        limiter.limit("50/day;5/minute")(forgot)


    # set up celery and store in extensions dictionary
    celery = make_celery(app)
    app.extensions['celery'] = celery

    # register celery tasks
    # there doesn't seem a good way of doing this using factory functions! Here I compromise by passing the
    # celery application instance to a collection of register_*() functions, which use an @celery decorator
    # to register callables. Then we write the callable into the app, in the 'tasks' dictionary
    app.tasks = {}
    tasks.register_send_log_email(celery, mail)
    tasks.register_utility_tasks(celery)
    tasks.register_prune_email(celery)
    tasks.register_backup_tasks(celery)
    tasks.register_rollover_tasks(celery)
    tasks.register_issue_confirm_tasks(celery)
    tasks.register_golive_tasks(celery)
    tasks.register_close_selection_tasks(celery)
    tasks.register_user_launch_tasks(celery)
    tasks.register_popularity_tasks(celery)
    tasks.register_matching_tasks(celery)
    tasks.register_matching_email_tasks(celery)
    tasks.register_availability_tasks(celery)
    tasks.register_scheduling_tasks(celery)
    tasks.register_maintenance_tasks(celery)
    tasks.register_assessment_tasks(celery)
    tasks.register_assessor_tasks(celery)
    tasks.register_email_notification_tasks(celery)
    tasks.register_precompute_tasks(celery)
    tasks.register_push_feedback_tasks(celery)
    tasks.register_system_tasks(celery)
    tasks.register_batch_create_tasks(celery)
    tasks.register_selecting_tasks(celery)
    tasks.register_session_tasks(celery)
    tasks.register_marking_tasks(celery)
    tasks.register_services_tasks(celery)
    tasks.register_test_tasks(celery)


    @security.login_context_processor
    def login_context_processor():
        # build list of system messages to consider displaying on login screen
        messages = []
        for message in MessageOfTheDay.query.filter_by(show_login=True).all():
            if message.project_classes.first() is None:
                messages.append(message)

        return dict(messages=messages)


    @app.before_request
    def before_request_handler():
        if current_user.is_authenticated:
            if request.endpoint is not None and 'ajax' not in request.endpoint:
                try:
                    Notification.query.filter_by(remove_on_pageload=True).delete()
                    db.session.commit()
                except SQLAlchemyError as e:
                    current_app.logger.exception("SQLAlchemyError exception", exc_info=e)


    @app.template_filter('dealingwithdollars')
    def dealingwithdollars(latex_string):
        if latex_string is None:
            return r'<div class="alert alert-danger">An empty string was supplied. ' \
                   r'Please check your project description.</div>'

        splat = list(latex_string)  # Splits string into list of characters
        dollar_inds = [i for i in range(0, len(splat)) if splat[i] == "$"]  # Finds indices of all dollar signs
        display_inds = []  # Less pythonic than list comprehension, but now inline_inds can exclude double dollar signs
        for elem in dollar_inds:
            if elem != len(splat) - 1:
                if splat[elem + 1] == r"$":
                    display_inds.append(elem)
                    display_inds.append(elem + 1)
        inline_inds = [elem for elem in dollar_inds if splat[elem - 1] != "\\" and elem not in display_inds]  # \$ is allowed in LaTeX, $ is not.
        just_dollar = [elem for elem in dollar_inds if elem not in inline_inds and elem not in display_inds]

        if len(inline_inds) % 2 != 0:  # Checks for lonely dollar signs
            latex_string = r'<div class="alert alert-danger">Failed to match LaTeX dollar delimiters. ' \
                           r'Please check the markup in your project description.</div>' + latex_string

        else:  # Only converts inline math delimiters, as latex2markdown seems to convert display math delimiters
            for i in range(0, len(inline_inds)):
                if i % 2 == 0:
                    splat[inline_inds[i]] = r"\\("
                else:
                    splat[inline_inds[i]] = r"\\)"

            for elem in just_dollar:
                splat.pop(elem - 1)

            latex_string = ''.join(splat)

        l2m_obj = latex2markdown.LaTeX2Markdown(latex_string)
        mathjax_string = l2m_obj.to_markdown()
        return mathjax_string


    @app.template_filter('urlencode')
    def urlencode_filter(s):
        if s is None:
            return None

        s = s.encode('utf8')
        s = parse.quote_plus(s)
        return bleach.clean(s)


    def _get_previous_login():
        if not has_request_context():
            return None

        if session.get('previous_login', None) is not None:
            real_id = session['previous_login']
            real_user = db.session.query(User).filter_by(id=real_id).first()
        else:
            real_user = None

        return real_user


    def _get_live_platform():
        if not has_request_context():
            return None

        return current_app.config.get('EMAIL_IS_LIVE', False)


    # collect all global context functions together in an attempt to avoid function call overhead
    @app.context_processor
    def global_context():
        if not has_request_context():
            return {}

        return {'get_previous_login': _get_previous_login,
                'website_revision': site_revision,
                'website_copyright_dates': site_copyright_dates,
                'build_version': git_tag,
                'home_dashboard_url': home_dashboard_url(),
                'get_base_context': get_global_context_data,
                'get_live_platform': _get_live_platform}


    @app.errorhandler(404)
    def not_found_error(error):
        return render_template('errors/404.html'), 404


    @app.errorhandler(429)
    def rate_limit_error(error):
        return render_template('errors/429.html'), 429


    @app.errorhandler(500)
    def internal_error(error):
        db.session.rollback()
        return render_template('errors/500.html'), 500


    if not app.debug:
        @app.after_request
        def after_request(response):
            timeout = app.config['DATABASE_QUERY_TIMEOUT']

            for query in get_debug_queries():
                if query.duration >= timeout:
                    app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % (
                    query.statement, query.parameters, query.duration, query.context))
            return response


    @user_logged_in.connect_via(app)
    def login_callback(self, user):
        # DS 3 Feb 2019 - why did we want to clear notifications?
        # disabled this for now

        # # clear notifications for the user who has just logged in
        # Notification.query.filter_by(user_id=user.id).delete()

        # force precompute of expensive views
        celery = current_app.extensions['celery']
        precompute_at_login(user, celery, autocommit=False)

        user.last_active = datetime.now()
        db.session.commit()


    from flask import Request
    class CustomRequest(Request):
        @property
        def rollbar_person(self):
            db.session.rollback()

            if current_user is None:
                return None

            # 'id' is required, 'username' and 'email' are indexed but optional.
            # all values are strings.
            return {'id': str(current_user.id),
                    'username': str(current_user.username),
                    'email': str(current_user.email)}

    app.request_class = CustomRequest


    # IMPORT BLUEPRINTS

    from .home import home as home_blueprint
    app.register_blueprint(home_blueprint, url_prefix='/')

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from .admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')

    from .faculty import faculty as faculty_blueprint
    app.register_blueprint(faculty_blueprint, url_prefix='/faculty')

    from .convenor import convenor as convenor_blueprint
    app.register_blueprint(convenor_blueprint, url_prefix='/convenor')

    from .student import student as student_blueprint
    app.register_blueprint(student_blueprint, url_prefix='/student')

    from .office import office as office_blueprint
    app.register_blueprint(office_blueprint, url_prefix='/office')

    from .reports import reports as reports_blueprint
    app.register_blueprint(reports_blueprint, url_prefix='/reports')

    from .user_approver import user_approver as user_approver_blueprint
    app.register_blueprint(user_approver_blueprint, url_prefix='/user_approver')

    from .project_approver import project_approver as project_approver_blueprint
    app.register_blueprint(project_approver_blueprint, url_prefix='/project_approver')

    from .loadbalancer import alb as alb_blueprint
    app.register_blueprint(alb_blueprint, url_prefix='/alb')

    from .manage_users import manage_users as manage_users_blueprint
    app.register_blueprint(manage_users_blueprint, url_prefix='/manage_users')

    from .documents import documents as documents_blueprint
    app.register_blueprint(documents_blueprint, url_prefix='/documents')

    from .services import services as services_blueprint
    app.register_blueprint(services_blueprint, url_prefix='/services')

    from .projecthub import projecthub as projecthub_blueprint
    app.register_blueprint(projecthub_blueprint, url_prefix='/projecthub')

    return app, celery
Ejemplo n.º 8
0
# def create_app(config_filename):
#    app = Flask(__name__)
#    app.config.from_pyfile(config_filename)
#    return app

app = Flask(__name__)

# Exploring creating RESTful Services
# Ref: https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
app.tasks = [{
    'id': 1,
    'title': u'Buy groceries',
    'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
    'done': False
}, {
    'id': 2,
    'title': u'Learn Python',
    'description': u'Need to find a good Python tutorial on the web',
    'done': False
}]


@app.route('/')  #decorator mapping root call
@app.route('/index')  #decorator mapping /index call
def index():
    return "Debug: Hello, welcome to the Web Server of team  Warriors"


@app.route('/api/v1.0/tasks', methods=['GET'])
def get_tasks():
Ejemplo n.º 9
0
__license__ = 'MIT'
__email__ = '*****@*****.**'

import json
import os
import sys
import uuid

from flask import Flask, render_template, request, redirect, make_response
from jinja2 import Environment, FileSystemLoader

import clsmenu

app = Flask(__name__)

app.tasks = []
app.menu = clsmenu.MenuStruct()
app.menu.read_json(os.path.dirname(__file__) + '/menu.json')
app.params = []

def genjson():
    result = app.tasks
    for typeres in result:
        for typeparam in app.params:
            if typeparam['type'] == typeres['type']:
                for actionres in typeres['actions']:
                    for actionparam in typeparam['actions']:
                        print >>sys.stderr, actionparam
                        if actionres['action'] == actionparam['action']:
                            for reskey in actionparam.keys():
                                actionres[reskey] = actionparam[reskey]