Пример #1
0
def plugin_load():
    from pytsite import reg, util
    from plugins import auth, odm
    from . import _driver

    # ODM models
    role_cls = reg.get('auth_storage_odm.role_odm_class', 'plugins.auth_storage_odm.ODMRole')
    user_cls = reg.get('auth_storage_odm.user_odm_class', 'plugins.auth_storage_odm.ODMUser')
    odm.register_model('role', util.get_module_attr(role_cls))
    odm.register_model('user', util.get_module_attr(user_cls))
    odm.register_model('follower', ODMFollower)
    odm.register_model('blocked_user', ODMBlockedUser)

    # Register storage driver
    auth.register_storage_driver(_driver.Storage())
Пример #2
0
def register_model(model: str, cls: Union[str, ContentModelClass], title: str, menu_weight: int = 0,
                   menu_icon: str = 'fa fa-file-text-o', menu_sid: str = 'content', replace: bool = False):
    """Register content model
    """
    # Resolve class
    if isinstance(cls, str):
        cls = util.get_module_attr(cls)  # type: ContentModelClass

    if not issubclass(cls, Content):
        raise TypeError('Subclass of {} expected, got {}'.format(Content, type(cls)))

    if not replace and is_model_registered(model):
        raise KeyError("Content model '{}' is already registered".format(model))

    # Register ODM model
    odm.register_model(model, cls, replace)

    # Saving info about registered _content_ model
    _models[model] = (cls, title)

    if reg.get('env.type') == 'wsgi':
        mock = dispense(model)
        perms = ['odm_auth@{}.{}'.format(p, model) for p in mock.odm_auth_permissions()],

        admin.sidebar.add_menu(
            sid=menu_sid,
            mid=model,
            title=title,
            path=router.rule_path('odm_ui@admin_browse', {'model': model}),
            icon=menu_icon,
            weight=menu_weight,
            permissions=perms,
            replace=replace,
        )
Пример #3
0
def register_model(model: str,
                   cls,
                   menu_title: str = None,
                   menu_weight: int = 0,
                   menu_icon: str = 'fa fas fa-tags',
                   menu_sid: str = 'taxonomy',
                   menu_roles: Union[str, list, tuple] = ('admin', 'dev'),
                   menu_permissions: Union[str, list, tuple] = None):
    """Register a taxonomy model
    """
    if model in _models:
        raise RuntimeError(
            "Taxonomy model '{}' is already registered".format(model))

    if isinstance(cls, str):
        cls = util.get_module_attr(cls)

    if not issubclass(cls, Term):
        raise TypeError('Subclass of {} expected'.format(Term))

    odm.register_model(model, cls)
    _models.append(model)

    if reg.get('env.type') == 'wsgi' and menu_title:
        menu_url = router.rule_path('odm_ui@admin_browse', {'model': model})
        admin.sidebar.add_menu(
            menu_sid,
            model,
            menu_title,
            menu_url,
            menu_icon,
            weight=menu_weight,
            roles=menu_roles,
            permissions=menu_permissions,
        )
Пример #4
0
    def __init__(self):
        self._role_cls = util.get_module_attr(
            reg.get(_REG_ROLE_CLS, 'plugins.auth_storage_odm.Role'))
        if not issubclass(self._role_cls, _model.Role):
            raise TypeError(
                "Subclass of {} expected, got {}. Please check the '{}' configuration parameter"
                .format(auth.AbstractRole, type(self._role_cls),
                        _REG_ROLE_CLS))

        self._user_cls = util.get_module_attr(
            reg.get(_REG_USER_CLS, 'plugins.auth_storage_odm.User'))
        if not issubclass(self._user_cls, _model.User):
            raise TypeError(
                "Subclass of {} expected, got {}. Please check the '{}' configuration parameter"
                .format(auth.AbstractUser, type(self._user_cls),
                        _REG_USER_CLS))
