def survey_response_change(request, id): """Update survey question for the logged in user **Attributes**: * ``form`` - SurveyQuestionForm * ``template`` - frontend/survey/survey_question.html **Logic Description**: * """ survey_resp = SurveyResponse.objects.get(pk=int(id)) form = SurveyResponseForm(request.user, survey_resp.surveyquestion.surveyapp_id, instance=survey_resp) request.session['err_msg'] = '' if request.GET.get('delete'): # perform delete surveyapp_id = survey_resp.surveyquestion.surveyapp_id survey_resp.delete() return HttpResponseRedirect('/survey/%s/' % str(surveyapp_id)) if request.method == 'POST': form = SurveyResponseForm(request.user, survey_resp.surveyquestion.surveyapp_id, request.POST, instance=survey_resp) if form.is_valid(): obj = form.save() return HttpResponseRedirect('/survey/%s/#row%s'\ % (obj.surveyquestion.surveyapp_id, obj.surveyquestion.id)) else: duplicate_count =\ SurveyResponse.objects.filter(key=request.POST['key'], surveyquestion=survey_resp.surveyquestion).count() if request.POST['key'] == survey_resp.key: if duplicate_count >= 1: form._errors["key"] = _("duplicate record key !") request.session["err_msg"] = _('Response is not added.') template = 'frontend/survey/survey_response_change.html' data = { 'form': form, 'surveyquestion_id': survey_resp.surveyquestion_id, 'surveyapp_id': survey_resp.surveyquestion.surveyapp_id, 'survey_response_id': id, 'module': current_view(request), 'action': 'update', 'err_msg': request.session.get('err_msg'), } request.session['err_msg'] = '' return render_to_response(template, data, context_instance=RequestContext(request))
def survey_response_add(request): """Add new Survey for the logged in user **Attributes**: * ``form`` - SurveyResponseForm * ``template`` - frontend/survey/survey_response_change.html **Logic Description**: * Add a new survey response which will belong to the logged in user via the SurveyResponseForm & get redirected to the selected survey """ surveyquestion_id = request.GET.get('surveyquestion_id') survey_que = SurveyQuestion.objects.get(pk=int(surveyquestion_id)) form = SurveyResponseForm(request.user, survey_que.surveyapp_id, initial={'surveyquestion': survey_que}) request.session['err_msg'] = '' if request.method == 'POST': form = SurveyResponseForm(request.user, survey_que.surveyapp_id, request.POST) if form.is_valid(): obj = form.save() request.session["msg"] = _('"%(key)s" is added.') %\ {'key': request.POST['key']} return HttpResponseRedirect('/survey/%s/#row%s'\ % (obj.surveyquestion.surveyapp_id, obj.surveyquestion.id)) else: form._errors["key"] = _("duplicate record key !") request.session["err_msg"] = _('Response is not added.') template = 'frontend/survey/survey_response_change.html' data = { 'form': form, 'surveyquestion_id': surveyquestion_id, 'surveyapp_id': survey_que.surveyapp_id, 'err_msg': request.session.get('err_msg'), 'action': 'add' } request.session['err_msg'] = '' return render_to_response(template, data, context_instance=RequestContext(request))
def survey_response_add_update(request, id, que_id, data, form_type, new_response_id): """ Ajax method to update the response """ dajax = Dajax() # 1st check survey question id if int(que_id) != 0: try: SurveyQuestion.objects.get(pk=que_id) except: #dajax.alert("error : First Save your survey question !!") return dajax.json() if int(que_id) == 0: #dajax.alert("error : First Save your survey question !!") return dajax.json() if form_type == 'old_form': form = SurveyResponseForm(data) survey_response = SurveyResponse.objects.get(pk=int(id)) form = SurveyResponseForm(data, instance=survey_response) if form_type == 'new_form': form = SurveyResponseForm(data) if int(new_response_id) != 0: survey_response = SurveyResponse.objects.get(pk=int(new_response_id)) form = SurveyResponseForm(data, instance=survey_response) if form.is_valid(): key = form.cleaned_data.get("key") keyvalue = form.cleaned_data.get('keyvalue') duplicate_count = 0 # start checking of duplicate key if form_type == 'old_form': resp_obj = SurveyResponse.objects.get(pk=id) if resp_obj.key != key: duplicate_count = SurveyResponse.objects.filter(key=key, surveyquestion=resp_obj.surveyquestion).count() if form_type == 'new_form': if que_id == 0: # without que id # get last inserted record surveyquestion = SurveyQuestion.objects.all().order_by('-id')[0] else: # with que id surveyquestion = SurveyQuestion.objects.get(pk=que_id) duplicate_count = SurveyResponse.objects.filter(key=key, surveyquestion=surveyquestion).count() if duplicate_count >= 1 and form_type == 'old_form': #dajax.alert("error : (%s) duplicate record key & previous key (%s) is not changed!" % (key, # resp_obj.key)) return dajax.json() if duplicate_count >= 1 and form_type == 'new_form' and int(new_response_id) == 0: #dajax.alert("error : (%s) duplicate record key !" % (key)) return dajax.json() # end checking of duplicate key if form_type == 'old_form': form.save() if form_type == 'new_form': obj = form.save(commit=False) obj.surveyquestion = surveyquestion obj.save() dajax.assign('#new_response_id', 'value', obj.id) #dajax.alert("(%s - %s) is successfully saved !!" % (form.cleaned_data.get('key'), # form.cleaned_data.get('keyvalue'))) else: if form_type == 'new_form': dajax.remove_css_class('#new_survey_que_form_'+id+' input','error') if form_type == 'old_form': dajax.remove_css_class('#que_form_'+id+' input','error') dajax.alert("error") for error in form.errors: dajax.add_css_class('#id_%s' % error,'error') return dajax.json()
def survey_response_add_update(request, id, que_id, data, form_type, new_response_id): """ Ajax method to update the response """ dajax = Dajax() # 1st check survey question id if int(que_id) != 0: try: SurveyQuestion.objects.get(pk=que_id) except: #dajax.alert("error : First Save your survey question !!") return dajax.json() if int(que_id) == 0: #dajax.alert("error : First Save your survey question !!") return dajax.json() if form_type == 'old_form': form = SurveyResponseForm(data) survey_response = SurveyResponse.objects.get(pk=int(id)) form = SurveyResponseForm(data, instance=survey_response) if form_type == 'new_form': form = SurveyResponseForm(data) if int(new_response_id) != 0: survey_response = SurveyResponse.objects.get( pk=int(new_response_id)) form = SurveyResponseForm(data, instance=survey_response) if form.is_valid(): key = form.cleaned_data.get("key") keyvalue = form.cleaned_data.get('keyvalue') duplicate_count = 0 # start checking of duplicate key if form_type == 'old_form': resp_obj = SurveyResponse.objects.get(pk=id) if resp_obj.key != key: duplicate_count = SurveyResponse.objects.filter( key=key, surveyquestion=resp_obj.surveyquestion).count() if form_type == 'new_form': if que_id == 0: # without que id # get last inserted record surveyquestion = SurveyQuestion.objects.all().order_by( '-id')[0] else: # with que id surveyquestion = SurveyQuestion.objects.get(pk=que_id) duplicate_count = SurveyResponse.objects.filter( key=key, surveyquestion=surveyquestion).count() if duplicate_count >= 1 and form_type == 'old_form': #dajax.alert("error : (%s) duplicate record key & previous key (%s) is not changed!" % (key, # resp_obj.key)) return dajax.json() if duplicate_count >= 1 and form_type == 'new_form' and int( new_response_id) == 0: #dajax.alert("error : (%s) duplicate record key !" % (key)) return dajax.json() # end checking of duplicate key if form_type == 'old_form': form.save() if form_type == 'new_form': obj = form.save(commit=False) obj.surveyquestion = surveyquestion obj.save() dajax.assign('#new_response_id', 'value', obj.id) #dajax.alert("(%s - %s) is successfully saved !!" % (form.cleaned_data.get('key'), # form.cleaned_data.get('keyvalue'))) else: if form_type == 'new_form': dajax.remove_css_class('#new_survey_que_form_' + id + ' input', 'error') if form_type == 'old_form': dajax.remove_css_class('#que_form_' + id + ' input', 'error') dajax.alert("error") for error in form.errors: dajax.add_css_class('#id_%s' % error, 'error') return dajax.json()