Skip to main content

Enables adding a preview button in your forms

Project description

General description

Preview cube adds the ability to preview the effect of a form submission.

The main idea is:

  • submit the form data to a special controller, that will insert / update data in the database transaction, as normal

  • generate the preview

  • rollback the database changes to leave the database unchanged

The only exceptions to this rules are downloadable entities, like images : when an html page (as a preview example view) that contains newly created / updated images is rendered, the actual image content is queried by users’ browser a while after the above-mentionned database rollback was performed. The solution adopted in this cube is to save such contents on the disk, and keep it for some time (that can be set using the preview-time option) so that it can be previewed and clean it up afterwards.

Usage

Simplest usage concerns automatic entity forms, where you generally want to preview the just created or updated entity : in this case, import PreviewFormMixin from cubicweb_preview.utils and make your form inherit it : you’re done. For example, to apply it to all AutomaticEntityForm forms, use:

from cubicweb.selectors import yes
from cubicweb_web.views.autoform import AutomaticEntityForm
from cubicweb_preview.views.forms import PreviewFormMixin

class PreviewAutomaticEntityForm(PreviewFormMixin, AutomaticEntityForm):
    __select__ = AutomaticEntityForm.__select__ & yes()

You can of course customize the preview, using PreviewFormMixin preview_vid and preview_rql attributes, that will be used by the controller to create a result set (using preview_rql, if not None) and apply a view (which name is preview_vid value, default to “index”) to it.

Below is an example for a CubicWeb instance using the file and preview cubes, that can be used to preview the list of all images when you add or edit one : we request all images through the preview_rql setting and display them using the primary view

from cubicweb.selectors import is_instance
from cubicweb_web.views.autoform import AutomaticEntityForm
from cubicweb_preview.views.forms import PreviewFormMixin

class ImageForm(PreviewFormMixin, AutomaticEntityForm):
   __select__ = AutomaticEntityForm.__select__ & is_instance('Image')
   preview_vid = 'primary'
   preview_rql = 'Any X WHERE X is Image'
   preview_mode = 'inline'

Note that the previously created images are stored in the database while the previewed one is temporarily stored on disk. By default, the previewed image will be kept for one hour on disk, which can be set using the preview-store-time option.

The preview_mode attribute accepts newtab (the default, opening the preview in a new browser windows/tab within a custom template), or inline (will show the preview below the form).

Authors

This cube has been written by SecondWeb S.A.S. and is now maintained and developped by Logilab S.A.

License

This cube is distributed under the LGPL-2 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cubicweb_preview-3.1.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cubicweb_preview-3.1.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file cubicweb_preview-3.1.0.tar.gz.

File metadata

  • Download URL: cubicweb_preview-3.1.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for cubicweb_preview-3.1.0.tar.gz
Algorithm Hash digest
SHA256 f7fc914623d2d9d09f56ce02b2d2fe3721417c56b5040d628e24e213c10a7f9c
MD5 b66f465df6ea23b55fc4b833227f6507
BLAKE2b-256 1a53e17687d81ee58231e9924daa26bf756482453e0900880a5e156b3f8db05e

See more details on using hashes here.

File details

Details for the file cubicweb_preview-3.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cubicweb_preview-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af20eefad90f18d0df7cf647a367b3cbdb8068b4ecc160b994cb4b06b7f712e1
MD5 61367eb8f069dd880c029580f13b69d9
BLAKE2b-256 57dfebe6df7a4e7c77fbe40e50a229e831e90922d700bf73d86a5e868b8c9963

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page