def post(self, request, *args, **kwargs): from survey.forms import SurveyForm self.form = SurveyForm(request.POST, request.FILES) self.community = Community.objects.get(pk=self.kwargs["pk"]) if request.is_ajax() and self.form.is_valid(): survey = self.form.save(commit=False) answers = request.POST.getlist("answers") if not answers: HttpResponse("not ansvers") new_survey = survey.create_survey( title=survey.title, image=survey.image, lists=request.POST.get("list"), creator=request.user, order=survey.order, is_anonymous=survey.is_anonymous, is_multiple=survey.is_multiple, is_no_edited=survey.is_no_edited, time_end=survey.time_end, answers=answers, community=self.community, ) return render_for_platform(request, 'survey/community/preview.html', {'object': new_survey}) else: return HttpResponseBadRequest("")
def batch_form(request, district_slug, school_slug, **kwargs): # check if district exists district = get_object_or_404(District.objects, slug__iexact=district_slug) # get school in district school = get_object_or_404(School.objects, districtid=district, slug__iexact=school_slug) # translate to lat/lon school.geometry.transform(4326) SurveyFormset = inlineformset_factory(Survey, Child, form=ChildForm, extra=1, can_delete=False) formerror = False message = "New survey" if request.method == 'POST': surveyform = SurveyForm(request.POST, school=school) if surveyform.is_valid(): survey = surveyform.save(commit=False) survey.user = request.user survey.school = school survey.update_distance() if survey.location is None or survey.location.x == 0: survey.update_location() survey.ip = request.META['REMOTE_ADDR'] surveyformset = SurveyFormset(request.POST, instance=survey) if surveyformset.is_valid(): survey.save() surveyformset.save() #Done. Make new form. message = "Survey submitted. New Entry." survey = Survey() surveyform = SurveyForm(instance=survey) surveyformset = SurveyFormset(instance=survey) else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: survey = Survey() surveyform = SurveyForm(instance=survey) surveyformset = SurveyFormset(instance=survey) return render_to_response('survey/batch.html', { 'message': message, 'formerror': formerror, 'school': school, 'surveyform': surveyform, 'surveyformset': surveyformset, }, context_instance=RequestContext(request) )
def survey_add(request, group_slug=None, group_slug_field=None, group_qs=None, template_name = 'survey/survey_add.html', extra_context=None, *args, **kw): if request.method == "POST": request_post = request.POST.copy() survey_form = SurveyForm(request_post) if survey_form.is_valid(): new_survey = survey_form.save(commit=False) new_survey.created_by = request.user new_survey.editable_by = request.user new_survey.slug = slugify(new_survey.title) if group_slug: group = get_object_or_404(group_qs,slug=group_slug) new_survey.recipient = group new_survey.save() return HttpResponseRedirect(reverse("surveys-editable",kwargs={})) else: survey_form = SurveyForm() return render_to_response(template_name, {'title': _("Add a survey"), 'form' : survey_form}, context_instance=RequestContext(request))
def survey_add(request): """Add new Survey for the logged in user **Attributes**: * ``form`` - SurveyForm * ``template`` - frontend/survey/change.html **Logic Description**: * Add a new survey which will belong to the logged in user via the SurveyForm & get redirected to the survey list """ form = SurveyForm() if request.method == "POST": form = SurveyForm(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.user = User.objects.get(username=request.user) obj.save() request.session["msg"] = _('"%(name)s" added.') % {"name": request.POST["name"]} return HttpResponseRedirect(redirect_url_to_survey_list + "%s/" % (obj.id)) template = "frontend/survey/survey_change.html" data = {"form": form, "action": "add"} return render_to_response(template, data, context_instance=RequestContext(request))
def survey_update(request, survey_slug, group_slug=None, group_slug_field=None, group_qs=None, template_name = 'survey/survey_add.html', extra_context=None, *args, **kw): if request.method == "POST": request_post = request.POST.copy() survey = get_object_or_404(Survey, slug=survey_slug) survey_form = SurveyForm(instance=survey,data=request_post) if survey_form.is_valid(): new_survey = survey_form.save(commit=False) new_survey.created_by = request.user new_survey.editable_by = request.user new_survey.slug = slugify(new_survey.title) new_survey.save() return HttpResponseRedirect(reverse("survey-edit",None,(),{"survey_slug":survey_slug})) else: survey = get_object_or_404(Survey, slug=survey_slug) survey_form = SurveyForm(instance=survey) return render_to_response(template_name, {'title': _("Update '%s'") % survey.title, 'survey' : survey, 'form' : survey_form}, context_instance=RequestContext(request))
def survey_add(request): """Add new Survey for the logged in user **Attributes**: * ``form`` - SurveyForm * ``template`` - frontend/survey/change.html **Logic Description**: * Add a new survey which will belong to the logged in user via the SurveyForm & get redirected to the survey list """ form = SurveyForm() if request.method == 'POST': form = SurveyForm(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.user = User.objects.get(username=request.user) obj.save() request.session["msg"] = _('"%(name)s" added.') %\ {'name': request.POST['name']} return HttpResponseRedirect('/survey/%s/' % (obj.id)) template = 'frontend/survey/survey_change.html' data = { 'module': current_view(request), 'form': form, 'action': 'add', } return render_to_response(template, data, context_instance=RequestContext(request))
def test_SurveyForm_is_valid(self): self.data['is_initial_evaluation'] = False survey = SurveyForm(data=self.data) self.assertTrue(survey.is_valid()) self.data['is_initial_evaluation'] = True survey = SurveyForm(data=self.data) self.assertTrue(survey.is_valid())
def survey_change(request, object_id): """Update/Delete Survey for the logged in user **Attributes**: * ``object_id`` - Selected survey object * ``form`` - SurveyForm * ``template`` - frontend/survey/change.html **Logic Description**: * Update/delete selected survey from the survey list via SurveyForm & get redirected to survey list """ survey = SurveyApp.objects.get(pk=object_id) survey_que_list = SurveyQuestion.objects\ .filter(surveyapp=survey).order_by('order') survey_response_list = {} for survey_que in survey_que_list: res_list = SurveyResponse.objects\ .filter(surveyquestion=survey_que).order_by('key') if res_list: # survey question response survey_response_list['%s' % survey_que.id] = res_list form = SurveyForm(instance=survey) if request.method == 'POST': if request.POST.get('delete'): survey_del(request, object_id) return HttpResponseRedirect('/survey/') else: form = SurveyForm(request.POST, request.user, instance=survey) if form.is_valid(): form.save() request.session["msg"] = _('"%(name)s" is updated.')\ % {'name': request.POST['name']} return HttpResponseRedirect('/survey/') template = 'frontend/survey/survey_change.html' data = { 'survey_obj_id': object_id, 'survey_que_list': survey_que_list, 'survey_response_list': survey_response_list, 'module': current_view(request), 'action': 'update', 'form': form, 'msg': request.session.get('msg'), 'notice_count': notice_count(request), } request.session['msg'] = '' return render_to_response(template, data, context_instance=RequestContext(request))
def form(request, district_slug, school_slug, **kwargs): # check if district exists district = get_object_or_404(District.objects, slug__iexact=district_slug) # get school in district school = get_object_or_404(School.objects, districtid=district, slug__iexact=school_slug) # translate to lat/lon school.geometry.transform(4326) SurveyFormset = inlineformset_factory(Survey, Child, form=ChildForm, extra=1, can_delete=False) formerror = False if request.method == 'POST': surveyform = SurveyForm(request.POST, school=school) if surveyform.is_valid(): survey = surveyform.save(commit=False) survey.school = school survey.update_distance() survey.created = datetime.now() survey.ip = request.META['REMOTE_ADDR'] surveyformset = SurveyFormset(request.POST, instance=survey) if surveyformset.is_valid(): survey.save() surveyformset.save() return render_to_response( 'survey/thanks.html', context_instance=RequestContext(request) ) else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: survey = Survey() surveyform = SurveyForm(instance=survey) surveyformset = SurveyFormset(instance=survey) return render_to_response('survey/form.html', { 'formerror': formerror, 'school': school, 'surveyform': surveyform, 'surveyformset': surveyformset, }, context_instance=RequestContext(request) )
def survey_change(request, object_id): """Update/Delete Survey for the logged in user **Attributes**: * ``object_id`` - Selected survey object * ``form`` - SurveyForm * ``template`` - frontend/survey/change.html **Logic Description**: * Update/delete selected survey from the survey list via SurveyForm & get redirected to survey list """ survey = get_object_or_404( Survey_template, pk=object_id, user=request.user) section_list = Section_template.objects.filter(survey=survey).order_by('order') form = SurveyForm(instance=survey) branching_list = Branching_template.objects\ .filter(section__survey=survey).order_by('id') branching_section_list = \ branching_list.values_list('section_id', flat=True).distinct() if request.method == 'POST': if request.POST.get('delete'): survey_del(request, object_id) return HttpResponseRedirect('/survey/') else: form = SurveyForm(request.POST, request.user, instance=survey) if form.is_valid(): form.save() request.session["msg"] = _('"%(name)s" is updated.')\ % {'name': request.POST['name']} return HttpResponseRedirect('/survey/') template = 'frontend/survey/survey_change.html' data = { 'survey_obj_id': object_id, 'section_list': section_list, 'branching_list': branching_list, 'branching_section_list': branching_section_list, 'module': current_view(request), 'action': 'update', 'form': form, 'msg': request.session.get('msg'), 'SECTION_TYPE': SECTION_TYPE, } request.session['msg'] = '' return render_to_response(template, data, context_instance=RequestContext(request))
def survey(request): if request.method == 'POST': form = SurveyForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/thanks') else: form = SurveyForm() counter = HitCount.objects.all()[0] counter.count += 1 counter.save() return render_to_response('survey.html', {'form': form}, context_instance=RequestContext(request))
def survey(request): user = request.user if request.method == 'POST': form = SurveyForm(request.POST) if form.is_valid(): source_id = form.cleaned_data['source_dataset_id'] target_id = form.cleaned_data['target_dataset_id'] similarity_str = form.cleaned_data['similarity'] sim_id = form.cleaned_data['sim_id'] source_dataset = Dataset.objects.get(id=source_id) target_dataset = Dataset.objects.get(id=target_id) similarity = Similarity.objects.get(id=sim_id) similarity.similarity = similarity_str similarity.save() user.userprofile.points = user.userprofile.points + 1; user.userprofile.rated_datasets.add(similarity) user.userprofile.save() selected_source_dataset = None selected_target_dataset = None sim_id = None for similarity in Similarity.objects.filter(similarity=None): sim_id = similarity.id source_dataset = similarity.source_dataset target_dataset = similarity.target_dataset try: user_rating = user.userprofile.rated_datasets.get(source_dataset=source_dataset, target_dataset=target_dataset) except: user_rating = None if user_rating == None and r.get('ld_survey:similarity:%s' % sim_id) == None: selected_source_dataset = source_dataset selected_target_dataset = target_dataset break if sim_id != None: r.set('ld_survey:similarity:%s' % sim_id, user.username) r.expire('ld_survey:similarity:%s' % sim_id, 300) form = SurveyForm(initial={'similarity': 'undefined'}) top_users = UserProfile.objects.filter(points__gt=0).order_by('-points')[:5] return render(request, 'survey/survey.html', {'form': form, 'source_dataset': selected_source_dataset, 'target_dataset': selected_target_dataset, 'top_users': top_users, 'sim_id': sim_id})
def survey_change(request, object_id): """Update/Delete Survey for the logged in user **Attributes**: * ``object_id`` - Selected survey object * ``form`` - SurveyForm * ``template`` - frontend/survey/change.html **Logic Description**: * Update/delete selected survey from the survey list via SurveyForm & get redirected to survey list """ survey = get_object_or_404(Survey_template, pk=object_id, user=request.user) section_list = Section_template.objects.filter(survey=survey).order_by("order") form = SurveyForm(instance=survey) branching_list = Branching_template.objects.filter(section__survey=survey).order_by("id") branching_section_list = branching_list.values_list("section_id", flat=True).distinct() if request.method == "POST": if request.POST.get("delete"): survey_del(request, object_id) return HttpResponseRedirect(redirect_url_to_survey_list) else: form = SurveyForm(request.POST, request.user, instance=survey) if form.is_valid(): form.save() request.session["msg"] = _('"%(name)s" is updated.') % {"name": request.POST["name"]} return HttpResponseRedirect(redirect_url_to_survey_list) template = "frontend/survey/survey_change.html" data = { "survey_obj_id": object_id, "section_list": section_list, "branching_list": branching_list, "branching_section_list": branching_section_list, "action": "update", "form": form, "msg": request.session.get("msg"), "SECTION_TYPE": SECTION_TYPE, } request.session["msg"] = "" return render_to_response(template, data, context_instance=RequestContext(request))
def index(request): template = 'survey/survey.html' form = SurveyForm() data = { 'form': form, } return render(request, template, data)
def survey_change(request, object_id): """Update/Delete Survey for the logged in user **Attributes**: * ``object_id`` - Selected survey object * ``form`` - SurveyForm * ``template`` - survey/change.html **Logic Description**: * Update/delete selected survey from the survey list via SurveyForm & get redirected to survey list """ survey = get_object_or_404(Survey_template, pk=object_id, user=request.user) section_list = Section_template.objects.filter(survey=survey).order_by('order') form = SurveyForm(request.POST or None, instance=survey) branching_list = Branching_template.objects.filter(section__survey=survey).order_by('id') branching_section_list = branching_list.values_list('section_id', flat=True).distinct() if form.is_valid(): if request.POST.get('delete'): survey_del(request, object_id) return HttpResponseRedirect(redirect_url_to_survey_list) else: form.save() request.session["msg"] = _('"%(name)s" is updated.') % {'name': request.POST['name']} return HttpResponseRedirect(redirect_url_to_survey_list) data = { 'survey_obj_id': object_id, 'section_list': section_list, 'branching_list': branching_list, 'branching_section_list': branching_section_list, 'action': 'update', 'form': form, 'msg': request.session.get('msg'), 'SECTION_TYPE': SECTION_TYPE, } request.session['msg'] = '' return render_to_response('survey/survey_change.html', data, context_instance=RequestContext(request))
class SurveyCommunityCreate(TemplateView): template_name = None def get(self, request, *args, **kwargs): self.template_name = get_detect_platform_template( "survey/community/add.html", request.user, request.META['HTTP_USER_AGENT']) return super(SurveyCommunityCreate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): from survey.forms import SurveyForm context = super(SurveyCommunityCreate, self).get_context_data(**kwargs) context["form"] = SurveyForm() return context def post(self, request, *args, **kwargs): from survey.forms import SurveyForm self.form = SurveyForm(request.POST, request.FILES) self.community = Community.objects.get(pk=self.kwargs["pk"]) if request.is_ajax() and self.form.is_valid(): survey = self.form.save(commit=False) answers = request.POST.getlist("answers") if not answers: HttpResponse("not ansvers") new_survey = survey.create_survey( title=survey.title, image=survey.image, lists=request.POST.get("list"), creator=request.user, order=survey.order, is_anonymous=survey.is_anonymous, is_multiple=survey.is_multiple, is_no_edited=survey.is_no_edited, time_end=survey.time_end, answers=answers, community=self.community, ) return render_for_platform(request, 'survey/community/preview.html', {'object': new_survey}) else: return HttpResponseBadRequest("")
def test_survey_view_add(self): """Test Function survey view add""" response = self.client.get('/module/survey/add/') self.assertTrue(response.context['form'], SurveyForm()) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'survey/survey_change.html') request = self.factory.post('/module/survey/add/', {'name': 'test_survey'}, follow=True) request.user = self.user request.session = {} response = survey_add(request) self.assertEqual(response.status_code, 302)
def survey_update(request, survey_slug, group_slug=None, group_slug_field=None, group_qs=None, template_name='survey/survey_add.html', extra_context=None, *args, **kw): if request.method == "POST": request_post = request.POST.copy() survey = get_object_or_404(Survey, slug=survey_slug) survey_form = SurveyForm(instance=survey, data=request_post) if survey_form.is_valid(): new_survey = survey_form.save(commit=False) new_survey.created_by = request.user new_survey.editable_by = request.user new_survey.slug = slugify(new_survey.title) new_survey.save() return HttpResponseRedirect( reverse("survey-edit", None, (), {"survey_slug": survey_slug})) else: survey = get_object_or_404(Survey, slug=survey_slug) survey_form = SurveyForm(instance=survey) return render_to_response(template_name, { 'title': _("Update '%s'") % survey.title, 'survey': survey, 'form': survey_form }, context_instance=RequestContext(request))
def survey_add(request, group_slug=None, group_slug_field=None, group_qs=None, template_name='survey/survey_add.html', extra_context=None, *args, **kw): if request.method == "POST": request_post = request.POST.copy() survey_form = SurveyForm(request_post) if survey_form.is_valid(): new_survey = survey_form.save(commit=False) new_survey.created_by = request.user new_survey.editable_by = request.user new_survey.slug = slugify(new_survey.title) if group_slug: group = get_object_or_404(group_qs, slug=group_slug) new_survey.recipient = group new_survey.save() return HttpResponseRedirect(reverse("surveys-editable", kwargs={})) else: survey_form = SurveyForm() return render_to_response(template_name, { 'title': _("Add a survey"), 'form': survey_form }, context_instance=RequestContext(request))
def survey_add(request): """Add new Survey for the logged in user **Attributes**: * ``form`` - SurveyForm * ``template`` - survey/change.html **Logic Description**: * Add a new survey which will belong to the logged in user via the SurveyForm & get redirected to the survey list """ form = SurveyForm(request.POST or None) if form.is_valid(): obj = form.save(user=request.user) request.session["msg"] = _('"%(name)s" added.') % {'name': request.POST['name']} return HttpResponseRedirect(redirect_url_to_survey_list + '%s/' % (obj.id)) data = { 'form': form, 'action': 'add', } return render_to_response('survey/survey_change.html', data, context_instance=RequestContext(request))
def form(request, district_slug, school_slug, **kwargs): # check if district exists district = get_object_or_404(District.objects, slug__iexact=district_slug) # get school in district school = get_object_or_404(School.objects, districtid=district, slug__iexact=school_slug) # translate to lat/lon school.geometry.transform(4326) SurveyFormset = inlineformset_factory(Survey, Child, form=ChildForm, extra=1, can_delete=False) formerror = False if request.method == 'POST': surveyform = SurveyForm(request.POST, school=school) if surveyform.is_valid(): survey = surveyform.save(commit=False) survey.school = school survey.update_distance() survey.created = datetime.now() survey.ip = request.META['REMOTE_ADDR'] surveyformset = SurveyFormset(request.POST, instance=survey) if surveyformset.is_valid(): survey.save() surveyformset.save() return render_to_response( 'survey/thanks.html', context_instance=RequestContext(request)) else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: surveyformset = SurveyFormset(request.POST, instance=Survey()) formerror = True else: survey = Survey() surveyform = SurveyForm(instance=survey) surveyformset = SurveyFormset(instance=survey) return render_to_response('survey/form.html', { 'formerror': formerror, 'school': school, 'surveyform': surveyform, 'surveyformset': surveyformset, }, context_instance=RequestContext(request))
def post(self, request, *args, **kwargs): from survey.forms import SurveyForm self.survey, self.form = Survey.objects.get( pk=self.kwargs["pk"]), SurveyForm(request.POST, request.FILES, instance=self.survey) if request.is_ajax() and self.form.is_valid( ) and request.user.is_staff_of_community(self.kwargs["pk"]): survey = self.form.save(commit=False) new_survey = survey.edit_survey( title=survey.title, image=survey.image, lists=request.POST.get("list"), order=survey.order, is_anonymous=survey.is_anonymous, is_multiple=survey.is_multiple, is_no_edited=survey.is_no_edited, time_end=survey.time_end, answers=answers, ) else: return HttpResponseBadRequest() return super(SurveyCommunityEdit, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs): from survey.forms import SurveyForm context = super(SurveyCommunityCreate, self).get_context_data(**kwargs) context["form"] = SurveyForm() return context
def index(request): if request.method == 'POST': # if 'previous_token' in request.POST: # id = int(request.POST.get('previous_token')) # if SurveyAnswer.objects.filter(id=id).exists(): # form = SurveyForm(instance=SurveyAnswer.objects.get(id=id)) # else: # form = SurveyForm() # id = 'invalid' # return render(request, 'survey/index.html', # context={'form': form, 'site_key': settings.RECAPTCHA_SITE_KEY, 'id': id}) # else: # Else start # create a form instance and populate it with data from the request: form = SurveyForm(request.POST) # check whether it's valid: if form.is_valid(): answer: SurveyAnswer = form.save(commit=False) answer = answer.calculate_infection_score() # RECAPTCHA v3 validation captcha_result = requests.post( 'https://www.google.com/recaptcha/api/siteverify', data={ 'response': request.POST.get('g-recaptcha-response'), 'secret': settings.RECAPTCHA_SECRET_KEY }).json() if captcha_result['success']: captcha_score = captcha_result['score'] else: captcha_score = 0 answer.captcha_score = captcha_score # Browser id if 'browser_id' in request.COOKIES: browser_id = int(request.COOKIES.get('browser_id')) answer.browser_id = browser_id else: browser_id = SurveyAnswer.objects.aggregate( Max('browser_id'))['browser_id__max'] if not browser_id: browser_id = 1 else: browser_id += 1 answer.browser_id = browser_id # If updating previous data if request.POST.get('id'): id = int(request.POST.get('id')) SurveyAnswer.objects.filter(id=id).update( fever=answer.fever, cough=answer.cough, diarrhea=answer.diarrhea, sore_throat=answer.sore_throat, body_ache=answer.body_ache, headache=answer.headache, breathless=answer.breathless, fatigue=answer.fatigue, age_group=answer.age_group, diabetes=answer.diabetes, heart=answer.heart, lever=answer.lever, smoking=answer.smoking, cancer_therapy=answer.cancer_therapy, steroid=answer.steroid, travel_14_days=answer.travel_14_days, travel_infected_3_month=answer.travel_infected_3_month, close_contact=answer.close_contact, postcode=answer.postcode, lat=answer.lat, lon=answer.lon, captcha_score=captcha_score, infection_score=answer.infection_score, browser_id=answer.browser_id) # Adding new data else: answer.save() html = render(request, 'survey/submitted.html', context={ 'success': True, 'id': answer.id }) html.set_cookie('browser_id', browser_id, max_age=999999999) # Else end else: print(form.errors) html = render(request, 'survey/submitted.html', context={'success': False}) return html else: form = SurveyForm() cookied = 'true' if bool( request.COOKIES.get('browser_id')) else 'false' return render(request, 'survey/index.html', context={ 'form': form, 'site_key': settings.RECAPTCHA_SITE_KEY, 'cookied': str(cookied) })