base.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. from functools import reduce
  2. import logging
  3. from typing import Any, Callable, cast, Dict, List, Optional, Type, TYPE_CHECKING, Union
  4. from flask import Blueprint, current_app, Flask, url_for
  5. from sqlalchemy.orm.session import Session as SessionBase
  6. from . import __version__
  7. from .api.manager import OpenApiManager
  8. from .babel.manager import BabelManager
  9. from .const import (
  10. LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
  11. LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW,
  12. LOGMSG_ERR_FAB_ADDON_IMPORT,
  13. LOGMSG_ERR_FAB_ADDON_PROCESS,
  14. LOGMSG_INF_FAB_ADD_VIEW,
  15. LOGMSG_INF_FAB_ADDON_ADDED,
  16. LOGMSG_WAR_FAB_VIEW_EXISTS,
  17. )
  18. from .filters import TemplateFilters
  19. from .menu import Menu, MenuApiManager
  20. from .views import IndexView, UtilView
  21. if TYPE_CHECKING:
  22. from flask_appbuilder.basemanager import BaseManager
  23. from flask_appbuilder.baseviews import BaseView, AbstractViewApi
  24. from flask_appbuilder.security.manager import BaseSecurityManager
  25. log = logging.getLogger(__name__)
  26. DynamicImportType = Union[
  27. Type["BaseManager"],
  28. Type["BaseView"],
  29. Type["BaseSecurityManager"],
  30. Type[Menu],
  31. Type["AbstractViewApi"],
  32. ]
  33. def dynamic_class_import(class_path: str) -> Optional[DynamicImportType]:
  34. """
  35. Will dynamically import a class from a string path
  36. :param class_path: string with class path
  37. :return: class
  38. """
  39. # Split first occurrence of path
  40. try:
  41. tmp = class_path.split(".")
  42. module_path = ".".join(tmp[0:-1])
  43. package = __import__(module_path)
  44. return reduce(getattr, tmp[1:], package)
  45. except Exception as e:
  46. log.exception(e)
  47. log.error(LOGMSG_ERR_FAB_ADDON_IMPORT, class_path, e)
  48. return None
  49. class AppBuilder:
  50. """
  51. This is the base class for all the framework.
  52. This is where you will register all your views and create the menu structure.
  53. Will hold your flask app object, all your views, and security classes.
  54. initialize your application like this for SQLAlchemy::
  55. from flask import Flask
  56. from flask_appbuilder import SQLA, AppBuilder
  57. app = Flask(__name__)
  58. app.config.from_object('config')
  59. db = SQLA(app)
  60. appbuilder = AppBuilder(app, db.session)
  61. When using MongoEngine::
  62. from flask import Flask
  63. from flask_appbuilder import AppBuilder
  64. from flask_appbuilder.security.mongoengine.manager import SecurityManager
  65. from flask_mongoengine import MongoEngine
  66. app = Flask(__name__)
  67. app.config.from_object('config')
  68. dbmongo = MongoEngine(app)
  69. appbuilder = AppBuilder(app, security_manager_class=SecurityManager)
  70. You can also create everything as an application factory.
  71. """
  72. security_manager_class = None
  73. template_filters = None
  74. def __init__(
  75. self,
  76. app: Optional[Flask] = None,
  77. session: Optional[SessionBase] = None,
  78. menu: Optional[Menu] = None,
  79. indexview: Optional[Type["AbstractViewApi"]] = None,
  80. base_template: str = "appbuilder/baselayout.html",
  81. static_folder: str = "static/appbuilder",
  82. static_url_path: str = "/appbuilder",
  83. security_manager_class: Optional[Type["BaseSecurityManager"]] = None,
  84. update_perms: bool = True,
  85. ) -> None:
  86. """
  87. AppBuilder init
  88. :param app:
  89. The flask app object
  90. :param session:
  91. The SQLAlchemy session object
  92. :param menu:
  93. optional, a previous contructed menu
  94. :param indexview:
  95. optional, your customized indexview
  96. :param static_folder:
  97. optional, your override for the global static folder
  98. :param static_url_path:
  99. optional, your override for the global static url path
  100. :param security_manager_class:
  101. optional, pass your own security manager class
  102. :param update_perms:
  103. optional, update permissions flag (Boolean) you can use
  104. FAB_UPDATE_PERMS config key also
  105. """
  106. self.baseviews: List[Union[Type["AbstractViewApi"], "AbstractViewApi"]] = []
  107. # temporary list that hold addon_managers config key
  108. self._addon_managers: List[str] = []
  109. # dict with addon name has key and instantiated class has value
  110. self.addon_managers: Dict[str, Any] = {}
  111. self.menu = menu
  112. self.base_template = base_template
  113. self.security_manager_class = security_manager_class
  114. self.indexview = indexview
  115. self.static_folder = static_folder
  116. self.static_url_path = static_url_path
  117. self.app = app
  118. self.update_perms = update_perms
  119. # Security Manager Class
  120. self.sm: BaseSecurityManager = None # type: ignore
  121. # Babel Manager Class
  122. self.bm: BabelManager = None # type: ignore
  123. self.openapi_manager: OpenApiManager = None # type: ignore
  124. self.menuapi_manager: MenuApiManager = None # type: ignore
  125. if app is not None:
  126. self.init_app(app, session)
  127. def init_app(self, app: Flask, session: SessionBase) -> None:
  128. """
  129. Will initialize the Flask app, supporting the app factory pattern.
  130. :param app:
  131. :param session: The SQLAlchemy session
  132. """
  133. app.config.setdefault("APP_NAME", "F.A.B.")
  134. app.config.setdefault("APP_THEME", "")
  135. app.config.setdefault("APP_ICON", "")
  136. app.config.setdefault("LANGUAGES", {"en": {"flag": "gb", "name": "English"}})
  137. app.config.setdefault("ADDON_MANAGERS", [])
  138. app.config.setdefault("RATELIMIT_ENABLED", False)
  139. app.config.setdefault("FAB_API_MAX_PAGE_SIZE", 100)
  140. app.config.setdefault("FAB_BASE_TEMPLATE", self.base_template)
  141. app.config.setdefault("FAB_STATIC_FOLDER", self.static_folder)
  142. app.config.setdefault("FAB_STATIC_URL_PATH", self.static_url_path)
  143. self.app = app
  144. self.base_template = app.config.get("FAB_BASE_TEMPLATE", self.base_template)
  145. self.static_folder = app.config.get("FAB_STATIC_FOLDER", self.static_folder)
  146. self.static_url_path = app.config.get(
  147. "FAB_STATIC_URL_PATH", self.static_url_path
  148. )
  149. _index_view = app.config.get("FAB_INDEX_VIEW", None)
  150. if _index_view:
  151. self.indexview = dynamic_class_import(_index_view) # type: ignore
  152. else:
  153. self.indexview = self.indexview or IndexView
  154. _menu = app.config.get("FAB_MENU", None)
  155. # Setup Menu
  156. if _menu is not None:
  157. menu = dynamic_class_import(_menu)
  158. if menu is not None and issubclass(menu, Menu):
  159. self.menu = menu()
  160. else:
  161. self.menu = self.menu or Menu()
  162. if self.update_perms: # default is True, if False takes precedence from config
  163. self.update_perms = app.config.get("FAB_UPDATE_PERMS", True)
  164. _security_manager_class_name = app.config.get(
  165. "FAB_SECURITY_MANAGER_CLASS", None
  166. )
  167. if _security_manager_class_name is not None:
  168. security_manager_class = dynamic_class_import(_security_manager_class_name)
  169. self.security_manager_class = cast(
  170. Type["BaseSecurityManager"], security_manager_class
  171. )
  172. if self.security_manager_class is None:
  173. from flask_appbuilder.security.sqla.manager import SecurityManager
  174. self.security_manager_class = SecurityManager
  175. self._addon_managers = app.config["ADDON_MANAGERS"]
  176. self.session = session
  177. self.sm = self.security_manager_class(self)
  178. self.bm = BabelManager(self)
  179. self.openapi_manager = OpenApiManager(self)
  180. self.menuapi_manager = MenuApiManager(self)
  181. self._add_global_static()
  182. self._add_global_filters()
  183. app.before_request(self.sm.before_request)
  184. self._add_admin_views()
  185. self._add_addon_views()
  186. if self.app:
  187. self._add_menu_permissions()
  188. else:
  189. self.post_init()
  190. self._init_extension(app)
  191. def _init_extension(self, app: Flask) -> None:
  192. app.appbuilder = self
  193. if not hasattr(app, "extensions"):
  194. app.extensions = {}
  195. app.extensions["appbuilder"] = self
  196. def post_init(self) -> None:
  197. for baseview in self.baseviews:
  198. # instantiate the views and add session
  199. baseview = self._check_and_init(baseview)
  200. # Register the views has blueprints
  201. if baseview.__class__.__name__ not in self.get_app.blueprints.keys():
  202. self.register_blueprint(baseview)
  203. # Add missing permissions where needed
  204. self.add_permissions()
  205. @property
  206. def get_app(self) -> Flask:
  207. """
  208. Get current or configured flask app
  209. :return: Flask App
  210. """
  211. if self.app:
  212. return self.app
  213. else:
  214. return current_app
  215. @property
  216. def get_session(self) -> SessionBase:
  217. """
  218. Get the current sqlalchemy session.
  219. :return: SQLAlchemy Session
  220. """
  221. return self.session
  222. @property
  223. def app_name(self) -> str:
  224. """
  225. Get the App name
  226. :return: String with app name
  227. """
  228. return self.get_app.config["APP_NAME"]
  229. @property
  230. def app_theme(self) -> str:
  231. """
  232. Get the App theme name
  233. :return: String app theme name
  234. """
  235. return self.get_app.config["APP_THEME"]
  236. @property
  237. def app_icon(self) -> str:
  238. """
  239. Get the App icon location
  240. :return: String with relative app icon location
  241. """
  242. return self.get_app.config["APP_ICON"]
  243. @property
  244. def languages(self) -> Dict[str, Any]:
  245. return self.get_app.config["LANGUAGES"]
  246. @property
  247. def version(self) -> str:
  248. """
  249. Get the current F.A.B. version
  250. :return: String with the current F.A.B. version
  251. """
  252. return __version__
  253. def _add_global_filters(self) -> None:
  254. self.template_filters = TemplateFilters(self.get_app, self.sm)
  255. def _add_global_static(self) -> None:
  256. bp = Blueprint(
  257. "appbuilder",
  258. __name__,
  259. url_prefix="/static",
  260. template_folder="templates",
  261. static_folder=self.static_folder,
  262. static_url_path=self.static_url_path,
  263. )
  264. self.get_app.register_blueprint(bp)
  265. def _add_admin_views(self) -> None:
  266. """
  267. Registers indexview, utilview (back function), babel views and Security views.
  268. """
  269. if self.indexview:
  270. self._indexview = self.add_view_no_menu(self.indexview)
  271. self.add_view_no_menu(UtilView)
  272. self.bm.register_views()
  273. self.sm.register_views()
  274. self.openapi_manager.register_views()
  275. self.menuapi_manager.register_views()
  276. def _add_addon_views(self) -> None:
  277. """
  278. Registers declared addon's
  279. """
  280. for addon in self._addon_managers:
  281. addon_class_ = dynamic_class_import(addon)
  282. addon_class = cast(Type["BaseManager"], addon_class_)
  283. if addon_class:
  284. # Instantiate manager with appbuilder (self)
  285. inst_addon_class: "BaseManager" = addon_class(self)
  286. try:
  287. inst_addon_class.pre_process()
  288. inst_addon_class.register_views()
  289. inst_addon_class.post_process()
  290. self.addon_managers[addon] = inst_addon_class
  291. log.info(LOGMSG_INF_FAB_ADDON_ADDED, addon)
  292. except Exception as e:
  293. log.exception(e)
  294. log.error(LOGMSG_ERR_FAB_ADDON_PROCESS, addon, e)
  295. def _check_and_init(
  296. self, baseview: Union[Type["AbstractViewApi"], "AbstractViewApi"]
  297. ) -> "AbstractViewApi":
  298. # If class if not instantiated, instantiate it
  299. # and add db session from security models.
  300. if hasattr(baseview, "datamodel"):
  301. if getattr(baseview, "datamodel").session is None:
  302. getattr(baseview, "datamodel").session = self.session
  303. if isinstance(baseview, type):
  304. baseview = baseview()
  305. return baseview
  306. def add_view(
  307. self,
  308. baseview: Union[Type["AbstractViewApi"], "AbstractViewApi"],
  309. name: str,
  310. href: str = "",
  311. icon: str = "",
  312. label: str = "",
  313. category: str = "",
  314. category_icon: str = "",
  315. category_label: str = "",
  316. menu_cond: Optional[Callable[..., bool]] = None,
  317. ) -> "AbstractViewApi":
  318. """
  319. Add your views associated with menus using this method.
  320. :param baseview:
  321. A BaseView type class instantiated or not.
  322. This method will instantiate the class for you if needed.
  323. :param name:
  324. The string name that identifies the menu.
  325. :param href:
  326. Override the generated href for the menu.
  327. You can use an url string or an endpoint name
  328. if non provided default_view from view will be set as href.
  329. :param icon:
  330. Font-Awesome icon name, optional.
  331. :param label:
  332. The label that will be displayed on the menu,
  333. if absent param name will be used
  334. :param category:
  335. The menu category where the menu will be included,
  336. if non provided the view will be acessible as a top menu.
  337. :param category_icon:
  338. Font-Awesome icon name for the category, optional.
  339. :param category_label:
  340. The label that will be displayed on the menu,
  341. if absent param name will be used
  342. :param menu_cond:
  343. If a callable, :code:`menu_cond` will be invoked when
  344. constructing the menu items. If it returns :code:`True`,
  345. then this link will be a part of the menu. Otherwise, it
  346. will not be included in the menu items. Defaults to
  347. :code:`None`, meaning the item will always be present.
  348. Examples::
  349. appbuilder = AppBuilder(app, db)
  350. # Register a view, rendering a top menu without icon.
  351. appbuilder.add_view(MyModelView(), "My View")
  352. # or not instantiated
  353. appbuilder.add_view(MyModelView, "My View")
  354. # Register a view, a submenu "Other View" from "Other" with a phone icon.
  355. appbuilder.add_view(
  356. MyOtherModelView,
  357. "Other View",
  358. icon='fa-phone',
  359. category="Others"
  360. )
  361. # Register a view, with category icon and translation.
  362. appbuilder.add_view(
  363. YetOtherModelView,
  364. "Other View",
  365. icon='fa-phone',
  366. label=_('Other View'),
  367. category="Others",
  368. category_icon='fa-envelop',
  369. category_label=_('Other View')
  370. )
  371. # Register a view whose menu item will be conditionally displayed
  372. appbuilder.add_view(
  373. YourFeatureView,
  374. "Your Feature",
  375. icon='fa-feature',
  376. label=_('Your Feature'),
  377. menu_cond=lambda: is_feature_enabled("your-feature"),
  378. )
  379. # Add a link
  380. appbuilder.add_link("google", href="www.google.com", icon = "fa-google-plus")
  381. """
  382. baseview = self._check_and_init(baseview)
  383. log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, name)
  384. if not self._view_exists(baseview):
  385. baseview.appbuilder = self
  386. self.baseviews.append(baseview)
  387. self._process_inner_views()
  388. if self.app:
  389. self.register_blueprint(baseview)
  390. self._add_permission(baseview)
  391. self.add_limits(baseview)
  392. self.add_link(
  393. name=name,
  394. href=href,
  395. icon=icon,
  396. label=label,
  397. category=category,
  398. category_icon=category_icon,
  399. category_label=category_label,
  400. baseview=baseview,
  401. cond=menu_cond,
  402. )
  403. return baseview
  404. def add_link(
  405. self,
  406. name: str,
  407. href: str,
  408. icon: str = "",
  409. label: str = "",
  410. category: str = "",
  411. category_icon: str = "",
  412. category_label: str = "",
  413. baseview: Optional["AbstractViewApi"] = None,
  414. cond: Optional[Callable[..., bool]] = None,
  415. ) -> None:
  416. """
  417. Add your own links to menu using this method
  418. :param baseview:
  419. :param name:
  420. The string name that identifies the menu.
  421. :param href:
  422. Override the generated href for the menu.
  423. You can use an url string or an endpoint name
  424. :param icon:
  425. Font-Awesome icon name, optional.
  426. :param label:
  427. The label that will be displayed on the menu,
  428. if absent param name will be used
  429. :param category:
  430. The menu category where the menu will be included,
  431. if non provided the view will be accessible as a top menu.
  432. :param category_icon:
  433. Font-Awesome icon name for the category, optional.
  434. :param category_label:
  435. The label that will be displayed on the menu,
  436. if absent param name will be used
  437. :param cond:
  438. If a callable, :code:`cond` will be invoked when
  439. constructing the menu items. If it returns :code:`True`,
  440. then this link will be a part of the menu. Otherwise, it
  441. will not be included in the menu items. Defaults to
  442. :code:`None`, meaning the item will always be present.
  443. """
  444. if self.menu is None:
  445. return
  446. self.menu.add_link(
  447. name=name,
  448. href=href,
  449. icon=icon,
  450. label=label,
  451. category=category,
  452. category_icon=category_icon,
  453. category_label=category_label,
  454. baseview=baseview,
  455. cond=cond,
  456. )
  457. if self.app:
  458. self._add_permissions_menu(name)
  459. if category:
  460. self._add_permissions_menu(category)
  461. def add_separator(
  462. self, category: str, cond: Optional[Callable[..., bool]] = None
  463. ) -> None:
  464. """
  465. Add a separator to the menu, you will sequentially create the menu
  466. :param category:
  467. The menu category where the separator will be included.
  468. :param cond:
  469. If a callable, :code:`cond` will be invoked when
  470. constructing the menu items. If it returns :code:`True`,
  471. then this separator will be a part of the menu. Otherwise,
  472. it will not be included in the menu items. Defaults to
  473. :code:`None`, meaning the separator will always be present.
  474. """
  475. if self.menu is None:
  476. return
  477. self.menu.add_separator(category, cond=cond)
  478. def add_view_no_menu(
  479. self,
  480. baseview: Union[Type["AbstractViewApi"], "AbstractViewApi"],
  481. endpoint: Optional[str] = None,
  482. static_folder: Optional[str] = None,
  483. ) -> "AbstractViewApi":
  484. """
  485. Add your views without creating a menu.
  486. :param baseview:
  487. A BaseView type class instantiated.
  488. :param endpoint: The endpoint path for the Flask blueprint
  489. :param static_folder: The static folder for the Flask blueprint
  490. """
  491. baseview = self._check_and_init(baseview)
  492. log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, "")
  493. if not self._view_exists(baseview):
  494. baseview.appbuilder = self
  495. self.baseviews.append(baseview)
  496. self._process_inner_views()
  497. if self.app:
  498. self.register_blueprint(
  499. baseview, endpoint=endpoint, static_folder=static_folder
  500. )
  501. self._add_permission(baseview)
  502. self.add_limits(baseview)
  503. else:
  504. log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS, baseview.__class__.__name__)
  505. return baseview
  506. def add_api(self, baseview: Type["AbstractViewApi"]) -> "AbstractViewApi":
  507. """
  508. Add a BaseApi class or child to AppBuilder
  509. :param baseview: A BaseApi type class
  510. :return: The instantiated base view
  511. """
  512. return self.add_view_no_menu(baseview)
  513. def security_cleanup(self) -> None:
  514. """
  515. This method is useful if you have changed
  516. the name of your menus or classes,
  517. changing them will leave behind permissions
  518. that are not associated with anything.
  519. You can use it always or just sometimes to
  520. perform a security cleanup. Warning this will delete any permission
  521. that is no longer part of any registered view or menu.
  522. Remember invoke ONLY AFTER YOU HAVE REGISTERED ALL VIEWS
  523. """
  524. self.sm.security_cleanup(self.baseviews, self.menu)
  525. def security_converge(self, dry: bool = False) -> Dict[str, Any]:
  526. """
  527. This method is useful when you use:
  528. - `class_permission_name`
  529. - `previous_class_permission_name`
  530. - `method_permission_name`
  531. - `previous_method_permission_name`
  532. migrates all permissions to the new names on all the Roles
  533. :param dry: If True will not change DB
  534. :return: Dict with all computed necessary operations
  535. """
  536. if self.menu is None:
  537. return {}
  538. return self.sm.security_converge(self.baseviews, self.menu.menu, dry)
  539. def get_url_for_login_with(self, next_url: str = None) -> str:
  540. if self.sm.auth_view is None:
  541. return ""
  542. return url_for("%s.%s" % (self.sm.auth_view.endpoint, "login"), next=next_url)
  543. @property
  544. def get_url_for_login(self) -> str:
  545. if self.sm.auth_view is None:
  546. return ""
  547. return url_for("%s.%s" % (self.sm.auth_view.endpoint, "login"))
  548. @property
  549. def get_url_for_logout(self) -> str:
  550. if self.sm.auth_view is None:
  551. return ""
  552. return url_for("%s.%s" % (self.sm.auth_view.endpoint, "logout"))
  553. @property
  554. def get_url_for_index(self) -> str:
  555. if self._indexview is None:
  556. return ""
  557. return url_for(
  558. "%s.%s" % (self._indexview.endpoint, self._indexview.default_view)
  559. )
  560. @property
  561. def get_url_for_userinfo(self) -> str:
  562. if self.sm.user_view is None:
  563. return ""
  564. return url_for("%s.%s" % (self.sm.user_view.endpoint, "userinfo"))
  565. def get_url_for_locale(self, lang: str) -> str:
  566. if self.bm.locale_view is None:
  567. return ""
  568. return url_for(
  569. "%s.%s" % (self.bm.locale_view.endpoint, self.bm.locale_view.default_view),
  570. locale=lang,
  571. )
  572. def add_limits(self, baseview: "AbstractViewApi") -> None:
  573. if hasattr(baseview, "limits"):
  574. self.sm.add_limit_view(baseview)
  575. def add_permissions(self, update_perms: bool = False) -> None:
  576. from flask_appbuilder.baseviews import AbstractViewApi
  577. if self.update_perms or update_perms:
  578. for baseview in self.baseviews:
  579. baseview = cast(AbstractViewApi, baseview)
  580. self._add_permission(baseview, update_perms=update_perms)
  581. self._add_menu_permissions(update_perms=update_perms)
  582. def _add_permission(
  583. self, baseview: "AbstractViewApi", update_perms: bool = False
  584. ) -> None:
  585. if self.update_perms or update_perms:
  586. try:
  587. self.sm.add_permissions_view(
  588. baseview.base_permissions, baseview.class_permission_name
  589. )
  590. except Exception as e:
  591. log.exception(e)
  592. log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW, e)
  593. def _add_permissions_menu(self, name: str, update_perms: bool = False) -> None:
  594. if self.update_perms or update_perms:
  595. try:
  596. self.sm.add_permissions_menu(name)
  597. except Exception as e:
  598. log.exception(e)
  599. log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU, e)
  600. def _add_menu_permissions(self, update_perms: bool = False) -> None:
  601. if self.menu is None:
  602. return
  603. if self.update_perms or update_perms:
  604. for category in self.menu.get_list():
  605. self._add_permissions_menu(category.name, update_perms=update_perms)
  606. for item in category.childs:
  607. # don't add permission for menu separator
  608. if item.name != "-":
  609. self._add_permissions_menu(item.name, update_perms=update_perms)
  610. def register_blueprint(
  611. self,
  612. baseview: "AbstractViewApi",
  613. endpoint: Optional[str] = None,
  614. static_folder: Optional[str] = None,
  615. ) -> None:
  616. self.get_app.register_blueprint(
  617. baseview.create_blueprint(
  618. self, endpoint=endpoint, static_folder=static_folder
  619. )
  620. )
  621. def _view_exists(self, view: "AbstractViewApi") -> bool:
  622. for baseview in self.baseviews:
  623. if baseview.__class__ == view.__class__:
  624. return True
  625. return False
  626. def _process_inner_views(self) -> None:
  627. from flask_appbuilder.baseviews import AbstractViewApi
  628. for view in self.baseviews:
  629. view = cast(AbstractViewApi, view)
  630. for inner_class in view.get_uninit_inner_views():
  631. for v in self.baseviews:
  632. if (
  633. isinstance(v, inner_class)
  634. and v not in view.get_init_inner_views()
  635. ):
  636. view.get_init_inner_views().append(v)