def view_form_entry(request, form_entry_slug, theme=None, template_name=None): """ View create form. :param django.http.HttpRequest request: :param string form_entry_slug: :param fobi.base.BaseTheme theme: Theme instance. :param string template_name: :return django.http.HttpResponse: """ try: kwargs = {'slug': form_entry_slug} if not request.user.is_authenticated(): kwargs.update({'is_public': True}) form_entry = FormEntry._default_manager.select_related('user') \ .get(**kwargs) except ObjectDoesNotExist as err: raise Http404(ugettext("Form entry not found.")) form_element_entries = form_entry.formelemententry_set.all()[:] # This is where the most of the magic happens. Our form is being built # dynamically. FormClass = assemble_form_class(form_entry, form_element_entries=form_element_entries, request=request) if 'POST' == request.method: form = FormClass(request.POST, request.FILES) # Fire pre form validation callbacks fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_BEFORE_FORM_VALIDATION) if form.is_valid(): # Fire form valid callbacks, before handling submitted plugin # form data. form = fire_form_callbacks( form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID_BEFORE_SUBMIT_PLUGIN_FORM_DATA) # Fire plugin processors form = submit_plugin_form_data(form_entry=form_entry, request=request, form=form) # Fire form valid callbacks form = fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID) # Run all handlers handler_responses, handler_errors = run_form_handlers( form_entry=form_entry, request=request, form=form, form_element_entries=form_element_entries) # Warning that not everything went ok. if handler_errors: for handler_error in handler_errors: messages.warning( request, _("Error occured: {0}." "").format(handler_error)) # Fire post handler callbacks fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID_AFTER_FORM_HANDLERS) messages.info( request, _("Form {0} was submitted successfully." "").format(form_entry.name)) return redirect( reverse('fobi.form_entry_submitted', args=[form_entry.slug])) else: # Fire post form validation callbacks fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_INVALID) else: # Providing initial form data by feeding entire GET dictionary # to the form, if ``GET_PARAM_INITIAL_DATA`` is present in the # GET. kwargs = {} if GET_PARAM_INITIAL_DATA in request.GET: kwargs = {'initial': request.GET} form = FormClass(**kwargs) # In debug mode, try to identify possible problems. if DEBUG: try: form.as_p() except Exception as err: logger.error(err) theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_element_entries) context = { 'form': form, 'form_entry': form_entry, 'fobi_theme': theme, } if not template_name: template_name = theme.view_form_entry_template return render_to_response(template_name, context, context_instance=RequestContext(request))
def view_form_entry(request, form_entry_slug, theme=None, template_name=None): """ View create form. :param django.http.HttpRequest request: :param string form_entry_slug: :param string template_name: :return django.http.HttpResponse: """ try: kwargs = {'slug': form_entry_slug} if not request.user.is_authenticated(): kwargs.update({'is_public': True}) form_entry = FormEntry._default_manager.select_related('user') \ .get(**kwargs) except ObjectDoesNotExist as err: raise Http404(ugettext("Form entry not found.")) form_element_entries = form_entry.formelemententry_set.all()[:] # This is where the most of the magic happens. Our form is being built # dynamically. FormClass = assemble_form_class( form_entry, form_element_entries = form_element_entries, request = request ) if 'POST' == request.method: form = FormClass(request.POST, request.FILES) # Fire pre form validation callbacks fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_BEFORE_FORM_VALIDATION) if form.is_valid(): # Fire form valid callbacks, before handling submitted plugin # form data. form = fire_form_callbacks( form_entry = form_entry, request = request, form = form, stage = CALLBACK_FORM_VALID_BEFORE_SUBMIT_PLUGIN_FORM_DATA ) # Fire plugin processors form = submit_plugin_form_data(form_entry=form_entry, request=request, form=form) # Fire form valid callbacks form = fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID) # Run all handlers handler_responses, handler_errors = run_form_handlers( form_entry = form_entry, request = request, form = form, form_element_entries = form_element_entries ) # Warning that not everything went ok. if handler_errors: for handler_error in handler_errors: messages.warning( request, _("Error occured: {0}." "").format(handler_error) ) # Fire post handler callbacks fire_form_callbacks( form_entry = form_entry, request = request, form = form, stage = CALLBACK_FORM_VALID_AFTER_FORM_HANDLERS ) messages.info( request, _("Form {0} was submitted successfully." "").format(form_entry.name) ) return redirect( reverse('fobi.form_entry_submitted', args=[form_entry.slug]) ) else: # Fire post form validation callbacks fire_form_callbacks(form_entry=form_entry, request=request, form=form, stage=CALLBACK_FORM_INVALID) else: # Providing initial form data by feeding entire GET dictionary # to the form, if ``GET_PARAM_INITIAL_DATA`` is present in the # GET. kwargs = {} if GET_PARAM_INITIAL_DATA in request.GET: kwargs = {'initial': request.GET} form = FormClass(**kwargs) # In debug mode, try to identify possible problems. if DEBUG: try: form.as_p() except Exception as err: logger.error(err) theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_element_entries) context = { 'form': form, 'form_entry': form_entry, 'fobi_theme': theme, } if not template_name: template_name = theme.view_form_entry_template return render_to_response(template_name, context, context_instance=RequestContext(request))
def _process_form(self, request, instance, **kwargs): """ Handle the form if no "sent" GET argument (see the ``WIDGET_FORM_SENT_GET_PARAM`` setting). :param django.http.HttpRequest request: :param fobi.models.FormEntry instance: FormEntry instance. :return django.http.HttpResponse | str: """ template_name = instance.form_template_name or None # Handle public/non-public forms. If form requires user authentication # redirect to login form with next parameter set to current request # path. if not request.user.is_authenticated() and not instance.form_entry.is_public: if self.can_redirect: return redirect( "{0}?next={1}".format(settings.LOGIN_URL, request.path) ) else: return self._show_login_required_page( request, instance, **kwargs ) form_element_entries = instance.form_entry.formelemententry_set.all()[:] # This is where the most of the magic happens. Our form is being built # dynamically. FormClass = assemble_form_class( instance.form_entry, form_element_entries = form_element_entries ) if 'POST' == request.method: form = FormClass(request.POST, request.FILES) # Fire pre form validation callbacks fire_form_callbacks( form_entry = instance.form_entry, request = request, form = form, stage = CALLBACK_BEFORE_FORM_VALIDATION) if form.is_valid(): # Fire form valid callbacks, before handling sufrom # django.http import HttpResponseRedirectbmitted plugin # form data form = fire_form_callbacks( form_entry = instance.form_entry, request = request, form = form, stage = CALLBACK_FORM_VALID_BEFORE_SUBMIT_PLUGIN_FORM_DATA ) # Fire plugin processors form = submit_plugin_form_data( form_entry = instance.form_entry, request = request, form = form ) # Fire form valid callbacks form = fire_form_callbacks( form_entry = instance.form_entry, request = request, form = form, stage = CALLBACK_FORM_VALID ) # Run all handlers run_form_handlers( form_entry = instance.form_entry, request = request, form = form ) # Fire post handler callbacks fire_form_callbacks( form_entry = instance.form_entry, request = request, form = form, stage = CALLBACK_FORM_VALID_AFTER_FORM_HANDLERS ) messages.info( request, _('Form {0} was submitted ' 'successfully.').format(instance.form_entry.name) ) if self.can_redirect: return redirect( "{0}?{1}={2}".format(request.path, \ self.form_sent_get_param, \ instance.form_entry.slug) ) else: return self._show_thanks_page(request, instance, **kwargs) else: # Fire post form validation callbacks fire_form_callbacks( form_entry = instance.form_entry, request = request, form = form, stage = CALLBACK_FORM_INVALID ) else: # Providing initial form data by feeding entire GET dictionary # to the form, if ``GET_PARAM_INITIAL_DATA`` is present in the # GET. kwargs = {} if GET_PARAM_INITIAL_DATA in request.GET: kwargs = {'initial': request.GET} form = FormClass(**kwargs) theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_element_entries) context = { 'form': form, 'form_entry': instance.form_entry, 'fobi_theme': theme, 'fobi_form_title': instance.form_title, 'fobi_hide_form_title': instance.hide_form_title, 'fobi_form_submit_button_text': instance.form_submit_button_text } if not template_name: template_name = theme.view_embed_form_entry_ajax_template self.rendered_output = render_to_string( template_name, context, context_instance=RequestContext(request) )
def _process_form(self, request, instance, **kwargs): """ Handle the form if no "sent" GET argument (see the ``WIDGET_FORM_SENT_GET_PARAM`` setting). :param django.http.HttpRequest request: :param fobi.models.FormEntry instance: FormEntry instance. :return django.http.HttpResponse | str: """ template_name = instance.form_template_name or None # Handle public/non-public forms. If form requires user authentication # redirect to login form with next parameter set to current request # path. if not request.user.is_authenticated( ) and not instance.form_entry.is_public: if self.can_redirect: return redirect("{0}?next={1}".format(settings.LOGIN_URL, request.path)) else: return self._show_login_required_page(request, instance, **kwargs) form_element_entries = instance.form_entry.formelemententry_set.all( )[:] # This is where the most of the magic happens. Our form is being built # dynamically. FormClass = assemble_form_class( instance.form_entry, form_element_entries=form_element_entries, request=request) if 'POST' == request.method: form = FormClass(request.POST, request.FILES) # Fire pre form validation callbacks fire_form_callbacks(form_entry=instance.form_entry, request=request, form=form, stage=CALLBACK_BEFORE_FORM_VALIDATION) if form.is_valid(): # Fire form valid callbacks, before handling sufrom # django.http import HttpResponseRedirectbmitted plugin # form data form = fire_form_callbacks( form_entry=instance.form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID_BEFORE_SUBMIT_PLUGIN_FORM_DATA) # Fire plugin processors form = submit_plugin_form_data(form_entry=instance.form_entry, request=request, form=form) # Fire form valid callbacks form = fire_form_callbacks(form_entry=instance.form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID) # Run all handlers run_form_handlers(form_entry=instance.form_entry, request=request, form=form) # Fire post handler callbacks fire_form_callbacks( form_entry=instance.form_entry, request=request, form=form, stage=CALLBACK_FORM_VALID_AFTER_FORM_HANDLERS) messages.info( request, _('Form {0} was submitted ' 'successfully.').format(instance.form_entry.name)) if self.can_redirect: return redirect( "{0}?{1}={2}".format(request.path, \ self.form_sent_get_param, \ instance.form_entry.slug) ) else: return self._show_thanks_page(request, instance, **kwargs) else: # Fire post form validation callbacks fire_form_callbacks(form_entry=instance.form_entry, request=request, form=form, stage=CALLBACK_FORM_INVALID) else: # Providing initial form data by feeding entire GET dictionary # to the form, if ``GET_PARAM_INITIAL_DATA`` is present in the # GET. kwargs = {} if GET_PARAM_INITIAL_DATA in request.GET: kwargs = {'initial': request.GET} form = FormClass(**kwargs) theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_element_entries) context = { 'form': form, 'form_entry': instance.form_entry, 'fobi_theme': theme, 'fobi_form_title': instance.form_title, 'fobi_hide_form_title': instance.hide_form_title, 'fobi_form_submit_button_text': instance.form_submit_button_text } if not template_name: template_name = theme.view_embed_form_entry_ajax_template self.rendered_output = render_to_string( template_name, context, context_instance=RequestContext(request))