def edit(request, id): """ 编辑投票项的基础信息 """ obj = get_object_or_404(model_class, id=id) form = SurveyForm(instance=obj) if request.method == "POST": data=request.POST form = SurveyForm(data,instance=obj) if form.is_valid(): o = form.save() return HttpResponseRedirect(reverse("table_detail", args=[obj.pk])) return HttpResponse(Template("{{ form }}").render(Context({'form': form})))
def edit(request, id): """ 编辑投票项的基础信息 """ obj = get_object_or_404(model_class, id=id) form = SurveyForm(instance=obj) if request.method == "POST": data = request.POST form = SurveyForm(data, instance=obj) if form.is_valid(): o = form.save() return HttpResponseRedirect(reverse("table_detail", args=[obj.pk])) return HttpResponse(Template("{{ form }}").render(Context({'form': form})))
def add_survey(request): if request.method == "POST": form = SurveyForm(request.POST) if form.is_valid(): form.save(commit=True) data = Survey.objects.all() return render(request,'survey/survey.html',{ 'data':data, 'success_added':1 }) else: return render(request,'survey/survey_form.html',{'form':form,'error':1}) form = SurveyForm() return render(request,'survey/survey_form.html',{'form':form})
def create_survey(request): """ Allows the logged in User to Create new Surveys. """ context = {} if request.method == "GET": form = SurveyForm() context["form"] = form if request.method == "POST": form = SurveyForm(request.POST) if form.is_valid(): pass else: # logger.info("{user} accessed create survey page".format(user=request.user.get_full_name())) return render(request, "create_survey.html", context)
def survey(request): # Save data or display form if request.method == 'POST': survey_form = SurveyForm(request.POST) if survey_form.is_valid(): survey_form.save() # Redirect return render_to_response( 'survey_complete.html', context_instance=RequestContext(request), ) else: survey_form = SurveyForm() # Display form return render_to_response( 'survey.html', {'form': survey_form}, RequestContext(request), )
def mine_survey_view(request): if request.method == 'GET': user = request.user data_dict = {} data_dict['survey'] = SurveyForm(); panels = Panel.objects.filter(user = user) if len(panels) > 1: return HttpResponseRedirect("/thanks") if UserSurvey.objects.filter(user_profile = request.user.userprofile):#request.user.userprofile.usersurvey: return HttpResponseRedirect("/wait_room") return render(request, 'survey.html', data_dict) else: # if UserSurvey.objects.filter(user_profile = request.user.userprofile):#request.user.userprofile.usersurvey: # return HttpResponseRedirect("/wait_room")# HttpResponse('Already there') profile = UserSurvey(user_profile = request.user.userprofile) f = SurveyForm(request.POST, instance = profile) if f.is_valid(): f.save() return HttpResponseRedirect("/wait_room") error_dict = {} error_dict['error'] = "Form invalid!" error_dict['relocation'] = "/mine_survey" return render(request, 'error.html', error_dict)
def processSurvey(request, adminView=False, adminViewId=None, userName=None): NatLangFormset = formset_factory( NativeLangForm, min_num=1, validate_min=True, extra=0, can_delete=True, formset=BaseLangFormSet, ) HerLangFormset = formset_factory( HeritageLangForm, can_delete=True, formset=BaseLangFormSet, ) ForLangFormset = formset_factory( ForeignLangForm, can_delete=True, formset=BaseLangFormSet, ) if request.method == 'POST': form = SurveyForm(request.POST) natLangsForms = NatLangFormset(request.POST, request.FILES, prefix=u"natLang") herLangsForms = HerLangFormset(request.POST, request.FILES, prefix=u"herLang") forLangsForms = ForLangFormset(request.POST, request.FILES, prefix=u"forLang") mainFormValid = form.is_valid() # see if the foreign languages were visible and need validation forLangBoolVal = form.cleaned_data.get("foreignLangBool") herLangBoolVal = form.cleaned_data.get("heritageLangBool") if not forLangBoolVal: for flf in forLangsForms: flf.needsValidation = False if not herLangBoolVal: for hlf in herLangsForms: hlf.needsValidation = False natLangsValid = natLangsForms.is_valid() forLangsValid = forLangsForms.is_valid() herLangsValid = herLangsForms.is_valid() if not forLangBoolVal: # post is valid but we won't be saving any data for it forLangsValid = True if not herLangBoolVal: herLangsValid = True if mainFormValid and natLangsValid and herLangsValid and forLangsValid: data = form.cleaned_data # see if admin is updating or if participant is posting new surveyLine = None try: if adminView: surveyLine = SurveyLine.objects.get(id=adminViewId) else: surveyLine = SurveyLine.objects.get(userName=userName) except ObjectDoesNotExist: pass if surveyLine is not None: # we are updating a pre-existing entry # first is the model attribute, second is the form attribute attrs = [ "age", "gender", "education", "undergradLevel", "foreignLangBool", "heritageLangBool", "visionProblems", "visionProblemsDetails", "hearingProblems", "hearingProblemsDetails", ] for att in attrs: setattr(surveyLine, att, data[att]) surveyLine.dateLastEdited = timezone.now() if adminView and "adminComment" in data: surveyLine.adminComment = data["adminComment"] # It's going to be easier to delete than to update nlEntry = surveyLine.nativelangline_set.all() hlEntry = surveyLine.heritagelangline_set.all() flEntry = surveyLine.foreignlangline_set.all() for lq in [nlEntry, hlEntry, flEntry]: for le in lq: le.delete() surveyLine.save() else: # we are creating a new entry surveyLine = SurveyLine( userName=request.user, date=timezone.now(), age=data['age'], gender=data["gender"], education=data['education'], undergradLevel=data['undergradLevel'], visionProblems=data['visionProblems'], visionProblemsDetails=data['visionProblemsDetails'], hearingProblems=data['hearingProblems'], hearingProblemsDetails=data['hearingProblemsDetails'], foreignLangBool=data['foreignLangBool'], heritageLangBool=data['heritageLangBool'], ) surveyLine.save() for natLangForm in natLangsForms: data = natLangForm.cleaned_data if not data["DELETE"]: natLangLine = NativeLangLine( surveyId=surveyLine, nativeLang=data["nativeLang"], ) natLangLine.save() if forLangBoolVal: for forLangForm in forLangsForms: data = forLangForm.cleaned_data if not data["DELETE"]: forLangLine = ForeignLangLine( surveyId=surveyLine, foreignLang=data["foreignLang"], proficiency=data["proficiency"], school=data["school"], lived=data["lived"], worked=data["worked"], other=data["other"], schoolSemestersTotal=forLangForm.schoolTotal, livedDaysTotal=forLangForm.livedTotal, workedDaysTotal=forLangForm.workedTotal, otherDaysTotal=forLangForm.otherTotal, schoolSemesters=data["schoolSemesters"], schoolYears=data["schoolYears"], livedYears=data["livedYears"], workedYears=data["workedYears"], otherYears=data["otherYears"], livedMonths=data["livedMonths"], workedMonths=data["workedMonths"], otherMonths=data["otherMonths"], livedWeeks=data["livedWeeks"], workedWeeks=data["workedWeeks"], otherWeeks=data["otherWeeks"], livedDays=data["livedDays"], workedDays=data["workedDays"], otherDays=data["otherDays"], ) otherDesc = data.get("otherStudyExplanation") if otherDesc is not None: forLangLine.otherDescription = otherDesc forLangLine.save() if herLangBoolVal: for herLangForm in herLangsForms: data = herLangForm.cleaned_data if not data["DELETE"]: herLangLine = HeritageLangLine( surveyId=surveyLine, heritageLang=data["heritageLang"], explanation=data["explanation"], ) herLangLine.save() if adminView: compTemp = "editSuccessful.html" else: compTemp = "completed.html" return request, compTemp, {} else: logger.info('Form is not valid') # the "if" before this was if == "POST" else: entry = None try: if adminView: entry = get_object_or_404(SurveyLine, id=adminViewId) else: # see if we have an entry in the database entry = SurveyLine.objects.get(userName=userName) except ObjectDoesNotExist: pass if entry is not None: initial = { "adminComment": entry.adminComment, "age": entry.age, "gender": entry.gender, "education": entry.education, "undergradLevel": entry.undergradLevel, "foreignLangBool": entry.foreignLangBool, "heritageLangBool": entry.heritageLangBool, "visionProblems": entry.visionProblems, "visionProblemsDetails": entry.visionProblemsDetails, "hearingProblems": entry.hearingProblems, "hearingProblemsDetails": entry.hearingProblemsDetails, } form = SurveyForm(initial=initial) # nativeLangEntries nles = entry.nativelangline_set.all() nlInitials = [] for nle in nles: nlInitial = { "nativeLang": nle.nativeLang, } nlInitials.append(nlInitial) # foreignLangEntries fles = entry.foreignlangline_set.all() if fles: # overwrite initial factory to set extra=0 ForLangFormset = formset_factory( ForeignLangForm, can_delete=True, formset=BaseLangFormSet, extra=0, ) flInitials = [] for fle in fles: flInitial = { "foreignLang": fle.foreignLang, "proficiency": fle.proficiency, "school": fle.school, "lived": fle.lived, "worked": fle.worked, "other": fle.other, "schoolSemesters": fle.schoolSemesters, "livedDays": fle.livedDays, "workedDays": fle.workedDays, "otherDays": fle.otherDays, "otherStudyExplanation": fle.otherDescription, "schoolYears": fle.schoolYears, "livedYears": fle.livedYears, "workedYears": fle.workedYears, "otherYears": fle.otherYears, "livedMonths": fle.livedMonths, "workedMonths": fle.workedMonths, "otherMonths": fle.otherMonths, "livedWeeks": fle.livedWeeks, "workedWeeks": fle.workedWeeks, "otherWeeks": fle.otherWeeks, "livedDays": fle.livedDays, "workedDays": fle.workedDays, "otherDays": fle.otherDays, } flInitials.append(flInitial) hles = entry.heritagelangline_set.all() hlInitials = [] if hles: HerLangFormset = formset_factory( HeritageLangForm, can_delete=True, formset=BaseLangFormSet, extra=0, ) hlInitials = [] for hle in hles: hlInitial = { "heritageLang": hle.heritageLang, "explanation": hle.explanation, } hlInitials.append(hlInitial) natLangsForms = NatLangFormset(prefix=u"natLang", initial=nlInitials) forLangsForms = ForLangFormset(prefix=u"forLang", initial=flInitials) herLangsForms = HerLangFormset(prefix=u"herLang", initial=hlInitials) # we have no entry, give them empty form else: form = SurveyForm() natLangsForms = NatLangFormset(prefix=u"natLang") herLangsForms = HerLangFormset(prefix=u"herLang") forLangsForms = ForLangFormset(prefix=u"forLang") argsDict = { 'form': form, 'natLangsForms': natLangsForms, 'herLangsForms': herLangsForms, 'forLangsForms': forLangsForms, } return request, "survey.html", argsDict