plugins.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. .. _plugins:
  2. Plugins
  3. =======
  4. .. module:: flaskbb.plugins
  5. FlaskBB provides an easy way to extend the functionality of your forum
  6. via so called `Plugins`. Plugins do not modify the `core` of FlaskBB, so
  7. you can easily activate and deactivate them anytime. This part of the
  8. documentation only covers the basic things for creating plugins. If you are
  9. looking for a tutorial you need to go to this section of the documentation:
  10. :doc:`plugin_tutorial/index`.
  11. Structure
  12. ---------
  13. A plugin has it's own folder where all the plugin specific files are living.
  14. For example, the structure of a plugin could look like this
  15. .. sourcecode:: text
  16. my_plugin
  17. |-- info.json Contains the Plugin's metadata
  18. |-- license.txt The full license text of your plugin
  19. |-- __init__.py The plugin's main class is located here
  20. |-- views.py
  21. |-- models.py
  22. |-- forms.py
  23. |-- static
  24. | |-- style.css
  25. |-- templates
  26. |-- myplugin.html
  27. |-- migrations
  28. |-- 59f7c49b6289_init.py
  29. Management
  30. ----------
  31. Database
  32. ~~~~~~~~
  33. Upgrading, downgrading and generating database revisions is all handled
  34. via alembic. We make use of a alembic feature called 'branch_labels'.
  35. Each plugin's identifier will be used as a branch_label if used with alembic.
  36. Lets say, that identifier of your plugin is ``portal_plugin``, then you have
  37. to use the following commands for generaring, upgrading and downgrading
  38. your plugins database migrations:
  39. * (Auto-)Generating revisions
  40. ``flaskbb db revision --branch portal_plugin "<YOUR_MESSAGE>"``
  41. Replace <YOUR_MESSAGE> with something like "initial migration" if it's
  42. the first migration or with just a few words that will describe the
  43. changes of the revision.
  44. * Applying revisions
  45. ``flaskbb db upgrade portal_plugin@head``
  46. If you want to upgrade to specific revision, replace ``head`` with the
  47. revision id.
  48. * Downgrading revisions
  49. ``flaskbb db downgrade portal_plugin@-1``
  50. If you just want to revert the latest revision, just use ``-1``.
  51. To downgrade all database migrations, use ``base``.
  52. Deactivating
  53. ~~~~~~~~~~~~
  54. The only way to disable a plugin without removing it is, to add a ``DISABLED``
  55. file in the plugin's root folder. You need to reload your application in order
  56. to have the plugin fully disabled. A disabled plugin could look like this::
  57. my_plugin
  58. |-- DISABLED # Just add a empty file named "DISABLED" to disable a plugin
  59. |-- info.json
  60. |-- __init__.py
  61. .. important:: Restart the server.
  62. You must restart the wsgi/in-built server in order to make the changes
  63. effect your forum.
  64. Activating
  65. ~~~~~~~~~~
  66. Simply remove the ``DISABLED`` file in the plugin directory and restart the
  67. server.
  68. Example Plugin
  69. --------------
  70. A simple Plugin could look like this:
  71. .. sourcecode:: python
  72. from flask import flash
  73. from flask.ext.plugins import connect_event
  74. from flaskbb.plugins import FlaskBBPlugin
  75. # This is the name of your Plugin class which implements FlaskBBPlugin.
  76. # The exact name is needed in order to be recognized as a plugin.
  77. __plugin__ "HelloWorldPlugin"
  78. def flash_index():
  79. """Flashes a message when visiting the index page."""
  80. flash("This is just a demonstration plugin", "success")
  81. class HelloWorldPlugin(FlaskBBPlugin):
  82. def setup(self):
  83. connect_event(before-forum-index-rendered, flash_index)
  84. def install(self):
  85. # there is nothing to install
  86. pass
  87. def uninstall(self):
  88. # and nothing to uninstall
  89. pass
  90. Your plugins also needs a ``info.json`` file, where it stores some meta data
  91. about the plugin. For more information see the `Metadata <#metadata>`_
  92. section below.
  93. Metadata
  94. ~~~~~~~~
  95. In order to get a working plugin, following metadata should be defined
  96. in a ``info.json`` file.
  97. ``identifier`` : **required**
  98. The plugin's identifier. It should be a Python identifier (starts with a
  99. letter or underscore, the rest can be letters, underscores, or numbers)
  100. and should match the name of the plugin's folder.
  101. ``name`` : **required**
  102. A human-readable name for the plugin.
  103. ``author`` : **required**
  104. The name of the plugin's author, that is, you. It does not have to include
  105. an e-mail address, and should be displayed verbatim.
  106. ``description``
  107. A description of the plugin in a few sentences. If you can write multiple
  108. languages, you can include additional fields in the form
  109. ``description_lc``, where ``lc`` is a two-letter language code like ``es``
  110. or ``de``. They should contain the description, but in the indicated
  111. language.
  112. ``website``
  113. The URL of the plugin's Web site. This can be a Web site specifically for
  114. this plugin, Web site for a collection of plugins that includes this plugin,
  115. or just the author's Web site.
  116. ``license``
  117. A simple phrase indicating your plugin's license, like ``GPL``,
  118. ``MIT/X11``, ``Public Domain``, or ``Creative Commons BY-SA 3.0``. You
  119. can put the full license's text in the ``license.txt`` file.
  120. ``version``
  121. This is simply to make it easier to distinguish between what version
  122. of your plugin people are using. It's up to the theme/layout to decide
  123. whether or not to show this, though.
  124. Events
  125. ------
  126. A full list with events can be found here :doc:`events`.
  127. Plugin Class
  128. ------------
  129. .. autoclass:: FlaskBBPlugin
  130. .. autoattribute:: settings_key
  131. .. autoattribute:: has_settings
  132. .. autoattribute:: installed
  133. .. automethod:: setup
  134. .. automethod:: install
  135. .. automethod:: uninstall
  136. .. automethod:: register_blueprint