def awc_pdf_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) permit_nums = request.GET.getlist('permit') submission_type = request.GET.get('submissionType', '') observations = request.GET.get('observations', None) if observations: observations = ["uuid:" + x for x in observations.strip().split(",")] if not observations or len(observations) < 1 or len(observations) > 5: return HttpResponseBadRequest("Provide between 1 and 5 observations") if len(permit_nums) == 0: return HttpResponseBadRequest("Must provide at least one permit") pdf = generate_pdf(id_string, submission_type, observations, user=owner, permit_nums=permit_nums) #TODO - Get full name. Also, observer or user? response = HttpResponse(pdf, mimetype="application/pdf") response['Content-Disposition'] = disposition_ext_and_date( '-'.join(permit_nums), 'pdf') response['Content-Length'] = len(pdf) return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message = "HELLO!!" owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) context.data = kml_export_data(id_string, user=owner) response = \ render_to_response("survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ disposition_ext_and_date(id_string, 'kml') audit = { "xform": xform.id_string, "export_type": Export.KML_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message = "HELLO!!" owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) context.data = kml_export_data(id_string, user=owner) response = \ render_to_response("survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ disposition_ext_and_date(id_string, 'kml') audit = {"xform": xform.id_string, "export_type": Export.KML_EXPORT} audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message = "HELLO!!" owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u"Not shared.")) dd = DataDictionary.objects.get(id_string=id_string, user=owner) pis = ParsedInstance.objects.filter( instance__user=owner, instance__xform__id_string=id_string, lat__isnull=False, lng__isnull=False ) data_for_template = [] labels = {} def cached_get_labels(xpath): if xpath in labels.keys(): return labels[xpath] labels[xpath] = dd.get_label(xpath) return labels[xpath] for pi in pis: # read the survey instances data_for_display = pi.to_dict() xpaths = data_for_display.keys() xpaths.sort(cmp=pi.data_dictionary.get_xpath_cmp()) label_value_pairs = [ (cached_get_labels(xpath), data_for_display[xpath]) for xpath in xpaths if not xpath.startswith(u"_") ] table_rows = [] for key, value in label_value_pairs: table_rows.append("<tr><td>%s</td><td>%s</td></tr>" % (key, value)) img_urls = image_urls(pi.instance) img_url = img_urls[0] if img_urls else "" data_for_template.append( { "name": id_string, "id": pi.id, "lat": pi.lat, "lng": pi.lng, "image_urls": img_urls, "table": '<table border="1"><a href="#"><img width="210" ' 'class="thumbnail" src="%s" alt=""></a>%s' "</table>" % (img_url, "".join(table_rows)), } ) context.data = data_for_template response = render_to_response( "survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml" ) response["Content-Disposition"] = disposition_ext_and_date(id_string, "kml") return response
def yukon_xls_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) site_nums = request.GET.getlist('site') if len(site_nums) != 1: return HttpResponseBadRequest("Must provide a single site id") xls = generate_yukon_xls(id_string, user=owner, site_nums=site_nums) response = HttpResponse(xls, mimetype="application/vnd.ms-excel") response['Content-Disposition'] = disposition_ext_and_date( '-'.join(site_nums), 'xls') return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message="HELLO!!" owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) if not has_permission(xform, owner, request): return HttpResponseForbidden('Not shared.') dd = DataDictionary.objects.get(id_string=id_string, user=owner) pis = ParsedInstance.objects.filter(instance__user=owner, instance__xform__id_string=id_string, lat__isnull=False, lng__isnull=False) data_for_template = [] labels = {} def cached_get_labels(xpath): if xpath in labels.keys(): return labels[xpath] labels[xpath] = dd.get_label(xpath) return labels[xpath] for pi in pis: # read the survey instances data_for_display = pi.to_dict() xpaths = data_for_display.keys() xpaths.sort(cmp=pi.data_dictionary.get_xpath_cmp()) label_value_pairs = [ (cached_get_labels(xpath), data_for_display[xpath]) for xpath in xpaths if not xpath.startswith(u"_")] table_rows = [] for key, value in label_value_pairs: table_rows.append('<tr><td>%s</td><td>%s</td></tr>' % (key, value)) img_urls = image_urls(pi.instance) img_url = img_urls[0] if img_urls else "" data_for_template.append({ 'name':id_string, 'id': pi.id, 'lat': pi.lat, 'lng': pi.lng, 'image_urls': img_urls, 'table': '<table border="1"><a href="#"><img width="210" class="thumbnail" src="%s" alt=""></a>%s</table>' % (img_url,''.join(table_rows))}) context.data = data_for_template response = render_to_response("survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = disposition_ext_and_date(id_string, 'kml') return response
def frp_xls_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) permit_nums = request.GET.getlist('permit') biol_date = request.GET.get('biologistContactDate', '') if len(permit_nums) != 1: return HttpResponseBadRequest("Must provide a single permit") pdf = generate_frp_xls(id_string, biol_date, user=owner, permit_nums=permit_nums) response = HttpResponse(pdf, mimetype="application/vnd.ms-excel") response['Content-Disposition'] = disposition_ext_and_date( '-'.join(permit_nums), 'xls') return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message="HELLO!!" owner = User.objects.get(username=username) xform = XForm.objects.get(id_string=id_string, user=owner) if not has_permission(xform, owner, request): return HttpResponseForbidden('Not shared.') dd = DataDictionary.objects.get(id_string=id_string, user=owner) pis = ParsedInstance.objects.filter(instance__user=owner, instance__xform__id_string=id_string, lat__isnull=False, lng__isnull=False) data_for_template = [] for pi in pis: # read the survey instances data = pi.to_dict() # get rid of keys with leading underscores data_for_display = {} for k, v in data.items(): if not k.startswith(u"_"): data_for_display[k] = v xpaths = data_for_display.keys() xpaths.sort(cmp=pi.data_dictionary.get_xpath_cmp()) label_value_pairs = [ (pi.data_dictionary.get_label(xpath), data_for_display[xpath]) for xpath in xpaths] table_rows = [] for key, value in label_value_pairs: table_rows.append('<tr><td>%s</td><td>%s</td></tr>' % (key, value)) img_urls = image_urls(pi.instance) img_url = img_urls[0] if img_urls else "" data_for_template.append({"name":id_string, "id": pi.id, "lat": pi.lat, "lng": pi.lng,'image_urls': img_urls, "table": '<table border="1"><a href="#"><img width="210" class="thumbnail" src="%s" alt=""></a><%s</table>' % (img_url,''.join(table_rows))}) context.data = data_for_template response = render_to_response("survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = disposition_ext_and_date(id_string, 'kml') return response
def kml_export(request, username, id_string): # read the locations from the database context = RequestContext(request) context.message = "HELLO!!" owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) dd = DataDictionary.objects.get(id_string=id_string, user=owner) pis = ParsedInstance.objects.filter(instance__user=owner, instance__xform__id_string=id_string, lat__isnull=False, lng__isnull=False) data_for_template = [] labels = {} def cached_get_labels(xpath): if xpath in labels.keys(): return labels[xpath] labels[xpath] = dd.get_label(xpath) return labels[xpath] for pi in pis: # read the survey instances data_for_display = pi.to_dict() xpaths = data_for_display.keys() xpaths.sort(cmp=pi.data_dictionary.get_xpath_cmp()) label_value_pairs = [ (cached_get_labels(xpath), data_for_display[xpath]) for xpath in xpaths if not xpath.startswith(u"_")] table_rows = [] for key, value in label_value_pairs: table_rows.append('<tr><td>%s</td><td>%s</td></tr>' % (key, value)) img_urls = image_urls(pi.instance) img_url = img_urls[0] if img_urls else "" data_for_template.append({ 'name': id_string, 'id': pi.id, 'lat': pi.lat, 'lng': pi.lng, 'image_urls': img_urls, 'table': '<table border="1"><a href="#"><img width="210" ' 'class="thumbnail" src="%s" alt=""></a>%s' '</table>' % (img_url, ''.join(table_rows))}) context.data = data_for_template response = \ render_to_response("survey.kml", context_instance=context, mimetype="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ disposition_ext_and_date(id_string, 'kml') audit = { "xform": xform.id_string, "export_type": Export.KML_EXPORT } audit_log(Actions.EXPORT_CREATED, request.user, owner, _("Created KML export on '%(id_string)s'.") %\ { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded KML export on '%(id_string)s'.") %\ { 'id_string': xform.id_string, }, audit, request) return response