viewhelpers.formview.previewmixin — Preview view mixin¶
Warning
PreviewMixin
is broken, and may be removed or fixed in a future cradmin release.
-
class
PreviewMixin
¶ Bases:
object
Mixin class to provide a preview view for your views.
You must override
serialize_preview()
,deserialize_preview()
andget_preview_url()
, and you must callstore_preview_in_session()
before opening the preview.You must also ensure your template extends
django_cradmin/viewhelpers/formview_base.django.html
.Preview data is added to a Django session using
store_preview_in_session()
, and popped (fetched and removed) from the session in the preview view usingget_preview_data()
. A typical place to callstore_preview_in_session()
is in the form_valid() method of form views. Example:class MyFormView(viewhelpers.formview.PreviewMixin, viewhelpers.formview.WithinRoleFormView): # Ensure this extends django_cradmin/viewhelpers/formview_base.django.html template_name = 'myapp/mytemplate.django.html' # ... other required code for formbase.FormView ... def form_valid(self, form): if self.preview_requested(): self.store_preview_in_session(self.serialize_preview(form)) return self.render_to_response(self.get_context_data(form=form, show_preview=True)) else: # ... save, update, or whatever you do on POST when preview is not requested ... def get_buttons(self): return [ PrimarySubmit('save', _('Save')), # When this button is clicked, self.preview_requested() returns True (see form_valid above). DefaultSubmit(self.submit_preview_name, _('Preview')) ] def serialize_preview(self, form): return json.dumps({ 'title': form.cleaned_data['title'], 'description': form.cleaned_data['description'], }) @classmethod def deserialize_preview(cls, serialized): return json.loads(serialized)
If you have something like MyFormView implemented, a preview view is as simple as this:
class MyPreviewView(View): def get(request): preview_data = MyFormView.get_preview_data() return HttpResponse(...)
How you render your preview data in your view is entirely up to you - a TemplateView that fetches preview data in get_context_data() is ususally more approproate than a View like the example above.
-
submit_preview_name
= 'submit-preview'¶ The name of the submit button used for preview.
-
preview_requested
()¶ Determine if a preview was requested.
Defaults to checking if
submit_preview_name
is inrequest.POST
.
-
serialize_preview
(form)¶ Seralize data for preview.
You must override this and
deserialize_preview()
- they work together to send the preview to the preview View. You can return anything that can be put into a Django session here. We recommend returning a string to ensure your code work with any session backend. JSON encoding is a good choice is most cases.
-
classmethod
deserialize_preview
(serialized)¶ Deseralize a preview serialized with
serialize_preview()
.You must override this and
serialize_preview()
- they work together to send the preview to the preview View.
-
classmethod
get_preview_sessionkey
()¶ Get the session key used for preview. You should not need to override this.
-
classmethod
get_preview_data
(request)¶ Get the preview data.
You should use this in the preview view to get the data for the preview.
You normally do not override this. If you want to manage serialization yourself, see
serialize_preview()
.
-
get_preview_url
()¶ Get the URL of the preview view.
-
add_preview_mixin_context_data
(context)¶ Must be used by get_context_data() in subclasses to add the context data required to render the view.
Examples
Adding the required context data:
def get_context_data(self, **kwargs): context = super(MyView, self).get_context_data(**kwargs) self.add_preview_mixin_context_data(context=context) return context
-