Beispiel #1
0
def dataAddView(request):
    """
    View function to add a survey and handle a POST for survey data.
    """
    theContext = {}
    theContext["wrongCredentials"] = 1

    theContext["whatData"] = "events"

    if request.method == "POST":

        fieldFormSet = formset_factory(AddFormFieldFormx)
        templateForm = AddFormTemplateFormx(request.POST)
        formset = fieldFormSet(request.POST)

        if templateForm.is_valid() and formset.is_valid():
            # creating and saving the webform object.
            templateObject = templateForm.save(commit=False)
            templateObject.ownedBy = request.user
            randomString = personalfunctions.generateRandom()
            templateObject.url = randomString
            print 'http://localhost:8000/preview_templates/'+randomString
            templateObject.save()

            for form in formset.forms:
                fieldObject = form.save(commit=False)
                fieldObject.ownedByWhichTemplate = templateObject
                fieldObject.save()

            home_cache_key = "%s-home" % request.user
            cache.set(home_cache_key, request.user.formtemplate_set.all())
            return HttpResponseRedirect(urlresolvers.reverse("surveyApp:ViewIndex"))

        else:
            theContext["wrongCredentials"] = -1
            print "This is the form invalid", templateForm.as_p()

    else:
        fieldFormSet = formset_factory(AddFormFieldFormx, extra=1)
        formset = fieldFormSet()
        templateForm = AddFormTemplateFormx()

    theContext['fieldNames'] = [i.name for i in FormField._meta.fields[1:-1]]
    theContext["templateform"] = templateForm
    theContext['formset'] = formset
    return render(request, "surveyApp/formTemplateAdd.html", theContext)
Beispiel #2
0
def IDView(request, TEMPLATE_ID):

    events = request.user.formtemplate_set.all()
    theContext = {}

    # note that TEMPLATE_ID is a string variable and not an int
    if int(TEMPLATE_ID) <= len(events) and int(TEMPLATE_ID) > 0:

        display = None

        selectedData = events[int(TEMPLATE_ID)-1]
        TemplateFormSet = inlineformset_factory(FormTemplate, FormField, extra=1)

        if request.method == "POST":

            formset = TemplateFormSet(request.POST, instance=selectedData)
            templateForm = AddFormTemplateFormx(request.POST, instance=selectedData)

            if formset.is_valid() and templateForm.is_valid():

                formset.save()
                templateForm.save()
                display = "You have successfully updated the event !"

                home_cache_key = "%s-home" % request.user
                cache.set(home_cache_key, request.user.formtemplate_set.all())

                return HttpResponseRedirect(urlresolvers.reverse("surveyApp:ViewID", args=(TEMPLATE_ID,)))

        else:
            templateForm = AddFormTemplateFormx(instance=selectedData)
            formset = TemplateFormSet(instance=selectedData)

    else:
        return HttpResponseNotFound("<h1>Sorry page not found ! </h1>")


    theContext["formset"] = formset
    theContext["templateForm"] = templateForm
    theContext["display"] = display
    theContext["TemplateID"] = TEMPLATE_ID

    return render(request, "surveyApp/templatesID.html", theContext)