Exemple #1
0
def home(request, **kwargs):
    '''
    Renders the main google map.
    One thing that seems to be working well so far, json encoding kwargs and
    using as js options for the map, that way other views can call home and
    pass whatever map options are needed
    '''
    date = int(time())

    # process show id
    loc_id = request.GET.get('show', None)

    # process search string
    search_query = request.GET.get('s', None)

    geo_placename = None
    geo_region = None
    geo_latlng = None
    if loc_id is None:
        location = kwargs.get('location', None)
        if location is not None:
            loc_id = location.id
            del kwargs['location']
            latlng = getattr(location, 'googlemap_point')
            geo_placename, geo_region = get_geo_data(latlng[0], latlng[1])
            geo_latlng = latlng
    else:
        try:
            location = MapObj.objects.get(pk=loc_id)
        except MapObj.DoesNotExist:
            pass
        else:
            latlng = location.json().get('googlemap_point')
            geo_placename, geo_region = get_geo_data(latlng[0], latlng[1])
            geo_latlng = latlng

    # Filter home page locations to building, locations, and groups
    show = map(lambda c: ContentType.objects.get_for_model(c), (Building, Location, Group, ParkingLot, DiningLocation))
    mobs = MapObj.objects.filter(content_type__in=map(lambda c: c.id, show))
    points = {}
    for o in mobs:
        o = o.json()
        points[o['id']] = {
            'name'   : o['name'],
            'gpoint' : o['googlemap_point'],
            'ipoint' : o['illustrated_point'],
            'type'   : o['object_type'],
        }

    """
        Google Maps API caches the KML data. In order to purge that cache,
        the latest MapObj (poly's) modified time is taken and appended to
        the end of the KML link making it unique.
    """
    map_objs = MapObj.objects.order_by('-modified')
    v = str(time())
    if map_objs.count():
        latest_mapobj = map_objs[0]
        v = str(mktime(latest_mapobj.modified.timetuple()))

    if settings.GOOGLE_CAN_SEE_ME:
        buildings_kml = "%s.kml?v=%s" % (request.build_absolute_uri(reverse('locations')[:-1]), v)
        sidewalks_kml = "%s.kml?v=%s" % (request.build_absolute_uri(reverse('sidewalks')[:-1]), v)
        parking_kml   = "%s.kml?v=%s" % (request.build_absolute_uri(reverse('parking')[:-1]), v)
    else:
        buildings_kml = "%s%s.kml?v=%s" % (settings.GOOGLE_LOOK_HERE, reverse('locations')[:-1], v)
        sidewalks_kml = "%s%s.kml?v=%s" % (settings.GOOGLE_LOOK_HERE, reverse('sidewalks')[:-1], v)
        parking_kml   = "%s%s.kml?v=%s" % (settings.GOOGLE_LOOK_HERE, reverse('parking')[:-1], v)
    loc = "%s.json" % reverse('location', kwargs={'loc':'foo'})
    loc = loc.replace('foo', '%s')
    kwargs['map'] = 'gmap';

    error = kwargs.get('error', None)
    if error:
        kwargs.pop('error')

    shuttle_info = ''
    try:
        shuttle_info = SimpleSetting.objects.get(name='shuttle_information')
        shuttle_info = shuttle_info.value
    except SimpleSetting.DoesNotExist:
        pass

    aeds_available = EmergencyAED.objects.all().count() > 0

    ucf_shuttle_api = ShuttleRouteAPI(settings.SHUTTLE_WSDL,
                                      settings.SHUTTLE_APP_CODE,
                                      settings.SHUTTLE_COST_CENTER_ID)
    shuttle_stops = ucf_shuttle_api.get_all_shuttle_stops_dict_from_db()
    shuttle_stops = sorted(shuttle_stops, key=lambda k: k['name'])
    context = {
        'infobox_location_id': json.dumps(loc_id),
        'geo_placename'      : geo_placename,
        'geo_region'         : geo_region,
        'geo_latlng'         : geo_latlng,
        'options'            : json.dumps(kwargs),
        'points'             : json.dumps(points),
        'date'               : date,
        'buildings_kml'      : buildings_kml,
        'sidewalks_kml'      : sidewalks_kml,
        'parking_kml'        : parking_kml,
        'parking_json'       : reverse('parking') + '.json',
        'dining_json'        : reverse('dining') + '.json',
        'loc_url'            : loc,
        'base_url'           : request.build_absolute_uri(reverse('home'))[:-1],
        'error'              : error,
        'shuttle_routes'     : json.dumps(get_shuttle_routes_dict()),
        'shuttle_stops'      : json.dumps(shuttle_stops),
        'shuttle_info'       : shuttle_info,
        'aeds_available'     : aeds_available,
        'cloud_typography'   : settings.CLOUD_TYPOGRAPHY_URL,
        'search_query_get'   : search_query,
        # These points are not displayed on the base tempalte but they
        # still need to be here to be available for searching infoboxes, etc.
        'base_ignore_types'  : json.dumps(['DiningLocation'])
    }

    return render_to_response('campus/base.djt', context, context_instance=RequestContext(request))