Beispiel #1
0
def configReset(path=os.getcwd()):
    """
    reset the configuration system
    """
    app_key = saltKey()
    app_config = ini_configuration(path).file_config

    # alembic configuration
    app_config.set('alembic', 'version_locations', '%(here)s/dbs/versions')
    app_config.set('alembic', 'script_location', os.path.join(path, 'scripts'))
    app_config.set('alembic', 'output_encoding', 'utf-8')

    # application configuration
    app_config.add_section('app')
    app_config.set('app', 'key', app_key)
    app_config.set('app', 'name', 'Metric')
    app_config.set('app', 'logs', '%(here)s/logs')

    # auth configuration
    app_config.add_section('auth')
    app_config.set('auth', 'expiry_time', '180')

    # uWSGI configuration
    app_config.add_section('uwsgi')
    app_config.set('uwsgi', 'module', 'app.wsgi')

    with open(os.path.join(path, 'config.ini'), 'w') as f:
        app_config.write(f)
        f.close()
Beispiel #2
0
def up_version(target='head', sql_mode=False):
    """ Upgrade version of migration available

    :@param target: parameter target of version migration

    :@param sql_mode: a toggle to show sql_mode, some kind like verbose on/off
    """
    CABIN.info('Upgrade migration!')
    return upgrade(ini_configuration(os.getcwd()), target, sql=sql_mode)
Beispiel #3
0
    def initialize(self, project_name):
        """
        ____This function is serve as initialize installer, and run through interface command line____

        @param project_name: string, name for your project either name or just . for current directory
        """
        if project_name != '.':
            createDirectory(os.path.join(os.getcwd(), project_name))
        self.project = os.path.join(os.getcwd(), project_name)

        init(ini_configuration(self.project), self.project)

        self._buildDirectory()
        self._fileToManage()

        configReset(self.project)
Beispiel #4
0
class Cabin:
    _config = ini_configuration(ROOTPATH)
    _now = datetime.now().strftime('%Y/%m/%d %H:%M:%S')

    def __init__(self):
        try:
            logging.basicConfig(filename=self._config.get_section_option(
                'app', 'logs'),
                                level=logging.INFO)
        except Exception:
            logging.basicConfig(level=logging.INFO)

    def info(self, info):
        logging.info(f'{self._now}: {info}')

    def warning(self, warning):
        logging.warning(f'{self._now}: {warning}')

    def error(self, error):
        logging.error(f'{self._now}: {error}')
Beispiel #5
0
def session(connection=False):
    '''
    session(conection=bool(True/False))
    ---
    ____fungsi ini digunakan untuk system melakukan koneksi sesi ke database dengan menggunakan
    sesi yang ada pada fungsi sqlalchemy, dan konfigurasi .ini menggunakan alembic konfigurasi.
    parameter connection berfungsi sebagai penanda dari sesi koneksi dalam boolean, jika "True"
    maka koneksi akan digunakan untuk membuat engine, jika tidak koneksi digunakan untuk mengikat
    sesi pada database____
    ---
    '''
    config = ini_configuration()
    try:
        url = config.get_main_option('sqlalchemy.url')
    except CommandError as e:
        raise e
    else:
        if not connection:
            session_ng = sessionmaker(bind=create_engine(url),
                                      expire_on_commit=False)
            return session_ng()
        else:
            return create_engine(url).begin()
Beispiel #6
0
def migration(message):
    """ Function will create a new revision migration from alembic

    :@param message: a parameter to describe your migration name and message.
    """
    return revision(ini_configuration(os.getcwd()), message=message)
Beispiel #7
0
def down_version(target='head', sql_mode=False):
    CABIN.info('Downgrade migration!')
    return downgrade(ini_configuration(os.getcwd()), target, sql=sql_mode)
Beispiel #8
0
class Auth:
    """
    Auth class extension for models
    
    - auth_identification is attribute for models to defined their identification field
    
    - attempt to attempt create authentication by entering the identification and password
    """
    auth_identification = 'username'
    _config = ini_configuration(ROOTPATH)

    @classmethod
    def attempt(cls, **kwargs):
        """
        attempt to auth the user from database
        """
        identification = kwargs[cls.auth_identification]
        password = kwargs["password"].encode("utf-8")

        user_check = cls().select().filter(cls.auth_identification,
                                           identification).first()
        user_check = user_check.result()

        try:
            if bcrypt.checkpw(password, user_check.password.encode("utf-8")):
                return {"identity": user_check.id}
            else:
                return False

        except AttributeError:
            return None

    def createToken(self, identity):
        """
        create access and refresh token
        """
        minutes = 180

        try:
            minutes = self._config.get_section_option('auth', 'expiry_time')
        except Exception:
            pass
        finally:
            expired = datetime.timedelta(minutes=minutes)

        return {
            'token_access':
            create_access_token(identity=identity, expires_delta=expired),
            'token_refresh':
            create_refresh_token(identity=identity)
        }

    def tokenVerify(self):
        """
        function to verify the authentication token if verified 
        @return:
        """
        if verify_jwt_in_request():
            return get_jwt_claims()
        else:
            return False