spec.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.plugins.spec
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. This module provides the core FlaskBB plugin hook definitions
  6. :copyright: (c) 2017 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from pluggy import HookspecMarker
  10. spec = HookspecMarker('flaskbb')
  11. # Setup Hooks
  12. @spec
  13. def flaskbb_extensions(app):
  14. """Hook for initializing any plugin loaded extensions."""
  15. @spec
  16. def flaskbb_load_translations():
  17. """Hook for registering translation folders."""
  18. @spec
  19. def flaskbb_load_migrations():
  20. """Hook for registering additional migrations."""
  21. @spec
  22. def flaskbb_load_blueprints(app):
  23. """Hook for registering blueprints.
  24. :param app: The application object.
  25. """
  26. @spec
  27. def flaskbb_request_processors(app):
  28. """Hook for registering pre/post request processors.
  29. :param app: The application object.
  30. """
  31. @spec
  32. def flaskbb_errorhandlers(app):
  33. """Hook for registering error handlers.
  34. :param app: The application object.
  35. """
  36. @spec
  37. def flaskbb_jinja_directives(app):
  38. """Hook for registering jinja filters, context processors, etc.
  39. :param app: The application object.
  40. """
  41. @spec
  42. def flaskbb_additional_setup(app, pluggy):
  43. """Hook for any additional setup a plugin wants to do after all other
  44. application setup has finished.
  45. For example, you could apply a WSGI middleware::
  46. @impl
  47. def flaskbb_additional_setup(app):
  48. app.wsgi_app = ProxyFix(app.wsgi_app)
  49. :param app: The application object.
  50. :param pluggy: The pluggy object.
  51. """
  52. @spec
  53. def flaskbb_cli(cli, app):
  54. """Hook for registering CLI commands.
  55. For example::
  56. @impl
  57. def flaskbb_cli(cli):
  58. @cli.command()
  59. def testplugin():
  60. click.echo("Hello Testplugin")
  61. return testplugin
  62. :param app: The application object.
  63. :param cli: The FlaskBBGroup CLI object.
  64. """
  65. @spec
  66. def flaskbb_shell_context():
  67. """Hook for registering shell context handlers
  68. Expected to return a single callable function that returns a dictionary or
  69. iterable of key value pairs.
  70. """
  71. # Event hooks
  72. @spec
  73. def flaskbb_event_post_save_before(post):
  74. """Hook for handling a post before it has been saved.
  75. :param flaskbb.forum.models.Post post: The post which triggered the event.
  76. """
  77. @spec
  78. def flaskbb_event_post_save_after(post, is_new):
  79. """Hook for handling a post after it has been saved.
  80. :param flaskbb.forum.models.Post post: The post which triggered the event.
  81. :param bool is_new: True if the post is new, False if it is an edit.
  82. """
  83. @spec
  84. def flaskbb_event_topic_save_before(topic):
  85. """Hook for handling a topic before it has been saved.
  86. :param flaskbb.forum.models.Topic topic: The topic which triggered the
  87. event.
  88. """
  89. @spec
  90. def flaskbb_event_topic_save_after(topic, is_new):
  91. """Hook for handling a topic after it has been saved.
  92. :param flaskbb.forum.models.Topic topic: The topic which triggered the
  93. event.
  94. :param bool is_new: True if the topic is new, False if it is an edit.
  95. """
  96. # Form hooks
  97. @spec
  98. def flaskbb_form_new_post(form):
  99. """Hook for modifying the :class:`~flaskbb.forum.forms.ReplyForm`.
  100. For example::
  101. @impl
  102. def flaskbb_form_new_post(form):
  103. form.example = TextField("Example Field", validators=[
  104. DataRequired(message="This field is required"),
  105. Length(min=3, max=50)])
  106. :param form: The :class:`~flaskbb.forum.forms.ReplyForm` class.
  107. """
  108. @spec
  109. def flaskbb_form_new_post_save(form):
  110. """Hook for modifying the :class:`~flaskbb.forum.forms.ReplyForm`.
  111. This hook is called while populating the post object with
  112. the data from the form. The post object will be saved after the hook
  113. call.
  114. :param form: The form object.
  115. :param post: The post object.
  116. """
  117. @spec
  118. def flaskbb_form_new_topic(form):
  119. """Hook for modifying the :class:`~flaskbb.forum.forms.NewTopicForm`
  120. :param form: The :class:`~flaskbb.forum.forms.NewTopicForm` class.
  121. """
  122. @spec
  123. def flaskbb_form_new_topic_save(form, topic):
  124. """Hook for modifying the :class:`~flaskbb.forum.forms.NewTopicForm`.
  125. This hook is called while populating the topic object with
  126. the data from the form. The topic object will be saved after the hook
  127. call.
  128. :param form: The form object.
  129. :param topic: The topic object.
  130. """
  131. # Template Hooks
  132. @spec
  133. def flaskbb_tpl_navigation_before():
  134. """Hook for registering additional navigation items.
  135. in :file:`templates/layout.html`.
  136. """
  137. @spec
  138. def flaskbb_tpl_navigation_after():
  139. """Hook for registering additional navigation items.
  140. in :file:`templates/layout.html`.
  141. """
  142. @spec
  143. def flaskbb_tpl_user_nav_loggedin_before():
  144. """Hook for registering additional user navigational items
  145. which are only shown when a user is logged in.
  146. in :file:`templates/layout.html`.
  147. """
  148. @spec
  149. def flaskbb_tpl_user_nav_loggedin_after():
  150. """Hook for registering additional user navigational items
  151. which are only shown when a user is logged in.
  152. in :file:`templates/layout.html`.
  153. """
  154. @spec
  155. def flaskbb_tpl_form_registration_before(form):
  156. """This hook is emitted in the Registration form **before** the first
  157. input field but after the hidden CSRF token field.
  158. in :file:`templates/auth/register.html`.
  159. :param form: The form object.
  160. """
  161. @spec
  162. def flaskbb_tpl_form_registration_after(form):
  163. """This hook is emitted in the Registration form **after** the last
  164. input field but before the submit field.
  165. in :file:`templates/auth/register.html`.
  166. :param form: The form object.
  167. """
  168. @spec
  169. def flaskbb_tpl_form_user_details_before(form):
  170. """This hook is emitted in the Change User Details form **before** an
  171. input field is rendered.
  172. in :file:`templates/user/change_user_details.html`.
  173. :param form: The form object.
  174. """
  175. @spec
  176. def flaskbb_tpl_form_user_details_after(form):
  177. """This hook is emitted in the Change User Details form **after** the last
  178. input field has been rendered but before the submit field.
  179. in :file:`templates/user/change_user_details.html`.
  180. :param form: The form object.
  181. """
  182. @spec
  183. def flaskbb_tpl_profile_settings_menu():
  184. """This hook is emitted on the user settings page in order to populate the
  185. side bar menu. Implementations of this hook should return a list of tuples
  186. that are view name and display text. The display text will be provided to
  187. the translation service so it is unnecessary to supply translated text.
  188. A plugin can declare a new block by setting the view to None. If this is
  189. done, consider marking the hook implementation with `trylast=True` to
  190. avoid capturing plugins that do not create new blocks.
  191. For example::
  192. @impl(trylast=True)
  193. def flaskbb_tpl_profile_settings_menu():
  194. return [
  195. (None, 'Account Settings'),
  196. ('user.settings', 'General Settings'),
  197. ('user.change_user_details', 'Change User Details'),
  198. ('user.change_email', 'Change E-Mail Address'),
  199. ('user.change_password', 'Change Password')
  200. ]
  201. Hookwrappers for this spec should not be registered as FlaskBB
  202. supplies its own hookwrapper to flatten all the lists into a single list.
  203. in :file:`templates/user/settings_layout.html`
  204. """
  205. @spec
  206. def flaskbb_tpl_admin_settings_menu(user):
  207. """This hook is emitted in the admin panel and used to add additional
  208. navigation links to the admin menu.
  209. Implementations of this hook should return a list of tuples
  210. that are view name, display text and optionally an icon.
  211. The display text will be provided to the translation service so it
  212. is unnecessary to supply translated text.
  213. For example::
  214. @impl(trylast=True)
  215. def flaskbb_tpl_admin_settings_menu():
  216. # only add this item if the user is an admin
  217. if Permission(IsAdmin, identity=current_user):
  218. return [
  219. ("myplugin.foobar", "Foobar", "fa fa-foobar")
  220. ]
  221. Hookwrappers for this spec should not be registered as FlaskBB
  222. supplies its own hookwrapper to flatten all the lists into a single list.
  223. in :file:`templates/management/management_layout.html`
  224. :param user: The current user object.
  225. """
  226. @spec
  227. def flaskbb_tpl_profile_sidebar_stats(user):
  228. """This hook is emitted on the users profile page below the standard
  229. information. For example, it can be used to add additional items
  230. such as a link to the profile.
  231. in :file:`templates/user/profile_layout.html`
  232. :param user: The user object for whom the profile is currently visited.
  233. """
  234. @spec
  235. def flaskbb_tpl_post_author_info_before(user, post):
  236. """This hook is emitted before the information about the
  237. author of a post is displayed (but after the username).
  238. in :file:`templates/forum/topic.html`
  239. :param user: The user object of the post's author.
  240. :param post: The post object.
  241. """
  242. @spec
  243. def flaskbb_tpl_post_author_info_after(user, post):
  244. """This hook is emitted after the information about the
  245. author of a post is displayed (but after the username).
  246. in :file:`templates/forum/topic.html`
  247. :param user: The user object of the post's author.
  248. :param post: The post object.
  249. """
  250. @spec
  251. def flaskbb_tpl_post_content_before(post):
  252. """Hook to do some stuff before the post content is rendered.
  253. in :file:`templates/forum/topic.html`
  254. :param post: The current post object.
  255. """
  256. @spec
  257. def flaskbb_tpl_post_content_after(post):
  258. """Hook to do some stuff after the post content is rendered.
  259. in :file:`templates/forum/topic.html`
  260. :param post: The current post object.
  261. """
  262. @spec
  263. def flaskbb_tpl_post_menu_before(post):
  264. """Hook for inserting a new item at the beginning of the post menu.
  265. in :file:`templates/forum/topic.html`
  266. :param post: The current post object.
  267. """
  268. @spec
  269. def flaskbb_tpl_post_menu_after(post):
  270. """Hook for inserting a new item at the end of the post menu.
  271. in :file:`templates/forum/topic.html`
  272. :param post: The current post object.
  273. """
  274. @spec
  275. def flaskbb_tpl_topic_controls(topic):
  276. """Hook for inserting additional topic moderation controls.
  277. in :file:`templates/forum/topic_controls.html`
  278. :param topic: The current topic object.
  279. """
  280. @spec
  281. def flaskbb_tpl_form_new_post_before(form):
  282. """Hook for inserting a new form field before the first field is
  283. rendered.
  284. For example::
  285. @impl
  286. def flaskbb_tpl_form_new_post_after(form):
  287. return render_template_string(
  288. \"""
  289. <div class="form-group">
  290. <div class="col-md-12 col-sm-12 col-xs-12">
  291. <label>{{ form.example.label.text }}</label>
  292. {{ form.example(class="form-control",
  293. placeholder=form.example.label.text) }}
  294. {%- for error in form.example.errors -%}
  295. <span class="help-block">{{error}}</span>
  296. {%- endfor -%}
  297. </div>
  298. </div>
  299. \"""
  300. in :file:`templates/forum/new_post.html`
  301. :param form: The form object.
  302. """
  303. @spec
  304. def flaskbb_tpl_form_new_post_after(form):
  305. """Hook for inserting a new form field after the last field is
  306. rendered (but before the submit field).
  307. in :file:`templates/forum/new_post.html`
  308. :param form: The form object.
  309. """
  310. @spec
  311. def flaskbb_tpl_form_new_topic_before(form):
  312. """Hook for inserting a new form field before the first field is
  313. rendered (but before the CSRF token).
  314. in :file:`templates/forum/new_topic.html`
  315. :param form: The form object.
  316. """
  317. @spec
  318. def flaskbb_tpl_form_new_topic_after(form):
  319. """Hook for inserting a new form field after the last field is
  320. rendered (but before the submit button).
  321. in :file:`templates/forum/new_topic.html`
  322. :param form: The form object.
  323. """