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, )
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, )
def define(uid: str, content: Type[_frm.Form], title: str, icon: str, roles: Union[str, list, tuple] = ('admin', 'dev'), permissions: Union[str, list, tuple] = None, weight: int = 0): """Define a setting """ if uid in _settings: raise KeyError("Setting '{}' is already defined".format(uid)) _settings[uid] = { 'title': title, 'content': content, } path = router.rule_path('settings@get_form', {'uid': uid}) admin.sidebar.add_menu('settings', uid, title, path, icon, weight=weight, roles=roles, permissions=permissions)
def plugin_load_wsgi(): from pytsite import router from plugins import auth_ui, settings, admin, http_api from . import _frm, _controllers, _http_api_controllers # Settings form settings.define('auth', _frm.Settings, 'auth_admin@security', 'fa fa-user', 'dev') # BAse path for route rules abp = admin.base_path() + '/auth' # HTTP API rules http_api.handle('GET', abp + '/browse/<e_type>', _http_api_controllers.GetBrowserRows, 'auth_admin@browser_rows') # Admin routes flt = auth_ui.AuthFilter router.handle(_controllers.Browser, abp + '/browse/role', 'auth_admin@browse_roles', {'e_type': 'role'}, filters=flt) router.handle(_controllers.Browser, abp + '/browse/user', 'auth_admin@browse_users', {'e_type': 'user'}, filters=flt) router.handle(_controllers.ModifyForm, abp + '/modify/role/<uid>', 'auth_admin@form_role_modify', {'e_type': 'role'}, filters=flt) router.handle(_controllers.ModifyForm, abp + '/modify/user/<uid>', 'auth_admin@form_user_modify', {'e_type': 'user'}, filters=flt) router.handle(_controllers.DeleteForm, abp + '/delete/role', 'auth_admin@form_role_delete', {'e_type': 'role'}, ('GET', 'POST'), flt) router.handle(_controllers.DeleteForm, abp + '/delete/user', 'auth_admin@form_user_delete', {'e_type': 'user'}, ('GET', 'POST'), flt) # 'Security' admin sidebar section admin.sidebar.add_section('security', 'auth_admin@security', 1000) # 'Users' admin sidebar menu admin.sidebar.add_menu('security', 'users', 'auth_admin@users', router.rule_path('auth_admin@browse_users'), 'fa fa-users', weight=10) # 'Roles' admin sidebar menu admin.sidebar.add_menu('security', 'roles', 'auth_admin@roles', router.rule_path('auth_admin@browse_roles'), 'fa fa-key', weight=20, roles='dev')
def plugin_load_uwsgi(): from pytsite import router, cron from plugins import admin from . import _eh # Cron tasks cron.every_min(_eh.cron_1min) # Sidebar menu m = 'content_import' admin.sidebar.add_menu(sid='content', mid=m, title=__name__ + '@import', path=router.rule_path('odm_ui@admin_browse', {'model': m}), icon='fa fa-download', permissions=('odm_auth@modify.' + m, 'odm_auth@modify_own.' + m), weight=110)
def _on_f_set(self, field_name: str, value, **kwargs): """Hook """ if field_name == 'route_alias' and isinstance(value, str): # Delegate string generation to dedicated hook route_alias_str = self.content_alter_route_alias_str(value.strip()) # Entity doesn't have attached route alias reference if not self.route_alias: # Route target cannot be built while entity is not saved, # so route alias needs to be saved temporary as a string if self.is_new: self.f_set('tmp_route_alias_str', value) value = None else: target = router.rule_path('content@view', {'model': self.model, 'eid': self.id}) value = route_alias.create(route_alias_str, target, self.language).save() else: # Existing route alias needs to be changed if self.route_alias.alias != route_alias_str: self.route_alias.f_set('alias', route_alias_str).save() value = self.route_alias return super()._on_f_set(field_name, value, **kwargs)