__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from flask.ext.plugins import Plugin
  2. from flask import current_app
  3. from flaskbb.management.models import SettingsGroup
  4. class FlaskBBPlugin(Plugin):
  5. #: This is the :class:`SettingsGroup` key - if your the plugin needs to
  6. #: install additional things you must set it, else it won't install
  7. #: anything.
  8. settings_key = None
  9. @property
  10. def installable(self):
  11. """Is ``True`` if the Plugin can be installed."""
  12. if self.settings_key is not None:
  13. return True
  14. return False
  15. @property
  16. def uninstallable(self):
  17. """Is ``True`` if the Plugin can be uninstalled."""
  18. if self.installable:
  19. group = SettingsGroup.query.filter_by(key=self.settings_key).first()
  20. if group and len(group.settings.all()) > 0:
  21. return True
  22. return False
  23. return False
  24. # Some helpers
  25. def register_blueprint(self, blueprint, **kwargs):
  26. """Registers a blueprint.
  27. :param blueprint: The blueprint which should be registered.
  28. """
  29. current_app.register_blueprint(blueprint, **kwargs)
  30. def create_table(self, model, db):
  31. """Creates the relation for the model
  32. :param model: The Model which should be created
  33. :param db: The database instance.
  34. """
  35. if not model.__table__.exists(bind=db.engine):
  36. model.__table__.create(bind=db.engine)
  37. def drop_table(self, model, db):
  38. """Drops the relation for the bounded model.
  39. :param model: The model on which the table is bound.
  40. :param db: The database instance.
  41. """
  42. model.__table__.drop(bind=db.engine)
  43. def create_all_tables(self, models, db):
  44. """A interface for creating all models specified in ``models``.
  45. :param models: A list with models
  46. :param db: The database instance
  47. """
  48. for model in models:
  49. self.create_table(model, db)
  50. def drop_all_tables(self, models, db):
  51. """A interface for dropping all models specified in the
  52. variable ``models``.
  53. :param models: A list with models
  54. :param db: The database instance.
  55. """
  56. for model in models:
  57. self.drop_table(model, db)