def survey(request, survey_slug=None, template='survey/survey.html'): if request.GET.get('id', None) is not None and request.GET.get( 'return', None) is not None: survey = get_object_or_404( Survey, slug='puget-sound-coastal-recreation-survey', anon=True) respondant, created = Respondant.objects.get_or_create( survey=survey, uuid=request.GET.get('id')) respondant.gfk_returnURL = request.GET.get('return') respondant.save() return redirect( "/respond#/survey/puget-sound-coastal-recreation-survey/%s" % (respondant.uuid)) elif survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse( simplejson.dumps({ 'success': "true", "uuid": respondant.uuid })) return redirect("/respond#/survey/%s/%s" % (survey.slug, respondant.uuid)) context = {'ANALYTICS_ID': settings.ANALYTICS_ID} return render_to_response(template, RequestContext(request, context))
def survey(request, survey_slug=None, template='survey/survey.html'): if survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse(simplejson.dumps({'success': "true", "uuid": respondant.uuid})) return redirect("/respond#/survey/%s/%s" % (survey.slug, respondant.uuid)) return render_to_response(template, RequestContext(request, {}))
def survey(request, survey_slug=None, template='survey/survey.html'): if survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey, user=request.user) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse(simplejson.dumps({'success': "true", "uuid": respondant.uuid})) return redirect("/respond#/survey/%s/0/%s/landing" % (survey.slug, respondant.uuid)) context = {'ANALYTICS_ID': settings.ANALYTICS_ID} return render_to_response(template, RequestContext(request, context))
def dash(request, survey_slug=None, template='survey/dash.html'): if survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse(simplejson.dumps({'success': "true", "uuid": respondant.uuid})) return redirect("/respond#/survey/%s/%s" % (survey.slug, respondant.uuid)) return render_to_response(template, RequestContext(request, {'api_key': request.user.api_key.key}))
def survey(request, survey_slug=None, template='survey/survey.html'): if survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey, surveyor=request.user) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse(simplejson.dumps({'success': "true", "uuid": respondant.uuid})) return redirect("/respond#/survey/%s/%s" % (survey.slug, respondant.uuid)) context = {'ANALYTICS_ID': settings.ANALYTICS_ID} return render_to_response(template, RequestContext(request, context))
def survey(request, survey_slug=None, template='survey/survey.html'): if request.GET.get('id', None) is not None and request.GET.get('return', None) is not None: survey = get_object_or_404(Survey, slug='puget-sound-coastal-recreation-survey', anon=True) respondant, created = Respondant.objects.get_or_create(survey=survey, uuid=request.GET.get('id')) respondant.gfk_returnURL = request.GET.get('return') respondant.save() return redirect("/respond#/survey/puget-sound-coastal-recreation-survey/%s" % (respondant.uuid)) elif survey_slug is not None: survey = get_object_or_404(Survey, slug=survey_slug, anon=True) respondant = Respondant(survey=survey) respondant.save() if request.GET.get('get-uid', None) is not None: return HttpResponse(simplejson.dumps({'success': "true", "uuid": respondant.uuid})) return redirect("/respond#/survey/%s/%s" % (survey.slug, respondant.uuid)) context = {'ANALYTICS_ID': settings.ANALYTICS_ID} return render_to_response(template, RequestContext(request, context))
def surveyor_stats_json(request, survey_slug, interval): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return _error(**form.errors) res = Respondant.stats_report_filter(survey_slug, **form.cleaned_data) if not res.exists(): return _error('No records for these filters.') res = (res.extra(select={'timestamp': "date_trunc('%s', ts)" % interval}) .values('surveyor__first_name', 'surveyor__last_name', 'timestamp') .annotate(count=Count('pk'))) res = res.order_by('timestamp') grouped_data = defaultdict(list) for respondant in res: name = _get_fullname(respondant) grouped_data[name].append( (calendar.timegm(respondant['timestamp'].utctimetuple()) * 1000, respondant['count']) ) graph_data = [] for name, data in grouped_data.iteritems(): graph_data.append({'data': data, 'name': name}) return HttpResponse(json.dumps({ 'success': True, 'graph_data': graph_data }, cls=DateTimeJSONEncoder), content_type='application/json')
def full_data_dump_csv(request, survey_slug): survey = Survey.objects.get(slug=survey_slug) response = _create_csv_response('full_dump_{0}.csv'.format( datetime.date.today().strftime('%d-%m-%Y'))) fields = OrderedDict(Respondant.get_field_names().items() + survey.generate_field_names().items()) writer = SlugCSVWriter(response, fields) writer.writeheader() for resp in survey.respondant_set.all(): writer.writerow(resp.generate_flat_dict()) return response
def full_data_dump_csv(request, survey_slug): survey = Survey.objects.get(slug=survey_slug) response = _create_csv_response('full_dump_{0}.csv'.format( datetime.date.today().strftime('%d-%m-%Y'))) fields = OrderedDict(Respondant.get_field_names().items() + survey.generate_field_names().items()) writer = SlugCSVWriter(response, fields) writer.writeheader() for resp in survey.respondant_set.all(): writer.writerow(json.loads(resp.csv_row.json_data)) return response
def full_data_dump_csv(request, survey_slug): survey = Survey.objects.get(slug=survey_slug) response = _create_csv_response("full_dump_{0}.csv".format(datetime.date.today().strftime("%d-%m-%Y"))) fields = OrderedDict(Respondant.get_field_names().items() + survey.generate_field_names().items()) writer = SlugCSVWriter(response, fields) writer.writeheader() for resp in survey.respondant_set.filter(complete=True): # very basic removal of some characters that were causing issue in writing rows # row_string = resp.csv_row.json_data.replace('\u2019', '\"') # row_string = row_string.replace('\u2026', '\"') row_ascii = resp.csv_row.json_data.encode("ascii", errors="ignore") writer.writerow(json.loads(row_ascii)) return response
def surveyor_stats_raw_data_csv(request, survey_slug): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return HttpResponseBadRequest(json.dumps(form.errors)) res = (Respondant.stats_report_filter(survey_slug, **form.cleaned_data) .select_related('surveyor')) response = _create_csv_response('surveyor_stats.csv') writer = csv.writer(response) writer.writerow(('Surveyor', 'market', 'timestamp', 'status')) for row in res: writer.writerow((row.surveyor.get_full_name() if row.surveyor else '', row.survey_site, row.ts, row.review_status)) return response
def surveyor_stats_raw_data_csv(request, survey_slug): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return HttpResponseBadRequest(json.dumps(form.errors)) res = (Respondant.stats_report_filter( survey_slug, **form.cleaned_data).select_related('surveyor')) response = _create_csv_response('surveyor_stats.csv') writer = csv.writer(response) writer.writerow(('Surveyor', 'market', 'timestamp', 'status')) for row in res: writer.writerow((row.surveyor.get_full_name() if row.surveyor else '', row.survey_site, row.ts, row.review_status)) return response
def full_data_dump_csv(request, survey_slug): survey = Survey.objects.get(slug=survey_slug) response = _create_csv_response('full_dump_{0}.csv'.format( datetime.date.today().strftime('%d-%m-%Y'))) fields = OrderedDict(Respondant.get_field_names().items() + survey.generate_field_names().items()) writer = SlugCSVWriter(response, fields) writer.writeheader() for resp in survey.respondant_set.filter(complete=True): # very basic removal of some characters that were causing issue in writing rows # row_string = resp.csv_row.json_data.replace('\u2019', '\"') # row_string = row_string.replace('\u2026', '\"') row_ascii = resp.csv_row.json_data.encode('ascii', errors='ignore') writer.writerow(json.loads(row_ascii)) return response
def surveyor_stats_csv(request, survey_slug, interval): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return HttpResponseBadRequest(json.dumps(form.errors)) res = Respondant.stats_report_filter(survey_slug, **form.cleaned_data) res = (res.extra(select={'timestamp': "date_trunc('%s', ts)" % interval}) .values('surveyor__first_name', 'surveyor__last_name', 'timestamp') .annotate(count=Count('pk'))) response = _create_csv_response('raw_surveyor_stats.csv') writer = csv.writer(response) writer.writerow(('Surveyor', 'timestamp', 'count')) for row in res: writer.writerow((_get_fullname(row), row['timestamp'], row['count'])) return response
def surveyor_stats_csv(request, survey_slug, interval): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return HttpResponseBadRequest(json.dumps(form.errors)) res = Respondant.stats_report_filter(survey_slug, **form.cleaned_data) res = (res.extra(select={ 'timestamp': "date_trunc(%s, ts)" }, select_params=(interval, )).values( 'surveyor__first_name', 'surveyor__last_name', 'timestamp').annotate(count=Count('pk'))) response = _create_csv_response('raw_surveyor_stats.csv') writer = csv.writer(response) writer.writerow(('Surveyor', 'timestamp', 'count')) for row in res: writer.writerow((_get_fullname(row), row['timestamp'], row['count'])) return response
def surveyor_stats_json(request, survey_slug, interval): form = SurveyorStatsForm(request.GET) if not form.is_valid(): return _error(**form.errors) res = Respondant.stats_report_filter(survey_slug, **form.cleaned_data) if not res.exists(): return _error('No records for these filters.') res = (res.extra(select={ 'timestamp': "date_trunc(%s, ts)" }, select_params=(interval, )).values( 'surveyor__first_name', 'surveyor__last_name', 'timestamp').annotate(count=Count('pk'))) res = res.order_by('timestamp') grouped_data = defaultdict(list) for respondant in res: name = _get_fullname(respondant) grouped_data[name].append( (calendar.timegm(respondant['timestamp'].utctimetuple()) * 1000, respondant['count'])) graph_data = [] for name, data in grouped_data.iteritems(): graph_data.append({'data': data, 'name': name}) return HttpResponse(json.dumps({ 'success': True, 'graph_data': graph_data }, cls=CustomJSONEncoder), content_type='application/json')