def run(self, form_entry, request, form, form_element_entries=None): """ :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: :param iterable form_element_entries: Iterable of ``fobi.models.FormElementEntry`` objects. """ # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries ) for key, value in cleaned_data.items(): if isinstance(value, (datetime.datetime, datetime.date)): cleaned_data[key] = value.isoformat() if hasattr(value, 'isoformat') else value saved_form_data_entry = SavedFormDataEntry( form_entry = form_entry, user = request.user if request.user and request.user.pk else None, form_data_headers = json.dumps(field_name_to_label_map), saved_data = json.dumps(cleaned_data) ) saved_form_data_entry.save()
def callback(self, form_entry, request, form): """Callback. :param form_entry: :param request: :param form: :return: """ form_element_entries = form_entry.formelemententry_set.all() # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries ) for key, value in cleaned_data.items(): if isinstance(value, (datetime.datetime, datetime.date)): cleaned_data[key] = value.isoformat() \ if hasattr(value, 'isoformat') \ else value saved_form_data_entry = SavedFormDataEntry( form_entry=form_entry, user=request.user if request.user and request.user.pk else None, form_data_headers=json.dumps(field_name_to_label_map), saved_data=json.dumps(cleaned_data) ) saved_form_data_entry.save()
def run(self, form_entry, request, form, form_element_entries=None): """Run. :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: :param iterable form_element_entries: Iterable of ``fobi.models.FormElementEntry`` objects. """ # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries) for key, value in cleaned_data.items(): if isinstance(value, (datetime.datetime, datetime.date)): cleaned_data[key] = value.isoformat() \ if hasattr(value, 'isoformat') \ else value saved_form_data_entry = SavedFormDataEntry( form_entry=form_entry, user=request.user if request.user and request.user.pk else None, form_data_headers=json.dumps(field_name_to_label_map), saved_data=json.dumps(cleaned_data)) saved_form_data_entry.save()
def run(self, form_entry, request, form, form_element_entries=None): """ :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: :param iterable form_element_entries: Iterable of ``fobi.models.FormElementEntry`` objects. """ base_url = 'http{secure}://{host}'.format( secure=('s' if request.is_secure() else ''), host=request.get_host()) # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries) rendered_data = [] for key, value in cleaned_data.items(): if value and isinstance(value, string_types) and value.startswith( settings.MEDIA_URL): cleaned_data[key] = '{base_url}{value}'.format( base_url=base_url, value=value) label = field_name_to_label_map.get(key, key) rendered_data.append('{0}: {1}\n'.format( safe_text(label), safe_text(cleaned_data[key]))) send_mail(safe_text(self.data.subject), "{0}\n\n{1}".format(safe_text(self.data.body), ''.join(rendered_data)), self.data.from_email, [self.data.to_email], fail_silently=True)
def run(self, form_entry, request, form, form_element_entries=None): """ :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: :param iterable form_element_entries: Iterable of ``fobi.models.FormElementEntry`` objects. """ base_url = 'http{secure}://{host}'.format( secure = ('s' if request.is_secure() else ''), host = request.get_host() ) # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries ) rendered_data = [] for key, value in cleaned_data.items(): if value and isinstance(value, string_types) \ and value.startswith(settings.MEDIA_URL): cleaned_data[key] = '{base_url}{value}'.format( base_url=base_url, value=value ) label = field_name_to_label_map.get(key, key) rendered_data.append('{0}: {1}\n'.format( safe_text(label), safe_text(cleaned_data[key])) ) files = self._prepare_files(request, form) # Handling more than one email address if isinstance(self.data.to_email, (list, tuple)): to_email = self.data.to_email else: # Assume that it's string to_email = self.data.to_email.split(MULTI_EMAIL_FIELD_VALUE_SPLITTER) send_mail( safe_text(self.data.subject), "{0}\n\n{1}".format(safe_text(self.data.body), ''.join(rendered_data)), self.data.from_email, to_email, fail_silently = False, attachments = files.values() )
def run(self, form_entry, request, form, form_element_entries=None): """Run. :param fobi.models.FormEntry form_entry: Instance of ``fobi.models.FormEntry``. :param django.http.HttpRequest request: :param django.forms.Form form: :param iterable form_element_entries: Iterable of ``fobi.models.FormElementEntry`` objects. """ base_url = 'http{secure}://{host}'.format( secure=('s' if request.is_secure() else ''), host=request.get_host()) # Clean up the values, leave our content fields and empty values. field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries) rendered_data = [] for key, value in cleaned_data.items(): if value and isinstance(value, string_types) \ and value.startswith(settings.MEDIA_URL): cleaned_data[key] = '{base_url}{value}'.format( base_url=base_url, value=value) label = field_name_to_label_map.get(key, key) rendered_data.append('{0}: {1}\n'.format( safe_text(label), safe_text(cleaned_data[key]))) files = self._prepare_files(request, form) # Handling more than one email address if isinstance(self.data.to_email, (list, tuple)): to_email = self.data.to_email else: # Assume that it's string to_email = self.data.to_email.split( MULTI_EMAIL_FIELD_VALUE_SPLITTER) send_mail(safe_text(self.data.subject), "{0}\n\n{1}".format(safe_text(self.data.body), ''.join(rendered_data)), self.data.from_email, to_email, fail_silently=False, attachments=files.values())
def inscripcion_extra(request): m = request.GET.get('m') text = request.GET.get('text') try: idInscripto = decode_data(m, text) except: messages.error(request, u'La url no es correcta.') return HttpResponseRedirect(reverse('inicio')) inscripto = get_object_or_404(InscripcionBase, pk=idInscripto) if inscripto.datos is not None: messages.error(request, u'Ya ha completado los datos extras.') return HttpResponseRedirect(reverse('inicio')) form_entry = inscripto.actividad.formDinamico form_element_entries = form_entry.formelemententry_set.all()[:] print(form_element_entries) # 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) if form.is_valid(): field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries ) for key, value in cleaned_data.items(): if isinstance(value, (datetime.datetime, datetime.date)): cleaned_data[key] = value.isoformat() if hasattr(value, 'isoformat') else value inscripto.datos = json.dumps(cleaned_data) inscripto.save() mensaje = u'Su solicitud ha sido procesada con éxito. Gracias por inscribirse' messages.success(request, mensaje) return HttpResponseRedirect(reverse('inicio')) 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) context = { 'form': form, 'inscripto': inscripto, } return render_to_response('formextra.html', context, context_instance=RequestContext(request))
def inscripcion_extra(request): m = request.GET.get('m') text = request.GET.get('text') try: idInscripto = decode_data(m, text) except: messages.error(request, u'La url no es correcta.') return HttpResponseRedirect(reverse('inicio')) inscripto = get_object_or_404(InscripcionBase, pk=idInscripto) if inscripto.datos is not None: messages.error(request, u'Ya ha completado los datos extras.') return HttpResponseRedirect(reverse('inicio')) form_entry = inscripto.actividad.formDinamico form_element_entries = form_entry.formelemententry_set.all()[:] print(form_element_entries) # 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) if form.is_valid(): field_name_to_label_map, cleaned_data = get_processed_form_data( form, form_element_entries) for key, value in cleaned_data.items(): if isinstance(value, (datetime.datetime, datetime.date)): cleaned_data[key] = value.isoformat() if hasattr( value, 'isoformat') else value inscripto.datos = json.dumps(cleaned_data) inscripto.save() mensaje = u'Su solicitud ha sido procesada con éxito. Gracias por inscribirse' messages.success(request, mensaje) return HttpResponseRedirect(reverse('inicio')) 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) context = { 'form': form, 'inscripto': inscripto, } return render_to_response('formextra.html', context, context_instance=RequestContext(request))