def preview_app_def(request): """ This renders the application definition form without creating submit buttons """ if request.method == 'GET': if request.GET.has_key('ad'): ad = get_object_or_404(ApplicationDefinition, id=int(request.GET['ad'])) else: raise http.Http404, _('Form Definition does not exist') else: raise http.Http404, _('Form Definition does not exist') form = ApplicationForm(ad) # create structure for the template that looks like # form-> (group, pre-text, post-text, page) fields = [] for field in ad.fielddefinition_set.filter(reviewer_only=False): field_dict = {'group': field.group, 'pre_text': mark_safe(field.pre_text), 'post_text': mark_safe(field.post_text), 'field': form.__getitem__(field.slug),}, fields += field_dict # Build sub forms based on sub application definitions subforms = [] if ad.applicationdefinition_set.all(): sub_apps = ad.applicationdefinition_set.all() for sub_app in sub_apps: sub_ad = sub_app.subapplicationdefinition_set.get() sub_app_formset = formunculous_subformset_factory(ApplicationForm, formset=FormunculousBaseFormSet, extra=sub_ad.extras, max_num = sub_ad.max_entries) formset = sub_app_formset(app_def=sub_app, prefix=sub_app.slug) forms = [] for sub_form in formset.forms: forms.append({"form": sub_form, "fields": build_template_structure(sub_form, sub_app) }) subforms.append({ "sub_ad": sub_app, "forms": forms, "formset": formset}) # Try a customized template. # if it is there use it, else use the default template. try: t = template.loader.get_template('formunculous/%s/apply.html' % ad.slug) t = 'formunculous/%s/apply.html' % ad.slug except: t = 'formunculous/apply.html' return render_to_response('formunculous/apply.html', {'form': form, 'ad': ad, 'fields': fields, 'subforms': subforms, 'preview': True,}, context_instance=template.RequestContext(request))
def get_context_data(self, **kwargs): context = super(self.__class__, self).get_context_data(**kwargs) if self.request.GET.has_key('ad'): ad = get_object_or_404(ApplicationDefinition, id=int(self.request.GET['ad'])) else: raise http.Http404, _('Form Definition does not exist') form = ApplicationForm(ad) # create structure for the template that looks like # form-> (group, pre-text, post-text, page) fields = [] for field in ad.fielddefinition_set.filter(reviewer_only=False): field_dict = {'group': field.group, 'pre_text': mark_safe(field.pre_text), 'post_text': mark_safe(field.post_text), 'field': form.__getitem__(field.slug),}, fields += field_dict # Build sub forms based on sub application definitions subforms = [] if ad.applicationdefinition_set.all(): sub_apps = ad.applicationdefinition_set.all() for sub_app in sub_apps: sub_ad = sub_app.subapplicationdefinition_set.get() sub_app_formset = formunculous_subformset_factory(ApplicationForm, formset=FormunculousBaseFormSet, extra=sub_ad.extras, max_num=sub_ad.max_entries) formset = sub_app_formset(app_def=sub_app, prefix=sub_app.slug) forms = [] for sub_form in formset.forms: forms.append({"form": sub_form, "fields": build_template_structure(sub_form, sub_app) }) subforms.append({ "sub_ad": sub_app, "forms": forms, "formset": formset}) # Try a customized template. # if it is there use it, else use the default template. try: self.template_name = template.loader.get_template( 'formunculous/{0}/apply.html'.format(ad.slug)) except: self.template_name = 'formunculous/apply.html' context['form'] = form context['ad'] = ad context['fields'] = fields context['subforms'] = subforms context['preview'] = True return context
def application(request, slug, app): """ The reviewer view of a specific application. Displays and handles a form for the reviewer only fields, and displays comments. """ ad = get_object_or_404(ApplicationDefinition, slug=slug) app = get_object_or_404(Application, id=app) validation = validate_user(request, ad) if validation: return validation breadcrumbs = [{'url': reverse('formunculous-index'), 'name': _('Form Index')}, {'url': reverse('reviewer-index', kwargs={'slug': slug}), 'name': ad.name}, ] message = '' if request.method == 'POST': if request.POST.has_key('save'): rev_form = ApplicationForm(ad, app, True, request.POST, request.FILES) if rev_form.is_valid(): rev_form.save() request.session['message'] = _('Form Data Saved') return redirect("reviewer-application", slug=slug, app=app.id) rev_form = ApplicationForm(ad, app, True) fields = build_template_structure(rev_form, ad, True) if 'message' in request.session: message = request.session['message'] del request.session['message'] sub_apps = get_sub_app_fields(app) return render_to_response('formunculous/review_application.html', {'ad': ad, 'app': app, 'app_fields': app.get_field_values(), 'sub_apps': sub_apps, 'review_form': rev_form, 'fields': fields, 'breadcrumbs': breadcrumbs, 'message': message, }, context_instance=template.RequestContext(request))
def application(request, slug, app): """ The reviewer view of a specific application. Displays and handles a form for the reviewer only fields, and displays comments. """ ad = get_object_or_404(ApplicationDefinition, slug=slug) app = get_object_or_404(Application, id=app) validation = validate_user(request, ad) if validation: return validation breadcrumbs = [ {"url": reverse("formunculous-index"), "name": _("Form Index")}, {"url": reverse("reviewer-index", kwargs={"slug": slug}), "name": ad.name}, ] message = "" if request.method == "POST": if request.POST.has_key("save"): rev_form = ApplicationForm(ad, app, True, request.POST, request.FILES) if rev_form.is_valid(): rev_form.save() request.session["message"] = _("Form Data Saved") return redirect("reviewer-application", slug=slug, app=app.id) rev_form = ApplicationForm(ad, app, True) fields = build_template_structure(rev_form, ad, True) if "message" in request.session: message = request.session["message"] del request.session["message"] sub_apps = get_sub_app_fields(app) return render_to_response( "formunculous/review_application.html", { "ad": ad, "app": app, "app_fields": app.get_field_values(), "sub_apps": sub_apps, "review_form": rev_form, "fields": fields, "breadcrumbs": breadcrumbs, "message": message, }, context_instance=template.RequestContext(request), )
def render(self, context): if self.ad == None: return _("Form requested is unavailable") ad = self.ad form = None app = None if datetime.datetime.now() < ad.start_date \ or datetime.datetime.now() > ad.stop_date: return _("This form is not active") if ad.applicationdefinition_set.all(): return _("This tag does not support forms that have sub-forms") if ad.authentication: return _("This tag does not support authenticated forms") if not form: form = ApplicationForm(ad, app) fields = build_template_structure(form, ad) # Try a customized template. # if it is there use it, else use the default template. try: t = template.loader.get_template('formunculous/%s/apply.html' % ad.slug) t = 'formunculous/%s/tag_apply.html' % ad.slug except: t = 'formunculous/tag_apply.html' rendered = render_to_string( t, {'form': form, 'ad': ad, 'fields': fields,}, context_instance=context) if self.var_name: context[self.var_name] = rendered return '' else: return rendered
def apply(request, slug): """ This is the primary form view. It handles displaying the form defined by the slug, and redirecting to either of the completion states. There are two primary branches, authenticated or unauthenticated. If the app is authenticated it searches for an existing partial or full application from the user for the slug definition. If it finds one, it displays either the partially completed form in an editable state, or a completion page if it is complete. """ form = None app = None formsets = None history = None breadcrumbs = [{'url': reverse('formunculous-index'), 'name': _('Forms')},] ad = get_object_or_404(ApplicationDefinition, slug=slug) if ad.parent: raise http.Http404, _("This form doesn't exist") if timezone.now() < ad.start_date or timezone.now() > ad.stop_date: raise http.Http404, _('This form is not active') # Require auth and redirect if ad.authentication: if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (reverse('formunculous-login'), request.path)) # Grab the most recent app if it already exists. try: app = Application.objects.filter( user__username__exact=request.user.username, app_definition=ad).order_by('id').reverse()[0] except (Application.DoesNotExist, IndexError): pass # Got the app, if it is already submitted, render a different # template that displays the application's data unless the # application definition allows multiple submissions and the # user has requested a new instance. if app: new = False if request.GET.has_key('new'): new = True if app.submission_date and \ not (new and ad.authentication_multi_submit): # Load a custom template if it exists. try: t = template.loader.get_template( 'formunculous/%s/completed.html' % ad.slug) t = 'formunculous/%s/completed.html' % ad.slug except: t = 'formunculous/completed.html' sub_apps = get_sub_app_fields(app) # If there are previous apps and this is a multi-auth # form, populate <history> with them if ad.authentication_multi_submit: try: apps_history = Application.objects.filter( user__username__exact=request.user.username, app_definition=ad).order_by('id').reverse()[1:] if apps_history.count() > 0: history = apps_history except: history = None return render_to_response(t, {'ad': ad, 'app': app, 'fields': app.get_field_values(), 'sub_apps': sub_apps, 'breadcrumbs': breadcrumbs, 'history': history, }, context_instance=template.RequestContext(request)) # If this is a new request and the existing app is finished, # create an additional app instance. if new and ad.authentication_multi_submit\ and app.submission_date: app = Application(user = request.user, app_definition = ad) app.save() # Begin form post processing. message = '' if request.method == 'POST': if request.POST.has_key('save'): # If this is the first save, create the app if not app: app = Application(app_definition = ad, user = request.user) app.save() form = ApplicationForm(ad, app, False, request.POST, request.FILES) if ad.applicationdefinition_set.all(): formsets = get_formsets(ad, request, request.user) valid = True if not form.is_valid(): valid = False if not validate_formsets(formsets): valid = False if valid: form.save() # Save formsets if formsets: save_formsets(formsets, form.app, request.user) request.session['message'] = _('Form Data Saved') # Redirect to prevent repost return redirect("formunculous-apply", slug=slug) # If final submission, save form and redirect to the confirmation # page. elif request.POST.has_key('submit'): # If the app doesn't exist yet, create it if not app: user = None if ad.authentication: user = request.user # Create the instance of the app app = Application(app_definition = ad, user = user) else: user = app.user form = ApplicationForm(ad, app, False, request.POST, request.FILES) # Walk through and grab the subapp formsets if ad.applicationdefinition_set.all(): formsets = get_formsets(ad, request, user) # Check for required fields, and show errors before # redirect # Check for base form validity valid = True if not (form.is_valid() and form.check_required()): valid = False if not fully_validate_formsets(formsets): valid = False if valid: form.save() # Save subapps if there are any if formsets: save_formsets(formsets, form.app, user) # Redirect to confirmation or thank you page if ad.authentication: return redirect("formunculous-confirm", slug=slug, app=app.id) else: # Notify here, so refreshing doesn't resend notification notify_reviewers(request, ad, app) return redirect("formunculous-thankyou",slug=slug, app=app.id) # Grab form from formunculous.forms if not form: form = ApplicationForm(ad, app) fields = build_template_structure(form, ad) # Build user for use in subapps user = None if request.user and ad.authentication: user = request.user # Build sub forms based on sub application definitions if not formsets: formsets = [] if ad.applicationdefinition_set.all(): sub_apps = ad.applicationdefinition_set.all() for sub_app in sub_apps: sub_ad = sub_app.subapplicationdefinition_set.get() sub_app_formset = formunculous_subformset_factory(ApplicationForm, formset=FormunculousBaseFormSet, extra=sub_ad.extras, max_num = sub_ad.max_entries,) formset = sub_app_formset(app_def=sub_app, parent=app,user=user, minimum=sub_ad.min_entries, prefix=sub_app.slug) formsets.append(formset) subforms = [] for formset in formsets: forms = [] for sub_form in formset.forms: forms.append({"form": sub_form, "fields": build_template_structure(sub_form, formset.app_def)}) subforms.append({ "sub_ad": formset.app_def, "forms": forms, "formset": formset}) # Try a customized template. # if it is there use it, else use the default template. try: t = template.loader.get_template('formunculous/%s/apply.html' % ad.slug) t = 'formunculous/%s/apply.html' % ad.slug except: t = 'formunculous/apply.html' if 'message' in request.session: message = request.session['message'] del request.session['message'] # If there are previous apps and this is a multi-auth # form, populate <history> with them if ad.authentication_multi_submit: try: apps_history = Application.objects.filter( user__username__exact=request.user.username, app_definition=ad, ).exclude(submission_date=None).order_by('id').reverse() if apps_history.count() > 0: history = apps_history except: history = None return render_to_response(t, {'form': form, 'ad': ad, 'fields': fields, 'subforms': subforms, 'message': message, 'breadcrumbs': breadcrumbs, 'history': history, }, context_instance=template.RequestContext(request))