plugins.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. You can either deactivate the plugin via the Admin Panel or by running::
  55. flaskbb plugins disable <plugin_name>
  56. .. important:: Restart the server.
  57. You must restart the wsgi/in-built server in order to make the changes
  58. effect your forum.
  59. Activating
  60. ~~~~~~~~~~
  61. All plugins are activated by default. To activate a deactivated plugin you
  62. either have to activate it via the Admin Panel again or by running the
  63. activation command::
  64. flaskbb plugins enable <plugin_name>
  65. Example Plugin
  66. --------------
  67. A simple Plugin could look like this:
  68. .. sourcecode:: python
  69. from flask import flash
  70. from flask.ext.plugins import connect_event
  71. from flaskbb.plugins import FlaskBBPlugin
  72. # This is the name of your Plugin class which implements FlaskBBPlugin.
  73. # The exact name is needed in order to be recognized as a plugin.
  74. __plugin__ "HelloWorldPlugin"
  75. def flash_index():
  76. """Flashes a message when visiting the index page."""
  77. flash("This is just a demonstration plugin", "success")
  78. class HelloWorldPlugin(FlaskBBPlugin):
  79. def setup(self):
  80. connect_event(before-forum-index-rendered, flash_index)
  81. def install(self):
  82. # there is nothing to install
  83. pass
  84. def uninstall(self):
  85. # and nothing to uninstall
  86. pass
  87. Your plugins also needs a ``info.json`` file, where it stores some meta data
  88. about the plugin. For more information see the `Metadata <#metadata>`_
  89. section below.
  90. Metadata
  91. ~~~~~~~~
  92. A proper plugin should have at least put the following metadata into
  93. the ``setup.py`` file.
  94. https://docs.python.org/3.6/distutils/setupscript.html#additional-meta-data
  95. https://github.com/pypa/sampleproject/blob/master/setup.py
  96. ``identifier`` : **required**
  97. The plugin's identifier. It should be a Python identifier (starts with a
  98. letter or underscore, the rest can be letters, underscores, or numbers)
  99. and should match the name of the plugin's folder.
  100. ``name`` : **required**
  101. A human-readable name for the plugin.
  102. ``author`` : **required**
  103. The name of the plugin's author, that is, you. It does not have to include
  104. an e-mail address, and should be displayed verbatim.
  105. ``description``
  106. A description of the plugin in a few sentences. If you can write multiple
  107. languages, you can include additional fields in the form
  108. ``description_lc``, where ``lc`` is a two-letter language code like ``es``
  109. or ``de``. They should contain the description, but in the indicated
  110. language.
  111. ``website``
  112. The URL of the plugin's Web site. This can be a Web site specifically for
  113. this plugin, Web site for a collection of plugins that includes this plugin,
  114. or just the author's Web site.
  115. ``license``
  116. A simple phrase indicating your plugin's license, like ``GPL``,
  117. ``MIT/X11``, ``Public Domain``, or ``Creative Commons BY-SA 3.0``. You
  118. can put the full license's text in the ``license.txt`` file.
  119. ``version``
  120. This is simply to make it easier to distinguish between what version
  121. of your plugin people are using. It's up to the theme/layout to decide
  122. whether or not to show this, though.
  123. Hooks
  124. -----
  125. FlaskBB uses so called 'Hooks' ...
  126. Under the hood we use `pluggy`...
  127. A full list of hooks can be found here :doc:`hooks`.