__init__.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.plugins
  4. ~~~~~~~~~~~~~~~
  5. The Plugin class that every plugin should implement
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. class PluginError(Exception):
  10. """Error class for all plugin errors."""
  11. pass
  12. class Plugin(object):
  13. """Every plugin should implement this class. It handles the registration
  14. for the plugin hooks, creates or modifies additional relations or
  15. registers plugin specific thinks"""
  16. #: If you want create your tables with one command, put your models
  17. #: in here.
  18. models = []
  19. #: The name of the plugin
  20. #: E.g. "Example Plugin"
  21. name = None
  22. #: A small description of the plugin
  23. description = None
  24. def install(self, app=None):
  25. """The plugin should specify here what needs to be installed.
  26. For example, create the database and register the hooks."""
  27. raise NotImplementedError
  28. def uninstall(self, app=None):
  29. """Uninstalls the plugin and deletes the things that
  30. the plugin has installed."""
  31. raise NotImplementedError
  32. def create_table(self, model, db):
  33. """Creates the table for the model
  34. :param model: The Model which should be created
  35. :param db: The database instance.
  36. """
  37. model.__table__.create(bind=db.engine)
  38. def drop_table(self, model, db):
  39. """Drops the table for the bounded model.
  40. :param model: The model on which the table is bound.
  41. :param db: The database instance.
  42. """
  43. model.__table__.drop(bind=db.engine)
  44. def create_all_tables(self, db):
  45. """A interface for creating all models specified in ``models``.
  46. If no models are specified in that variable it will abort
  47. with a exception.
  48. :param db: The database instance
  49. """
  50. if len(self.models) > 0:
  51. for model in self.models:
  52. self.create_table(model, db)
  53. else:
  54. raise PluginError("No models found in 'models'.")
  55. def drop_all_tables(self, db):
  56. """A interface for dropping all models specified in the
  57. variable ``models``. If no models are specified in that variable,
  58. it will abort with an exception.
  59. :param db: The database instance.
  60. """
  61. if len(self.models) > 0:
  62. for model in self.models:
  63. self.drop_table(model, db)
  64. else:
  65. raise PluginError("No models found in 'models'.")