def profile(request): user=request.user #retrieve or create the UserProfile object for the user try: up = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: up = UserProfile(user=request.user) up.save() return render(request, 'registration/profile.html', {'user':user, 'userProfile':up})
def showSurvey(request, companyId, questionNumber): errors = [] # pull out company, survey, current question, and the number of questions c = Company.objects.get(id=companyId) s = c.survey q = s.questions.get(number=questionNumber) maxQ = int(s.questions.all().aggregate(Max('number'))['number__max']) # change button text to submit on the last question if maxQ == q.number: lastQ=True submitText='submit' else: lastQ=False submitText='next' if request.method == 'POST': if 'answer' in request.POST: a = request.POST['answer'] if not a: errors.append('Please answer the question below.') else: # retrieve or create the UserProfile for this user # and put the survey on the object try: up = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: surveys = [s] import pdb; pdb.set_trace() up = UserProfile(user=request.user) up.save() ans = request.POST['answer'] import pdb; pdb.set_trace() # save the user's response r = Response(question=q, survey=s, user=up, answer=ans) r.save() # once saved, move on to next question following same url convention try: nextQ = s.questions.get(number=(q.number+1)) except ObjectDoesNotExist: return HttpResponseRedirect("/thankyou/") return HttpResponseRedirect("/user-survey/"+str(c.id)+"/"+str(nextQ.id)+"/") return render(request, 'show_survey.html', {'errors':errors, 'survey':s, 'company':c, 'question':q, 'submitText':submitText})