def index(request, geography=None, is_json=False): """ Homepage view. Can also return json for the city or neighborhoods. """ if geography is None: requests = Request.objects.all() else: neighborhood = Geography.objects.get(pk=geography) requests = Request.objects.filter( geo_point__contained=neighborhood.geo) total_open = requests.filter(status="Open").count() most_recent = requests.latest('requested_datetime') minus_7 = most_recent.requested_datetime - datetime.timedelta(days=7) minus_14 = most_recent.requested_datetime - datetime.timedelta(days=14) this_week = requests.filter(requested_datetime__range= \ (minus_7, most_recent.requested_datetime)) last_week = requests.filter(requested_datetime__range= \ (minus_14, minus_7)) this_week_stats = run_stats(this_week, request_types=False, open_requests=False) last_week_stats = run_stats(last_week, request_types=False, open_requests=False) # Calculate deltas delta = {} delta['count'] = calculate_delta(this_week_stats['request_count'], last_week_stats['request_count']) delta['closed_count'] = calculate_delta( \ this_week_stats['closed_request_count'], last_week_stats['closed_request_count']) delta['opened_count'] = calculate_delta( \ this_week_stats['open_request_count'], last_week_stats['open_request_count']) delta['time'] = calculate_delta(this_week_stats['average_response'], last_week_stats['average_response']) # Put everything in a dict so we can do what we want with it. c_dict = { 'open_tickets': total_open, 'this_week_stats': this_week_stats, 'last_week_stats': last_week_stats, 'delta': delta, } if is_json is False: neighborhoods = Geography.objects.all() c_dict['neighborhoods'] = neighborhoods c_dict['latest'] = most_recent.requested_datetime c = Context(c_dict) return render(request, 'index.html', c) else: data = json.dumps(c_dict, True) return HttpResponse(data, content_type='application/json')
def index(request, geography=None, is_json=False): """ Homepage view. Can also return json for the city or neighborhoods. """ if geography is None: requests = Request.objects.all() else: neighborhood = Geography.objects.get(pk=geography) requests = Request.objects.filter(geo_point__contained=neighborhood.geo) total_open = requests.filter(status="Open").count() most_recent = requests.latest('requested_datetime') minus_7 = most_recent.requested_datetime-datetime.timedelta(days=7) minus_14 = most_recent.requested_datetime-datetime.timedelta(days=14) this_week = requests.filter(requested_datetime__range= \ (minus_7, most_recent.requested_datetime)) last_week = requests.filter(requested_datetime__range= \ (minus_14, minus_7)) this_week_stats = run_stats(this_week, request_types=False, open_requests=False) last_week_stats = run_stats(last_week, request_types=False, open_requests=False) # Calculate deltas delta = {} delta['count'] = calculate_delta(this_week_stats['request_count'], last_week_stats['request_count']) delta['closed_count'] = calculate_delta( \ this_week_stats['closed_request_count'], last_week_stats['closed_request_count']) delta['opened_count'] = calculate_delta( \ this_week_stats['open_request_count'], last_week_stats['open_request_count']) delta['time'] = calculate_delta(this_week_stats['average_response'], last_week_stats['average_response']) # Put everything in a dict so we can do what we want with it. c_dict = { 'open_tickets': total_open, 'this_week_stats': this_week_stats, 'last_week_stats': last_week_stats, 'delta': delta, } if is_json is False: neighborhoods = Geography.objects.all() c_dict['neighborhoods'] = neighborhoods c_dict['latest'] = most_recent.requested_datetime c = Context(c_dict) return render(request, 'index.html', c) else: data = json.dumps(c_dict, True) return HttpResponse(data, content_type='application/json')
def neighborhood_detail(request, neighborhood_id): """ Show detail for a specific neighborhood. Uses templates/geo_detail.html """ neighborhood = Geography.objects.get(pk=neighborhood_id) nearby = Geography.objects.all().distance(neighborhood.geo) \ .exclude(name=neighborhood.name).order_by('distance')[:5] # Get the requests inside the neighborhood, run the stats requests = Request.objects.filter(geo_point__contained=neighborhood.geo) stats = run_stats(requests) title = neighborhood.name neighborhood.geo.transform(4326) simple_shape = neighborhood.geo.simplify(.0003, preserve_topology=True) c = Context({ 'title': title, 'geometry': simple_shape.geojson, 'centroid': [simple_shape.centroid[0], simple_shape.centroid[1]], 'extent': simple_shape.extent, 'stats': stats, 'nearby': nearby, 'type': 'neighborhood', 'id': neighborhood_id }) return render(request, 'geo_detail.html', c)
def street_view(request, street_id): """ View details for a specific street. Renders geo_detail.html like neighborhood_detail does. """ street = Street.objects.get(pk=street_id) nearby = Street.objects.all().distance(street.line) \ .exclude(street_name=street.street_name).order_by('distance')[:5] neighborhood = Geography.objects.all() \ .distance(street.line).order_by('distance')[:1] # Max/min addresses addresses = [ street.left_low_address, street.left_high_address, street.right_low_address, street.right_high_address ] addresses.sort() title = "%s %i - %i" % (street.street_name, addresses[0], addresses[3]) # Requests requests = Request.objects.filter(street=street_id) stats = run_stats(requests) street.line.transform(4326) c = Context({ 'title': title, 'geometry': street.line.geojson, 'centroid': [street.line.centroid[0], street.line.centroid[1]], 'extent': street.line.extent, 'stats': stats, 'nearby': nearby, 'neighborhood': neighborhood[0], 'type': 'street', 'id': street_id }) return render(request, 'geo_detail.html', c)
def street_view(request, street_id): """ View details for a specific street. Renders geo_detail.html like neighborhood_detail does. """ street = Street.objects.get(pk=street_id) nearby = Street.objects.all().distance(street.line) \ .exclude(street_name=street.street_name).order_by('distance')[:5] neighborhood = Geography.objects.all() \ .distance(street.line).order_by('distance')[:1] # Max/min addresses addresses = [street.left_low_address, street.left_high_address, street.right_low_address, street.right_high_address] addresses.sort() title = "%s %i - %i" % (street.street_name, addresses[0], addresses[3]) # Requests requests = Request.objects.filter(street=street_id) stats = run_stats(requests) street.line.transform(4326) c = Context({ 'title': title, 'geometry': street.line.geojson, 'centroid': [street.line.centroid[0], street.line.centroid[1]], 'extent': street.line.extent, 'stats': stats, 'nearby': nearby, 'neighborhood': neighborhood[0], 'type': 'street', 'id': street_id }) return render(request, 'geo_detail.html', c)