# Path: htm-site/htmsite/util.py
# def du(unix):
#     return datetime.utcfromtimestamp(float(unix))
# 
# def get_accident_near(time_start, time_end, intersection, radius=150, request=None):
#     """
#     Return any accidents at this time,
#     should probably be listed in the app
#     :param time:
#     :param intersection:
#     :return:
#     """
# 
#     crashes = request.db.crashes
#     locations = request.db.locations
#     location = locations.find_one({'site_no': intersection})
#     # timestamp = datetime.utcfromtimestamp(float(time))
#     # delta = timedelta(minutes=30)
#     query = {
#         'loc': {
#             '$geoNear': {
#                 '$geometry': location['loc'],
#                 '$maxDistance': radius
#             }
#         },
#     }
#     if time_start is not None:
#         query['datetime'] = {
#             '$gte': time_start,
#             '$lte': time_end
#         }
# 
#     return list(crashes.find(query).sort('datetime', pymongo.ASCENDING)), radius
# 
# Path: htm-site/htmsite/intersection.py
# def _get_intersections(request, images=True):
#     """
#     Get the signalised intersections for Adelaide
#     :return: a cursor of documents of signalised intersections
#     """
#     exclude = {'_id': False}
#     if not images:
#         exclude['scats_diagram'] = False
#     return request.db.locations.find({'site_no': {'$exists': True}}, exclude)


import json
from datetime import timedelta, datetime
from geopy.distance import geodesic
from .util import du, get_accident_near
from .intersection import _get_intersections
from pluck import pluck
from pyramid.view import view_config



@view_config(route_name='crash_investigate', renderer='views/crash.mako', request_method='GET')
def investigate_crash(request):
    intersections = _get_intersections(request, False)
    return {
        'intersections': json.dumps([i for i in intersections if 'loc' in i])
    }


@view_config(route_name='accidents', renderer='bson')
def get_accident_near_json(request):
    args = request.matchdict
    request.response.content_type = 'application/json'
    intersection, time_start, time_end = args['intersection'], du(args['time_start']), du(args['time_end'])
    radius = int(args['radius'])

# Path: htm-site/htmsite/util.py
# def du(unix):
#     return datetime.utcfromtimestamp(float(unix))
# 
# def get_accident_near(time_start, time_end, intersection, radius=150, request=None):
#     """
#     Return any accidents at this time,
#     should probably be listed in the app
#     :param time:
#     :param intersection:
#     :return:
#     """
# 
#     crashes = request.db.crashes
#     locations = request.db.locations
#     location = locations.find_one({'site_no': intersection})
#     # timestamp = datetime.utcfromtimestamp(float(time))
#     # delta = timedelta(minutes=30)
#     query = {
#         'loc': {
#             '$geoNear': {
#                 '$geometry': location['loc'],
#                 '$maxDistance': radius
#             }
#         },
#     }
#     if time_start is not None:
#         query['datetime'] = {
#             '$gte': time_start,
#             '$lte': time_end
#         }
# 
#     return list(crashes.find(query).sort('datetime', pymongo.ASCENDING)), radius
# 
# Path: htm-site/htmsite/intersection.py
# def _get_intersections(request, images=True):
#     """
#     Get the signalised intersections for Adelaide
#     :return: a cursor of documents of signalised intersections
#     """
#     exclude = {'_id': False}
#     if not images:
#         exclude['scats_diagram'] = False
#     return request.db.locations.find({'site_no': {'$exists': True}}, exclude)


# Path: htm-site/htmsite/crashes.py
import json
from datetime import timedelta, datetime
from geopy.distance import geodesic
from .util import du, get_accident_near
from .intersection import _get_intersections
from pluck import pluck
from pyramid.view import view_config




@view_config(route_name='crash_investigate', renderer='views/crash.mako', request_method='GET')
def investigate_crash(request):
    intersections = _get_intersections(request, False)
    return {
        'intersections': json.dumps([i for i in intersections if 'loc' in i])
    }


@view_config(route_name='accidents', renderer='bson')
def get_accident_near_json(request):
    args = request.matchdict
    request.response.content_type = 'application/json'
    intersection, time_start, time_end = args['intersection'], du(args['time_start']), du(args['time_end'])
    radius = int(args['radius'])

    return get_accident_near(time_start, time_end, intersection, radius, request)