PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/shabti_templates/shabti_formalchemy.rst

https://bitbucket.org/gjhiggins/shabti
ReStructuredText | 247 lines | 156 code | 91 blank | 0 comment | 0 complexity | 86a4b2d3e4f8e02f2ad695f3034187a2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. .. _shabti_formalchemy:
  2. |today|
  3. .. _shabti-formalchemy:
  4. **shabti_formalchemy** -- "Pylons Admin" Web UI
  5. ===============================================
  6. .. note:: This Shabti template was kindly contributed by Ga??l Pasgrimaud of the Formalchemy development team.
  7. .. rubric :: This template is a basic :ref:`shabti auth <shabti-auth>` setup which has been configured to use the `Formalchemy <http://code.google.com/p/formalchemy/>`_ package. Formalchemy generates HTML form fields and tables from SQLAlchemy mapped classes or manually added Fields. And, also, Elixir mapped classes. In this template, the standard Shabti identity model is coupled with FormAlchemy's "Pylons Admin" facility to produce an auto-generated facility for administrating the population of the identity model.
  8. In essence this template provides an out-of-the-box web interface for maintaining a lightweight identity model of Users, Groups and Permissions. Using the Pylons Admin interface is straightforward, direct and the interface itself is quite self-explanatory.
  9. .. note :: shabti_formalchemy source code is in the `bitbucket code repository <http://bitbucket.org/gjhiggins/shabti/src/tip/shabti/templates/formalchemy/>`_
  10. About Formalchemy
  11. -----------------
  12. The following description is taken from the `Formalchemy project web site <http://code.google.com/p/formalchemy/>`_ ...
  13. "FormAlchemy greatly speeds development with SQLAlchemy mapped classes (models) in a HTML forms environment.
  14. FormAlchemy eliminates boilerplate by autogenerating HTML input fields from a given model. FormAlchemy will try to figure out what kind of HTML code should be returned by introspecting the model's properties and generate ready-to-use HTML code that will fit the developer's application.
  15. Of course, FormAlchemy can't figure out everything, i.e, the developer might want to display only a few columns from the given model. Thus, FormAlchemy is also highly customizable."
  16. .. seealso:: For further details, see the project `documentation <http://docs.formalchemy.org/>`_
  17. Dependencies
  18. ------------
  19. You need to ``easy-install FormAlchemy`` and ``easy-install fa.jquery`` before using the template.
  20. Using the template
  21. ------------------
  22. After successfully installing Shabti, additional paster templates will be available. Simply create a Shabti-configured project by specifying that paster should use the shabti_formalchemy template:
  23. .. code-block :: bash
  24. $ paster create -t shabti_formalchemy myproj
  25. These are the option dialogue choices appropriate for the Shabti auth shabti_formalchemy template --- which uses mako templates and requires SQLAlchemy ...
  26. .. code-block :: bash
  27. (mako/genshi/jinja/etc: Template language) ['mako']:
  28. (True/False: Include SQLAlchemy 0.4 configuration) [False]: True
  29. Once the project has been created, navigate to the project directory.
  30. The next step is to initialise the database by running the project setup script which will create the initial entries.
  31. .. code-block :: bash
  32. $ paster setup-app development.ini
  33. The next (optional) step after initialising the relational store is to run the tests.
  34. .. code-block :: bash
  35. $ nosetests
  36. 8/10 tests should run successfully. Two are known to fail but these failures appear to be restricted to the test environment, normal functioning remains unaffected.
  37. Running the generated app
  38. -------------------------
  39. After initialising and testing, start the Pylons web app with:
  40. .. code-block :: bash
  41. $ paster serve --reload development.ini
  42. The Shabti Formalchemy template's variant on the standard Pylons welcome screen is browsable at at ``http://localhost:5000/`` ...
  43. Welcome screen
  44. ^^^^^^^^^^^^^^
  45. .. image:: images/shabti_formalchemy_welcome.jpg
  46. :align: center
  47. .. note:: The identity admin interface is pre-rolled and is accessible via the "Admin WUI" link.
  48. Template details
  49. ----------------
  50. This template is an extension of the standard :ref:`shabti auth <shabti-auth>` template that adds and routes an "admin" controller for the CRUD operations on the identity model and an ancillary "jquery" controller whose sole purpose is the handling of the resolution of JQuery libs.
  51. The "admin" controller imports the standard Shabti Elixir-mapped identity model of User, Permission and Group entities and hooks them up to FormAlchemy:
  52. .. code-block:: python
  53. import logging
  54. from MYPROJ.lib.base import BaseController, render
  55. from MYPROJ import model
  56. from MYPROJ import forms
  57. from formalchemy.ext.pylons.admin import FormAlchemyAdminController
  58. log = logging.getLogger(__name__)
  59. class AdminController(BaseController):
  60. model = model # where the Elixir mappers are
  61. forms = forms # module containing FormAlchemy fieldsets definitions
  62. # # Uncomment this to impose an authentication requirement
  63. # @authorize(SignedIn())
  64. # def __before__(self):
  65. # pass
  66. def Session(self): # Session factory
  67. return meta.Session
  68. AdminController = FormAlchemyAdminController(AdminController)
  69. (Note the commented-out code for access-protecting the Pylons Admin controller. Uncommenting the code will cause requests made of ``/admin`` resources to be automatically redirected to a login page and, upon successful authentication, will be automatically redirected to the originally-requested resource.)
  70. The routing is simple and straighforward:
  71. .. code-block:: python
  72. from formalchemy.ext.pylons import maps # routes generator
  73. # [ ... ]
  74. # CUSTOM ROUTES HERE
  75. # Map the /admin url to FA's AdminController
  76. # Map static files
  77. map.connect('fa_static', '/jquery/{path_info:.*}',
  78. controller='jquery')
  79. # Index page
  80. map.connect('admin', '/admin',
  81. controller='admin', action='models')
  82. map.connect('formatted_admin', '/admin.json',
  83. controller='admin', action='models', format='json')
  84. # Models
  85. map.resource('model', 'models', path_prefix='/admin/{model_name}',
  86. controller='admin')
  87. The form templating engine (by default Tempita but in this instance, Mako) is initialised and the JQuery-supported fieldset and grid form holders are all defined in ``MYPROJ/form/__init__.py``:
  88. .. code-block:: python
  89. from pylons import config
  90. from fatest import model
  91. from fatest.lib.base import render
  92. from formalchemy import config as fa_config
  93. from formalchemy import templates
  94. from formalchemy import validators
  95. from formalchemy import fields
  96. from formalchemy import forms
  97. from formalchemy import tables
  98. from formalchemy.ext.fsblob import FileFieldRenderer
  99. from formalchemy.ext.fsblob import ImageFieldRenderer
  100. from fa.jquery import renderers as jquery
  101. if config.get('storage_path'):
  102. # set the storage_path if we can find its setting
  103. FileFieldRenderer.storage_path = config.get('storage_path')
  104. ImageFieldRenderer.storage_path = config.get('storage_path')
  105. fa_config.encoding = 'utf-8'
  106. class TemplateEngine(templates.TemplateEngine):
  107. def render(self, name, **kwargs):
  108. return render('/forms/%s.mako' % name, extra_vars=kwargs)
  109. fa_config.engine = TemplateEngine()
  110. # use jquery renderers
  111. forms.FieldSet.default_renderers.update(jquery.default_renderers)
  112. class FieldSet(forms.FieldSet):
  113. pass
  114. class Grid(tables.Grid):
  115. pass
  116. ## Initialize fieldsets
  117. User = FieldSet(model.User)
  118. User.configure(options=[User.created.readonly()])
  119. UserAdd = FieldSet(model.User)
  120. UserAdd.configure(exclude=[UserAdd.created])
  121. ## Initialize grids
  122. UserGrid = Grid(model.User)
  123. UserGrid.configure(include=[
  124. UserGrid.username,
  125. UserGrid.email,
  126. UserGrid.active,
  127. UserGrid.groups,
  128. ])
  129. A set of FA's CSS grid-styled Mako templates are installed into the ``myproj/templates`` directory and the application is immediately ready for use.
  130. Screenshots
  131. -----------
  132. View of the Shabti standard identity model
  133. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  134. .. image:: images/shabti_formalchemy_admin.jpg
  135. :align: center
  136. View of the User admin page
  137. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  138. .. image:: images/shabti_formalchemy_user_index.jpg
  139. :align: center
  140. Editing an existing User entity
  141. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  142. .. image:: images/shabti_formalchemy_user_edit.jpg
  143. :align: center
  144. Creating a new user entity
  145. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  146. .. image:: images/shabti_formalchemy_user_new.jpg
  147. :align: center
  148. .. seealso:: Form structure, content and appearance can all be customized. For details on how to customize the forms and the fields, see the docs for the `Formalchemy Pylons extension <http://docs.formalchemy.org/current/ext/pylons.html#customization>`_
  149. TODO
  150. ----
  151. 1. Adjust the generation of the form so that password fields are presented unpopulated.
  152. 2. Find out why the 2 authentication tests fail.
  153. 3. Add a more extensive example, demonstrating more of FormAlchemy's features.
  154. 4. Add FormAlchemy and the `CouchDBkit Formalchemy extension <http://couchdbkit.org/docs/formalchemy.html>`_ to the Shabti auth_couchdb template.
  155. :author: Graham Higgins <gjh@bel-epa.com>
  156. |today|