<commit_before>"""
Form designer link plugin.

This plugin displays a form at the page, that was created with form_designer.
To customize the output, configure the ``django-form-designer`` application via the settings file.
For example, use:

 * ``FORM_DESIGNER_DEFAULT_FORM_TEMPLATE`` or ``FORM_TEMPLATES`` to control the form output (e.g. render it with ``django-uni-form``).
 * ``FORM_DESIGNER_FIELD_CLASSES`` to define which field types are allowed.
 * ``FORM_DESIGNER_WIDGET_CLASSES`` to define which widgets are allowed.
"""
from django.utils.translation import ugettext_lazy as _
from fluent_contents.extensions import ContentPlugin, plugin_pool
from fluent_contents.plugins.formdesignerlink.models import FormDesignerLink
from form_designer import settings as form_designer_settings
from form_designer.views import process_form


@plugin_pool.register
class FormDesignerLinkPlugin(ContentPlugin):
    model = FormDesignerLink
    category = _('Interactivity')
    cache_output = False


    def get_render_template(self, request, instance, **kwargs):
        return instance.form_definition.form_template_name or self.render_template or form_designer_settings.DEFAULT_FORM_TEMPLATE


    def get_context(self, request, instance, **kwargs):
        context = {}
        # The process_form() function is designed with Django CMS in mind,
        # and responds to both the GET and POST request.
        return process_form(request, instance.form_definition, context, is_cms_plugin=True)

<commit_msg>Simplify the formdesignerlink code by overwriting render() instead.<commit_after>"""
Form designer link plugin.

This plugin displays a form at the page, that was created with form_designer.
To customize the output, configure the ``django-form-designer`` application via the settings file.
For example, use:

 * ``FORM_DESIGNER_DEFAULT_FORM_TEMPLATE`` or ``FORM_TEMPLATES`` to control the form output (e.g. render it with ``django-uni-form``).
 * ``FORM_DESIGNER_FIELD_CLASSES`` to define which field types are allowed.
 * ``FORM_DESIGNER_WIDGET_CLASSES`` to define which widgets are allowed.
"""
from django.utils.translation import ugettext_lazy as _
from fluent_contents.extensions import ContentPlugin, plugin_pool
from fluent_contents.plugins.formdesignerlink.models import FormDesignerLink
from form_designer import settings as form_designer_settings
from form_designer.views import process_form


@plugin_pool.register
class FormDesignerLinkPlugin(ContentPlugin):
    model = FormDesignerLink
    category = _('Interactivity')
    cache_output = False


    def get_render_template(self, request, instance, **kwargs):
        # Overwritten to return a template from the instance.
        return instance.form_definition.form_template_name or self.render_template or form_designer_settings.DEFAULT_FORM_TEMPLATE


    def render(self, request, instance, **kwargs):
        # While overwriting get_context() would be sufficient here, this is rather easier to understand.
        # Implemented a custom rendering function instead.

        # The process_form() function is designed with Django CMS in mind,
        # and responds to both the GET and POST request.
        context = process_form(request, instance.form_definition, {}, is_cms_plugin=True)
        render_template = self.get_render_template(request, instance, **kwargs)

        return self.render_to_string(request, render_template, context)
{'fraction_non_alphanumeric': 0.07172131147540983, 'fraction_numerical': 0.0, 'mean_word_length': 4.963350785340314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5042294', 'n_tokens_mistral': 944, 'n_tokens_neox': 907, 'n_words': 310}