Ejemplo n.º 1
0
def records(request):
    query = request.GET.copy()
    records = None

    record_filter = query.pop("filter", ["none"])[0].lower()
    record_format = query.pop("format", ["json"])[0].lower()
    record_sort = query.pop("sort", ["none"])[0].lower()

    cached_records = query.pop("cached", ["true"])[0].lower() in ("yes", "true")
    compress_records = query.pop("compress", ["true"])[0].lower() in ("yes", "true")
    pretty_records = query.pop("pretty", ["false"])[0].lower() in ("yes", "true")

    cache_key = "UI_REQUEST(%s)" % hash_to_query(query)

    geocode_records = query.pop("geocode", ["false"])[0].lower() in ("yes", "true")
    remap_records = query.pop("remap", ["false"])[0].lower() in ("yes", "true")

    if config.UI_CACHE_REQUESTS and cached_records:
        records = models.cache_get_records(cache_key)

    if records is None:
        logger.info("Not using UI cache for %s" % cache_key)
        records = models.query_ls(query=query, cached_records=cached_records)
        if geocode_records:
            records = models.geocode_records(records)
        if remap_records:
            records = models.remap_records(records)
        if config.UI_CACHE_REQUESTS:
            models.cache_set_records(cache_key, records, config.UI_CACHE_TIMEOUT)
    else:
        logger.info("Using UI cache for %s" % cache_key)

    if record_filter in ("default",):
        records = models.filter_default(records)
    if record_sort not in ("none",):
        records = sorted(records, key=lambda v: v.get(record_sort, ""))
    if record_format in ("html",):
        context = {}
        if pretty_records:
            context = {"records": records, "pretty": True}
        else:
            context = {"records": records, "pretty": False}
        response = render(request, "servicesDirectory/records.html", context)
    else:
        content = ""
        if pretty_records or record_format in ("pretty",):
            content = json.dumps(records, sort_keys=True, indent=4)
        else:
            content = json.dumps(records)
        response = HttpResponse(content, content_type="application/json")
    if compress_records:
        gzip = GZipMiddleware()
        return gzip.process_response(request, response)
    else:
        return response
Ejemplo n.º 2
0
def query_ls(query="", cached_records=True):
    try:
        query = simplels_client.hash_to_query(query)
    except:
        pass
    records = None
    if config.LS_CACHE_QUERIES:
        cache_key = "LS_QUERY(%s)" % query
        if cached_records:
            records = cache_get_records(cache_key)
        if records is None:
            records = simplels_client.query(query)
            cache_set_records(cache_key, records)
    else:
        records = simplels_client.query(query)
    return records
Ejemplo n.º 3
0
def query_ls(query="", cached_records=True):
    try:
        query = simplels_client.hash_to_query(query)
    except:
        pass
    records = None
    if config.LS_CACHE_QUERIES:
        cache_key = "LS_QUERY(%s)" % query
        if cached_records:
            records = cache_get_records(cache_key)
        if records is None:
            records = simplels_client.query(query)
            cache_set_records(cache_key, records)
    else:
        records = simplels_client.query(query)
    return records
Ejemplo n.º 4
0
def records(request):
    #UNCOMMENT TO PROFILE - 1 of 2
    #pr = cProfile.Profile()
    #pr.enable()
    
    query = request.GET.copy()
    records = None
    
    record_filter = query.pop("filter", ["none"])[0].lower()
    record_format = query.pop("format", ["json"])[0].lower()
    record_sort = query.pop("sort", ["none"])[0].lower()
    
    #Remove deprecated options
    cached_records = query.pop("cached", ["true"])[0].lower() in ("yes", "true",)
    compress_records = query.pop("compress", ["true"])[0].lower() in ("yes", "true",)
    pretty_records = query.pop("pretty", ["false"])[0].lower() in ("yes", "true",)
    cache_key = "UI_REQUEST(%s)" % hash_to_query(query)
    geocode_records = query.pop("geocode", ["false"])[0].lower() in ("yes", "true",)
    remap_records = query.pop("remap", ["false"])[0].lower() in ("yes", "true",)

    # Hit the cache from previously generated data.
    
    if config.UI_CACHE_REQUESTS and cached_records:
        records = models.cache_get_records(cache_key)

    # Optional (experimental) check for what appears to be obvious under-reporting 
    # in the cache. If it looks dubious, then force a reload of lookup service info.
    # Enable/disable in the config module.

    under_threshold = False

    if records and config.UNDER_REPORT_CHECK:
        t = dict()
        for i in records:
            if not t.has_key(i.get('type')[0]): t[i.get('type')[0]] = 0
            t[i.get('type')[0]] += 1
        if t.get('host') < config.UNDER_REPORT_THRESHOLD:
            under_threshold = True
            cached_records = False

    # Generate data again if cache empty or if the test forces 
    # reload.
        
    if (records is None) or (under_threshold is True):
        #logger.info("Not using UI cache for %s" % cache_key)
        records = models.query_ls(query=query, cached_records=cached_records)
        #if geocode_records:
        #    records = models.geocode_records(records)
        #if remap_records:
        #    records = models.remap_records(records)
        if config.UI_CACHE_REQUESTS:
            models.cache_set_records(cache_key, records, config.UI_CACHE_TIMEOUT)
    #else:
    #    logger.info("Using UI cache for %s" % cache_key)
    
    if record_filter in ("default",):
        records = models.filter_default(records)
    if record_sort not in ("none",):
        records = sorted(records, key=lambda v: v.get(record_sort, ""))
    if record_format in ("html",):
        context = {}
        if pretty_records:
            context = { "records": records, "pretty": True }
        else:
            context = { "records": records, "pretty": False }
        response = render(request, 'servicesDirectory/records.html', context)
    else:
        content = ""
        if pretty_records or record_format in ("pretty",):
            content = json.dumps(records, sort_keys=True, indent=4)
        else:
            content = json.dumps(records)
        response = HttpResponse(content, content_type="application/json")
    
    #UNCOMMENT TO PROFILE - 2 of 2
    #pr.disable()
    #pr.print_stats()

    return response