def plots_closest_to_point(request, lat=None, lon=None): ###TODO: Need to pin to an instance instance = Instance.objects.all()[0] point = Point(float(lon), float(lat), srid=4326) try: max_plots = int(request.GET.get('max_plots', '1')) except ValueError: raise HttpBadRequestException( 'The max_plots parameter must be a number between 1 and 500') if max_plots > 500 or max_plots < 1: raise HttpBadRequestException( 'The max_plots parameter must be a number between 1 and 500') try: distance = float(request.GET.get( 'distance', settings.MAP_CLICK_RADIUS)) except ValueError: raise HttpBadRequestException( 'The distance parameter must be a number') # 100 meters plots = Plot.objects.distance(point)\ .filter(instance=instance)\ .filter(geom__distance_lte=(point, D(m=distance)))\ .order_by('distance')[0:max_plots] if 'q' in request.REQUEST: q = request.REQUEST['q'] plots = plots.filter(create_filter(q)) return plots_to_list_of_dict( plots, longform=True, user=request.user)
def csv_export(job_pk, model, query): job = ExportJob.objects.get(pk=job_pk) instance = job.instance if model == 'species': initial_qs = (Species.objects. filter(instance=instance)) else: # model == 'tree' # TODO: if an anonymous job with the given query has been # done since the last update to the audit records table, # just return that job # get the plots for the provided # query and turn them into a tree queryset plot_query = (create_filter(query) .filter(instance_id=instance.id)) initial_qs = Tree.objects.filter(plot__in=plot_query) # limit_fields_by_user exists on authorizable models/querysets # keep track of the before/after queryset to determine if empty # querysets were caused by authorization failure. if hasattr(initial_qs, 'limit_fields_by_user'): if job.user and job.user.is_authenticated(): limited_qs = initial_qs.limit_fields_by_user(instance, job.user) else: limited_qs = initial_qs.none() else: limited_qs = initial_qs if not initial_qs.exists(): job.status = ExportJob.EMPTY_QUERYSET_ERROR # if the initial queryset was not empty but the limited queryset # is empty, it means that there were no fields which the user # was allowed to export. elif not limited_qs.exists(): job.status = ExportJob.MODEL_PERMISSION_ERROR else: csv_file = make_csv_file(limited_qs) csv_name = generate_filename(limited_qs) job.outfile.save(csv_name, File(csv_file)) job.status = ExportJob.COMPLETE job.save()
def csv_export(job_pk, model, query): job = ExportJob.objects.get(pk=job_pk) instance = job.instance if job.user and job.user.is_authenticated(): if model == 'species': initial_qs = (Species.objects. filter(instance=instance)) extra_select, values = extra_select_and_values_for_model( instance, job.user, 'treemap_species', 'species') ordered_fields = values + extra_select.keys() limited_qs = initial_qs.extra(select=extra_select)\ .values(*ordered_fields) else: # model == 'tree' # TODO: if an anonymous job with the given query has been # done since the last update to the audit records table, # just return that job # get the plots for the provided # query and turn them into a tree queryset plot_query = (create_filter(query) .filter(instance_id=instance.id)) initial_qs = Tree.objects.filter(plot__in=plot_query) extra_select_tree, values_tree = extra_select_and_values_for_model( instance, job.user, 'treemap_tree', 'Tree') extra_select_plot, values_plot = extra_select_and_values_for_model( instance, job.user, 'treemap_mapfeature', 'Plot', prefix='plot') extra_select_sp, values_sp = extra_select_and_values_for_model( instance, job.user, 'treemap_species', 'Species', prefix='species') if 'plot__geom' in values_plot: values_plot = [f for f in values_plot if f != 'plot__geom'] values_plot += ['plot__geom__x', 'plot__geom__y'] extra_select = {'plot__geom__x': 'ST_X(treemap_mapfeature.the_geom_webmercator)', 'plot__geom__y': 'ST_Y(treemap_mapfeature.the_geom_webmercator)'} extra_select.update(extra_select_tree) extra_select.update(extra_select_plot) extra_select.update(extra_select_sp) ordered_fields = (sorted(values_tree) + sorted(values_plot) + sorted(values_sp)) if ordered_fields: limited_qs = initial_qs.extra(select=extra_select)\ .values(*ordered_fields) else: limited_qs = initial_qs.none() else: ordered_fields = None limited_qs = initial_qs.none() if not initial_qs.exists(): job.status = ExportJob.EMPTY_QUERYSET_ERROR # if the initial queryset was not empty but the limited queryset # is empty, it means that there were no fields which the user # was allowed to export. elif not limited_qs.exists(): job.status = ExportJob.MODEL_PERMISSION_ERROR else: csv_file = TemporaryFile() write_csv(limited_qs, csv_file, field_order=ordered_fields) csv_name = generate_filename(limited_qs) job.outfile.save(csv_name, File(csv_file)) job.status = ExportJob.COMPLETE job.save()
def _execute_filter(instance, filter_str): return create_filter(filter_str).filter(instance=instance)