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 addSampleSurveysView(request):

    for i in range(3):
        ownedBy = request.user
        randomString = personalfunctions.generateRandom()

        templateObject = FormTemplate(name="survey"+str(i),
                                      url=randomString,
                                      commentDescription="description"+str(i),
                                      ownedBy=ownedBy)

        templateObject.save()

        for j in range(2):
            formField = FormField(descriptiveLabel="Question"+str(j),
                      fieldChoices='integer',
                      ownedByWhichTemplate=templateObject)
            formField.save()


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