Exemplo n.º 1
0
import traceback

from functools import wraps

from pymongo.cursor import Cursor
from pymongo.command_cursor import CommandCursor

from utils.common import logging
from utils.common.connectors import Connector

logger = logging.getLogger("{}.{}".format(__name__, "MongoDbDecorator"))


class MongoDbDecorator(object):
    @classmethod
    def connect(cls, database=None, document=None):
        def decorator(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                conn = Connector.connect("mongo")

                return func(*args, **kwargs)

            return wrapper

        return decorator

    @classmethod
    def select_one(cls, database=None, document=None):
        def decorator(func):
            @wraps(func)
Exemplo n.º 2
0
import traceback

from utils.common import logging

from pymongo import MongoClient
from config import MONGO_CONNECT

logger = logging.getLogger("{}.MongoDB".format(__name__))


class MongoDB(object):
    conn = None

    @classmethod
    def connect(cls, *args, **kwargs):
        cls.conn = None
        try:
            cls.conn = MongoClient(MONGO_CONNECT, maxPoolSize=10)

            logger.debug(cls.conn.server_info())
            logger.info("MongoDB connected.")
        except Exception as e:
            logger.error(traceback.format_exc())
        finally:
            return cls.conn

    @classmethod
    def close(cls):
        if cls.conn is not None:
            cls.conn.close()
        logger.info("MongoDB disconnected.")
Exemplo n.º 3
0
import traceback

from functools import wraps
from flask import request
from marshmallow import ValidationError

from utils.common.logging import getLogger, DEBUG

logger = getLogger(__name__, level=DEBUG)


class SchemaDecorator(object):
    def __init__(self, Schema):
        self.Schema = Schema

    def load(self, data):
        return self.Schema().load(data)


class ParamsDecorator(SchemaDecorator):
    @classmethod
    def args(cls, Schema):
        def decorator(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                try:
                    schema = Schema().load(dict(request.args))
                    return func(schema, *args, **kwargs)
                except ValidationError as err:
                    logger.error(traceback.format_exc())
                    return (err.messages, 422)
Exemplo n.º 4
0
import os

from flask import Flask, render_template, jsonify
from flask_cors import CORS

from config import FLASK_CONFIG
from utils.common import logging
from utils.common.connectors import Connector

logger = logging.getLogger(__file__)

app = None


def createApp(env="dev"):
    global app

    app = Flask(__name__,
                root_path="",
                static_folder="public",
                template_folder="public")
    app.config.from_pyfile(FLASK_CONFIG.format(env=env))

    if "CORS" in app.config:
        CORS(app=app, resources=app.config["CORS"])

    Connector.connect("mongo")

    load_routes(app)

    return app
Exemplo n.º 5
0
from utils.common import logging
from utils.common.connectors.mongo import MongoDB

logger = logging.getLogger("{}.Connector".format(__name__))


class Connector(object):
    database = None
    conn = None

    @classmethod
    def connect(cls, dbms, *args, **kwargs):
        if dbms == "mongo" and cls.database != MongoDB:
            cls.database = MongoDB
            cls.conn = cls.database.connect()
        elif dbms == "others":
            pass

        return cls.conn

    @classmethod
    def close(cls):
        MongoDB.close()
        if (cls.database is not None):
            cls.database.close()
        if (cls.conn is not None):
            cls.conn.close()
        logger.info("MongoDB disconnected.")

    @classmethod
    def clear(cls):