Пример #5
0
def register_model(model: str, cls: Union[str, Type[_model.Entity]], replace: bool = False):
    """Register a new ODM model
    """
    if isinstance(cls, str):
        cls = util.get_module_attr(cls)  # type: Type[_model.Entity]

    if not issubclass(cls, _model.Entity):
        raise TypeError("Unable to register model '{}': subclass of odm.model.Entity expected."
                        .format(model))

    if is_model_registered(model) and not replace:
        raise _error.ModelAlreadyRegistered(model)

    # Create finder cache pool for each newly registered model
    if not replace:
        cache.create_pool('odm.finder.' + model)

    _MODEL_TO_CLASS[model] = cls

    cls.on_register(model)
    events.fire('*****@*****.**', model=model, cls=cls, replace=replace)

    mock = dispense(model)

    # Save model's collection name
    _MODEL_TO_COLLECTION[model] = mock.collection
    _COLLECTION_NAME_TO_MODEL[mock.collection.name] = model

    # Automatically create indices on new collections
    if mock.collection.name not in mongodb.get_collection_names():
        mock.create_indexes()
Пример #6
0
    def __init__(self, handler: str, *args, **kwargs):
        if isinstance(handler, str):
            handler = _util.get_module_attr(handler)

        self._handler = handler
        self._args = args
        self._kwargs = kwargs
Пример #7
0
def get_driver() -> _driver.Abstract:
    global _current_driver
    if not _current_driver:
        driver_class = _util.get_module_attr(
            _reg.get('file.driver', 'plugins.file_storage_odm.Driver'))
        driver = driver_class()

        if not isinstance(driver, _driver.Abstract):
            raise TypeError('Invalid driver instance')

        _current_driver = driver

    return _current_driver
Пример #8
0
def register_model(model: str,
                   cls,
                   admin_menu_title: str = None,
                   admin_menu_weight: int = 0,
                   admin_menu_icon: str = 'fa fas fa-bars',
                   admin_menu_sid: str = 'menu',
                   admin_menu_roles: Union[str, list,
                                           tuple] = ('admin', 'dev'),
                   admin_menu_permissions: Union[str, list, tuple] = None):
    """Register a menu ODM model
    """
    if isinstance(cls, str):
        cls = util.get_module_attr(cls)

    if not issubclass(cls, _model.Menu):
        raise TypeError('Subclass of {} expected'.format(_model.Menu))

    taxonomy.register_model(model, cls, admin_menu_title, admin_menu_weight,
                            admin_menu_icon, admin_menu_sid, admin_menu_roles,
                            admin_menu_permissions)
Пример #9
0
def dispense(request: _http.Request, uid: str) -> _form.Form:
    """Dispense a form
    """
    try:
        # Determine form's class
        cid = uid.replace('cid:', '') if uid.startswith(
            'cid:') else _cache.get_pool('form.form_cid').get(uid)
        cls = _util.get_module_attr(cid)

        # Prevent instantiating other classes via HTTP API
        if not issubclass(cls, _form.Form):
            raise RuntimeError('Form class is not found')

        # Instantiate form
        return cls(request) if uid.startswith('cid:') else cls(request,
                                                               _uid=uid)

    except _cache.error.KeyNotExist:
        raise RuntimeError('Invalid form UID')

    # Hide all other exceptions info from outer world
    except Exception as e:
        _logger.error(e)
        raise RuntimeError('Unexpected form exception')
Пример #10
0
def user_form(request: http.Request = None, user_uid: str = None) -> form.Form:
    """Get user edit form
    """
    form_cls = util.get_module_attr(reg.get('auth_ui.user_form_class', 'plugins.auth_ui._frm.User'))

    return form_cls(request or router.request(), user_uid=user_uid)
Пример #11
0
def role_form(request: http.Request = None, role_uid: str = None) -> form.Form:
    """Get role edit form
    """
    form_cls = util.get_module_attr(reg.get('auth_ui.role_form_class', 'plugins.auth_ui._frm.Role'))

    return form_cls(request or router.request(), role_uid=role_uid)