__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.plugins
  4. ~~~~~~~~~~~~~~~
  5. This module contains the Plugin class used by all Plugins for FlaskBB.
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import warnings
  10. from flask import current_app
  11. from flask_plugins import Plugin
  12. from flaskbb.management.models import SettingsGroup
  13. class FlaskBBPluginDeprecationWarning(DeprecationWarning):
  14. pass
  15. warnings.simplefilter("always", FlaskBBPluginDeprecationWarning)
  16. class FlaskBBPlugin(Plugin):
  17. #: This is the :class:`SettingsGroup` key - if your the plugin needs to
  18. #: install additional things you must set it, else it won't install
  19. #: anything.
  20. settings_key = None
  21. @property
  22. def has_settings(self):
  23. """Is ``True`` if the Plugin **can** be installed."""
  24. if self.settings_key is not None:
  25. return True
  26. return False
  27. @property
  28. def installed(self):
  29. is_installed = False
  30. if self.has_settings:
  31. group = SettingsGroup.query.\
  32. filter_by(key=self.settings_key).\
  33. first()
  34. is_installed = group and len(group.settings.all()) > 0
  35. return is_installed
  36. @property
  37. def uninstallable(self):
  38. """Is ``True`` if the Plugin **can** be uninstalled."""
  39. warnings.warn(
  40. "self.uninstallable is deprecated. Use self.installed instead.",
  41. FlaskBBPluginDeprecationWarning
  42. )
  43. return self.installed
  44. @property
  45. def installable(self):
  46. warnings.warn(
  47. "self.installable is deprecated. Use self.has_settings instead.",
  48. FlaskBBPluginDeprecationWarning
  49. )
  50. return self.has_settings
  51. # Some helpers
  52. def register_blueprint(self, blueprint, **kwargs):
  53. """Registers a blueprint.
  54. :param blueprint: The blueprint which should be registered.
  55. """
  56. current_app.register_blueprint(blueprint, **kwargs)