|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +__author__ = 'Robin Quetin, Shamal Faily' |
| 19 | + |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import sys |
| 23 | +if (sys.version_info > (3,)): |
| 24 | + import http.client |
| 25 | +else: |
| 26 | + import httplib |
| 27 | +from flask import Flask |
| 28 | +from flask_mail import Mail |
| 29 | +from flask_security import Security, SQLAlchemyUserDatastore, user_registered |
| 30 | +from flask_security import Security, SQLAlchemyUserDatastore |
| 31 | +from cairis.bin.add_cairis_user import addAdditionalUserData |
| 32 | +from flask_cors import CORS |
| 33 | +from cairis.core.Borg import Borg |
| 34 | +from cairis.daemon.WebConfig import * |
| 35 | +from .cdb import db |
| 36 | +from .models import User, Role |
| 37 | + |
| 38 | +app = Flask(__name__) |
| 39 | + |
| 40 | +@user_registered.connect_via(app) |
| 41 | +def enroll(sender, user, confirm_token,confirmation_token=None,form_data = {}): |
| 42 | + addAdditionalUserData(user.email, user.password) |
| 43 | + |
| 44 | + |
| 45 | +def create_app(): |
| 46 | + options = { |
| 47 | + 'port' : 0, |
| 48 | + 'unitTesting': False |
| 49 | + } |
| 50 | + WebConfig.config(options) |
| 51 | + |
| 52 | + b = Borg() |
| 53 | + app.config['DEBUG'] = True |
| 54 | + app.config['SECRET_KEY'] = b.secretKey |
| 55 | + app.config['SECURITY_PASSWORD_SALT'] = 'None' |
| 56 | + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 57 | + app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:' + b.rPasswd + '@' + b.dbHost + '/cairis_user' |
| 58 | + |
| 59 | + if (b.mailServer != '' and b.mailPort != '' and b.mailUser != '' and b.mailPasswd != ''): |
| 60 | + app.config['SECURITY_REGISTERABLE'] = True |
| 61 | + app.config['SECURITY_RECOVERABLE'] = True |
| 62 | + app.config['MAIL_SERVER'] = b.mailServer |
| 63 | + app.config['MAIL_PORT'] = b.mailPort |
| 64 | + app.config['MAIL_USE_SSL'] = True |
| 65 | + app.config['MAIL_USERNAME'] = b.mailUser |
| 66 | + app.config['MAIL_PASSWORD'] = b.mailPasswd |
| 67 | + app.config['SECURITY_EMAIL_SENDER'] = b.mailUser |
| 68 | + |
| 69 | + b.logger.setLevel(b.logLevel) |
| 70 | + b.logger.debug('Error handlers: {0}'.format(app.error_handler_spec)) |
| 71 | + app.secret_key = os.urandom(24) |
| 72 | + logger = logging.getLogger('werkzeug') |
| 73 | + logger.setLevel(b.logLevel) |
| 74 | + enable_debug = b.logLevel = logging.DEBUG |
| 75 | + |
| 76 | + mail = Mail(app) |
| 77 | + cors = CORS(app) |
| 78 | + with app.app_context(): |
| 79 | + db.init_app(app) |
| 80 | + user_datastore = SQLAlchemyUserDatastore(db,User, Role) |
| 81 | + security = Security(app, user_datastore) |
| 82 | + |
| 83 | + from .main import main as main_blueprint |
| 84 | + app.register_blueprint(main_blueprint) |
| 85 | + db.create_all() |
| 86 | + return app |
| 87 | + |
| 88 | +def create_test_app(): |
| 89 | + options = { |
| 90 | + 'port' : 0, |
| 91 | + 'unitTesting': True |
| 92 | + } |
| 93 | + WebConfig.config(options) |
| 94 | + |
| 95 | + b = Borg() |
| 96 | + app = Flask(__name__) |
| 97 | + app.config['DEBUG'] = True |
| 98 | + app.config['SECRET_KEY'] = b.secretKey |
| 99 | + app.config['SECURITY_PASSWORD_SALT'] = 'None' |
| 100 | + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 101 | + app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:' + b.rPasswd + '@' + b.dbHost + '/cairis_user' |
| 102 | + b.logger.setLevel(b.logLevel) |
| 103 | + b.logger.debug('Error handlers: {0}'.format(app.error_handler_spec)) |
| 104 | + app.secret_key = os.urandom(24) |
| 105 | + logger = logging.getLogger('werkzeug') |
| 106 | + logger.setLevel(b.logLevel) |
| 107 | + enable_debug = b.logLevel = logging.DEBUG |
| 108 | + db.init_app(app) |
| 109 | + user_datastore = SQLAlchemyUserDatastore(db,User, Role) |
| 110 | + security = Security(app, user_datastore) |
| 111 | + from .main import main as main_blueprint |
| 112 | + app.register_blueprint(main_blueprint) |
| 113 | + with app.app_context(): |
| 114 | + db.create_all() |
| 115 | + return app |
0 commit comments