def _test_form_action_url(self, form_entry, action_url): """Test form action URL.""" request_factory = RequestFactory() request = request_factory.post('/en/fobi/forms/edit/27/', data={ 'name': "John Doe", 'is_public': False, 'success_page_title': '', 'success_page_message': '', 'action': action_url, }) request.META['SERVER_NAME'] = 'localhost' form = FormEntryForm(request.POST, request=request, instance=form_entry) saved = False try: if form.is_valid(): form.save() saved = True except Exception as err: pass return saved
def _test_form_action_url(self, form_entry, action_url): request_factory = RequestFactory() request = request_factory.post( '/en/fobi/forms/edit/27/', data = { 'name': "John Doe", 'is_public': False, 'success_page_title': '', 'success_page_message': '', 'action': action_url, } ) request.META['SERVER_NAME'] = 'localhost' form = FormEntryForm(request.POST, request=request, instance=form_entry) saved = False try: if form.is_valid(): form.save() saved = True except Exception as err: pass return saved
def _test_form_action_url(self, form_entry, action_url): """Test form action URL.""" request_factory = RequestFactory() request = request_factory.post(reverse('fobi.edit_form_entry', args=[form_entry.pk]), data={ 'name': "John Doe", 'is_public': False, 'success_page_title': '', 'success_page_message': '', 'action': action_url, }) form = FormEntryForm(request.POST, request=request, instance=form_entry) saved = False try: if form.is_valid(): form.save() saved = True except Exception: pass return saved
def create_form_entry(request, theme=None, template_name=None): """ :param django.http.HttpRequest request: :return django.http.HttpResponse: """ if 'POST' == request.method: form = FormEntryForm(request.POST, request.FILES, request=request) if form.is_valid(): form_entry = form.save(commit=False) form_entry.user = request.user try: form_entry.save() messages.info( request, _('Form {0} was created successfully.').format( form_entry.name ) ) return redirect( 'fobi.edit_form_entry', form_entry_id=form_entry.pk ) except IntegrityError as err: messages.info( request, _('Errors occured while saving the form: {0}.').format( str(err) ) ) else: form = FormEntryForm(request=request) context = {'form': form} # If given, pass to the template (and override the value set by # the context processor. if theme: context.update({'fobi_theme': theme}) if not template_name: if not theme: theme = get_theme(request=request, as_instance=True) template_name = theme.create_form_entry_template return render_to_response( template_name, context, context_instance=RequestContext(request) )
def create_form_entry(request, theme=None, template_name=None): """ :param django.http.HttpRequest request: :param fobi.base.BaseTheme theme: Theme instance. :param str template_name: :return django.http.HttpResponse: """ if 'POST' == request.method: form = FormEntryForm(request.POST, request.FILES, request=request) if form.is_valid(): form_entry = form.save(commit=False) form_entry.user = request.user try: form_entry.save() messages.info( request, _('Form {0} was created ' 'successfully.').format(form_entry.name)) return redirect('fobi.edit_form_entry', form_entry_id=form_entry.pk) except IntegrityError as err: messages.info( request, _('Errors occured while saving ' 'the form: {0}.').format(str(err))) else: form = FormEntryForm(request=request) context = {'form': form} # If given, pass to the template (and override the value set by # the context processor. if theme: context.update({'fobi_theme': theme}) if not template_name: if not theme: theme = get_theme(request=request, as_instance=True) template_name = theme.create_form_entry_template return render_to_response(template_name, context, context_instance=RequestContext(request))
def edit_form_entry(request, form_entry_id, theme=None, template_name=None): """ Edit form entry. :param django.http.HttpRequest request: :param int form_entry_id: :param fobi.base.BaseTheme theme: Theme instance. :param str template_name: :return django.http.HttpResponse: """ try: form_entry = FormEntry._default_manager \ .select_related('user') \ .prefetch_related('formelemententry_set') \ .get(pk=form_entry_id, user__pk=request.user.pk) #.prefetch_related('formhandlerentry_set') \ except ObjectDoesNotExist as err: raise Http404(ugettext("Form entry not found.")) if 'POST' == request.method: # The form entry form (does not contain form elenments) form = FormEntryForm(request.POST, request.FILES, instance=form_entry, request=request) if 'ordering' in request.POST: form_element_entry_formset = FormElementEntryFormSet( request.POST, request.FILES, queryset=form_entry.formelemententry_set.all(), #prefix = 'form_element' ) # If form elements aren't properly made (developers's fault) # there might be problems with saving the ordering - likely # in case of hidden elements only. Thus, we want to avoid # errors here. try: if form_element_entry_formset.is_valid(): form_element_entry_formset.save() messages.info(request, _("Elements ordering edited successfully.")) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id})) except MultiValueDictKeyError as err: messages.error( request, _("Errors occured while trying to change the " "elements ordering!")) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id})) else: form_element_entry_formset = FormElementEntryFormSet( queryset=form_entry.formelemententry_set.all(), #prefix = 'form_element' ) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user try: obj.save() messages.info( request, _('Form {0} was edited ' 'successfully.').format(form_entry.name)) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id})) except IntegrityError as err: messages.info( request, _('Errors occured while saving ' 'the form: {0}.').format(str(err))) else: # The form entry form (does not contain form elenments) form = FormEntryForm(instance=form_entry, request=request) form_element_entry_formset = FormElementEntryFormSet( queryset=form_entry.formelemententry_set.all(), #prefix = 'form_element' ) # In case of success, we don't need this (since redirect would happen). # Thus, fetch only if needed. form_elements = form_entry.formelemententry_set.all() form_handlers = form_entry.formhandlerentry_set.all()[:] used_form_handler_uids = [form_handler.plugin_uid for form_handler \ in form_handlers] all_form_entries = FormEntry._default_manager.only('id', 'name', 'slug') \ .filter(user__pk=request.user.pk) # List of form element plugins allowed to user user_form_element_plugins = get_user_form_element_plugins_grouped( request.user) # List of form handler plugins allowed to user user_form_handler_plugins = get_user_form_handler_plugins( request.user, exclude_used_singles=True, used_form_handler_plugin_uids=used_form_handler_uids) # Assembling the form for preview FormClass = assemble_form_class( form_entry, origin='edit_form_entry', origin_kwargs_update_func=append_edit_and_delete_links_to_field, request=request) assembled_form = FormClass() # In debug mode, try to identify possible problems. if DEBUG: try: assembled_form.as_p() except Exception as err: logger.error(err) # If no theme provided, pick a default one. if not theme: theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_elements) context = { 'form': form, 'form_entry': form_entry, 'form_elements': form_elements, 'form_handlers': form_handlers, 'all_form_entries': all_form_entries, 'user_form_element_plugins': user_form_element_plugins, 'user_form_handler_plugins': user_form_handler_plugins, 'assembled_form': assembled_form, 'form_element_entry_formset': form_element_entry_formset, 'fobi_theme': theme, } if not template_name: template_name = theme.edit_form_entry_template return render_to_response(template_name, context, context_instance=RequestContext(request))
def edit_form_entry(request, form_entry_id, theme=None, template_name=None): """ Edit form entry. :param django.http.HttpRequest request: :param int form_entry_id: :param string template_name: :return django.http.HttpResponse: """ try: form_entry = FormEntry._default_manager \ .select_related('user') \ .prefetch_related('formelemententry_set') \ .get(pk=form_entry_id, user__pk=request.user.pk) #.prefetch_related('formhandlerentry_set') \ except ObjectDoesNotExist as err: raise Http404(ugettext("Form entry not found.")) if 'POST' == request.method: # The form entry form (does not contain form elenments) form = FormEntryForm(request.POST, request.FILES, instance=form_entry, request=request) if 'ordering' in request.POST: form_element_entry_formset = FormElementEntryFormSet( request.POST, request.FILES, queryset = form_entry.formelemententry_set.all(), #prefix = 'form_element' ) # If form elements aren't properly made (developers's fault) # there might be problems with saving the ordering - likely # in case of hidden elements only. Thus, we want to avoid # errors here. try: if form_element_entry_formset.is_valid(): form_element_entry_formset.save() messages.info( request, _("Elements ordering edited successfully.") ) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id}) ) except MultiValueDictKeyError as err: messages.error( request, _("Errors occured while trying to change the " "elements ordering!") ) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id}) ) else: form_element_entry_formset = FormElementEntryFormSet( queryset = form_entry.formelemententry_set.all(), #prefix = 'form_element' ) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user try: obj.save() messages.info( request, _('Form {0} was edited successfully.').format( form_entry.name ) ) return redirect( reverse('fobi.edit_form_entry', kwargs={'form_entry_id': form_entry_id}) ) except IntegrityError as err: messages.info( request, _('Errors occured while saving the form: {0}.').format( str(err) ) ) else: # The form entry form (does not contain form elenments) form = FormEntryForm(instance=form_entry, request=request) form_element_entry_formset = FormElementEntryFormSet( queryset = form_entry.formelemententry_set.all(), #prefix = 'form_element' ) # In case of success, we don't need this (since redirect would happen). # Thus, fetch only if needed. form_elements = form_entry.formelemententry_set.all() form_handlers = form_entry.formhandlerentry_set.all()[:] used_form_handler_uids = [form_handler.plugin_uid for form_handler \ in form_handlers] all_form_entries = FormEntry._default_manager.only('id', 'name', 'slug') \ .filter(user__pk=request.user.pk) # List of form element plugins allowed to user user_form_element_plugins = get_user_form_element_plugins_grouped( request.user ) # List of form handler plugins allowed to user user_form_handler_plugins = get_user_form_handler_plugins( request.user, exclude_used_singles = True, used_form_handler_plugin_uids = used_form_handler_uids ) # Assembling the form for preview FormClass = assemble_form_class( form_entry, origin = 'edit_form_entry', origin_kwargs_update_func = append_edit_and_delete_links_to_field, request = request ) assembled_form = FormClass() # In debug mode, try to identify possible problems. if DEBUG: try: assembled_form.as_p() except Exception as err: logger.error(err) # If no theme provided, pick a default one. if not theme: theme = get_theme(request=request, as_instance=True) theme.collect_plugin_media(form_elements) context = { 'form': form, 'form_entry': form_entry, 'form_elements': form_elements, 'form_handlers': form_handlers, 'all_form_entries': all_form_entries, 'user_form_element_plugins': user_form_element_plugins, 'user_form_handler_plugins': user_form_handler_plugins, 'assembled_form': assembled_form, 'form_element_entry_formset': form_element_entry_formset, 'fobi_theme': theme, } if not template_name: template_name = theme.edit_form_entry_template return render_to_response( template_name, context, context_instance=RequestContext(request) )