def generate_index_entry(args):

            index = {}

            # Generate title
            country_names = None
            if args[2] is not None:
                country_names = [c.name_en for c in args[2]]

            title = get_title(
                api_name=args[0],
                app_name=args[1],
                country_names=country_names,
                trade_flow=args[3],
                years=args[4],
                product_name=args[5].name_en if args[5] is not None else None
            )
            index["title"] = title

            # Generate url
            country_codes = None
            if args[2] is not None:
                country_codes = [c.name_3char.lower() for c in args[2]]

            url = params_to_url(
                api_name=args[0],
                app_name=args[1][0],
                country_codes=country_codes,
                trade_flow=args[3],
                years=args[4],
                product_code=args[5].code if args[5] is not None else None
            )
            index["url"] = url

            regions = None
            if args[2] is not None:
                regions = [c.region.name for c in args[2]]

            # Add in params into elasticsearch in case we need them later
            kwargs = dict(
                api_name=args[0],
                app_name=args[1],
                country_names=country_names,
                country_codes=country_codes,
                regions=regions,
                trade_flow=args[3],
                years=args[4],
                product_name=args[5].name_en if args[5] is not None else None,
                product_code=args[5].code if args[5] is not None else None,
                product_community=args[5].community.name if args[5] is not None else None,
            )
            kwargs = {k: v for k, v in kwargs.iteritems() if v is not None}
            index.update(kwargs)

            return index
Exemple #2
0
        def generate_index_entry(args):

            index = {}

            # Generate title
            country_names = None
            if args[2] is not None:
                country_names = [c.name_en for c in args[2]]

            title = get_title(
                api_name=args[0],
                app_name=args[1],
                country_names=country_names,
                trade_flow=args[3],
                years=args[4],
                product_name=args[5].name_en if args[5] is not None else None)
            index["title"] = title

            # Generate url
            country_codes = None
            if args[2] is not None:
                country_codes = [c.name_3char.lower() for c in args[2]]

            url = params_to_url(
                api_name=args[0],
                app_name=args[1][0],
                country_codes=country_codes,
                trade_flow=args[3],
                years=args[4],
                product_code=args[5].code if args[5] is not None else None)
            index["url"] = url

            regions = None
            if args[2] is not None:
                regions = [c.region.name for c in args[2]]

            # Add in params into elasticsearch in case we need them later
            kwargs = dict(
                api_name=args[0],
                app_name=args[1],
                country_names=country_names,
                country_codes=country_codes,
                regions=regions,
                trade_flow=args[3],
                years=args[4],
                product_name=args[5].name_en if args[5] is not None else None,
                product_code=args[5].code if args[5] is not None else None,
                product_community=args[5].community.name
                if args[5] is not None else None,
            )
            kwargs = {k: v for k, v in kwargs.iteritems() if v is not None}
            index.update(kwargs)

            return index
def api_search(request):

    query = request.GET.get("term", None)
    if query is None:
        return HttpResponse("[]")

    # For user experiment, run search version 1 or 2, 2 being more feature
    # rich and having parsed filters. See atlas-data#32
    search_version = int(request.GET.get("search_var", 0))

    # Parse search query
    query, query_type, kwargs = parse_search(
        query, strip_keywords=(search_version != 1))

    # Resolve any synonyms. feasibility -> pie_scatter etc.
    if "app_name" in kwargs:
        given_app_name = kwargs["app_name"][0]
        kwargs["app_name"] = [
            APP_NAME_SYNONYMS.get(given_app_name, given_app_name)
        ]

    # Viz params are not an elasticsearch filter so pop that off
    viz_params = kwargs.pop("viz_params", None)

    # Prepare elasticsearch filters
    if search_version == 2 or search_version == 0:
        filters = prepare_filters(kwargs)
    else:
        filters = {}

    es_query = {"query": {"filtered": {}}, "size": 8}

    # Add filters to the query if they were given. Filters are ANDed.
    if len(filters) > 0:
        es_filters = [{
            "terms": {
                k: [x.lower() for x in v]
            }
        } for k, v in filters.iteritems()]
        es_filters = {"bool": {"must": es_filters}}
        es_query["query"]["filtered"]["filter"] = es_filters

    # Add fuzzy search for query string if any non-filter query string remains
    # after taking out the filters
    if query.strip() != "":
        es_query["query"]["filtered"]["query"] = {
            "fuzzy_like_this": {
                "like_text": query,
                "fields": ["title"],
                "max_query_terms": 15,
                "prefix_length": 3
            }
        }

    # Do the query
    es = Elasticsearch()
    result = es.search(index="questions", body=es_query)

    # Format the results in a way that complies with the OpenSearch standard's
    # suggestion extension
    labels = []
    urls = []
    for x in result['hits']['hits']:
        data = x['_source']

        # Regenerate title and url so we can add stuff into it dynamically,
        # like the year being searched for, or forcing an app.
        years = kwargs.get('years', None)

        # Possible apps this title could be visualized as
        app_names = data['app_name']

        # If the app the user requested is possible, use that. Otherwise, use
        # the first one as default. App names in the elasticsearch index are
        # sorted in a certain way for this to make sense so check out the
        # indexer script
        requested_app_name = filters.get("app_name", [None])[0]
        if requested_app_name in app_names:
            app_name = requested_app_name
        else:
            app_name = app_names[0]

        if years and len(years) == 2:
            if app_name in ["map", "tree_map"]:
                # If multiple years are specified and we can do a stacked
                # graph, do a stacked graph instead of a treemap or map
                app_name = "stacked"
            elif app_name in ["product_space", "pie_scatter"]:
                # Some apps can never have multiple years so just use the first
                # one specified
                years = [years[0]]

        # If no years specified, use default years
        if years is None:
            if app_name == "stacked":
                years = [settings.YEAR_MIN_HS4, settings.YEAR_MAX_HS4]
            else:
                years = [settings.YEAR_MAX_HS4]

        # You can't show a product space based on imports so ignore those
        if app_name == "product_space" and data["trade_flow"] == "import":
            continue

        title = get_title(api_name=data['api_name'],
                          app_name=app_name,
                          country_names=data.get('country_names', None),
                          trade_flow=data['trade_flow'],
                          years=years,
                          product_name=data.get('product_name', None))
        url = params_to_url(api_name=data['api_name'],
                            app_name=app_name,
                            country_codes=data.get('country_codes', None),
                            trade_flow=data['trade_flow'],
                            years=years,
                            product_code=data.get('product_code', None))

        if viz_params:
            if app_name == "pie_scatter":
                url += "?queryActivated=True"
                url += "&yaxis=%s" % viz_params[0]

        labels.append(title)
        urls.append(settings.HTTP_HOST + url)

    return HttpResponse(json.dumps([query, labels, [], urls]))
def api_search(request):

    query = request.GET.get("term", None)
    if query is None:
        return HttpResponse("[]")

    # For user experiment, run search version 1 or 2, 2 being more feature
    # rich and having parsed filters. See atlas-data#32
    search_version = int(request.GET.get("search_var", 0))

    # Parse search query
    query, query_type, kwargs = parse_search(
        query,
        strip_keywords=(search_version != 1))

    # Resolve any synonyms. feasibility -> pie_scatter etc.
    if "app_name" in kwargs:
        given_app_name = kwargs["app_name"][0]
        kwargs["app_name"] = [APP_NAME_SYNONYMS.get(given_app_name,
                                                    given_app_name)]

    # Viz params are not an elasticsearch filter so pop that off
    viz_params = kwargs.pop("viz_params", None)

    # Prepare elasticsearch filters
    if search_version == 2 or search_version == 0:
        filters = prepare_filters(kwargs)
    else:
        filters = {}

    es_query = {
        "query": {
            "filtered": {}
        },
        "size": 8
    }

    # Add filters to the query if they were given. Filters are ANDed.
    if len(filters) > 0:
        es_filters = [{"terms": {k: [x.lower() for x in v]}}
                      for k, v in filters.iteritems()]
        es_filters = {"bool": {"must": es_filters}}
        es_query["query"]["filtered"]["filter"] = es_filters

    # Add fuzzy search for query string if any non-filter query string remains
    # after taking out the filters
    if query.strip() != "":
        es_query["query"]["filtered"]["query"] = {
            "fuzzy_like_this": {
                "like_text": query,
                "fields": ["title"],
                "max_query_terms": 15,
                "prefix_length": 3
            }
        }

    # Do the query
    es = Elasticsearch()
    result = es.search(index="questions", body=es_query)

    # Format the results in a way that complies with the OpenSearch standard's
    # suggestion extension
    labels = []
    urls = []
    for x in result['hits']['hits']:
        data = x['_source']

        # Regenerate title and url so we can add stuff into it dynamically,
        # like the year being searched for, or forcing an app.
        years = kwargs.get('years', None)

        # Possible apps this title could be visualized as
        app_names = data['app_name']

        # If the app the user requested is possible, use that. Otherwise, use
        # the first one as default. App names in the elasticsearch index are
        # sorted in a certain way for this to make sense so check out the
        # indexer script
        requested_app_name = filters.get("app_name", [None])[0]
        if requested_app_name in app_names:
            app_name = requested_app_name
        else:
            app_name = app_names[0]

        if years and len(years) == 2:
            if app_name in ["map", "tree_map"]:
                # If multiple years are specified and we can do a stacked
                # graph, do a stacked graph instead of a treemap or map
                app_name = "stacked"
            elif app_name in ["product_space", "pie_scatter"]:
                # Some apps can never have multiple years so just use the first
                # one specified
                years = [years[0]]

        # If no years specified, use default years
        if years is None:
            if app_name == "stacked":
                years = [settings.YEAR_MIN_HS4, settings.YEAR_MAX_HS4]
            else:
                years = [settings.YEAR_MAX_HS4]

        # You can't show a product space based on imports so ignore those
        if app_name == "product_space" and data["trade_flow"] == "import":
            continue

        title = get_title(
            api_name=data['api_name'],
            app_name=app_name,
            country_names=data.get('country_names', None),
            trade_flow=data['trade_flow'],
            years=years,
            product_name=data.get('product_name', None)
        )
        url = params_to_url(
            api_name=data['api_name'],
            app_name=app_name,
            country_codes=data.get('country_codes', None),
            trade_flow=data['trade_flow'],
            years=years,
            product_code=data.get('product_code', None)
        )

        if viz_params:
            if app_name == "pie_scatter":
                url += "?queryActivated=True"
                url += "&yaxis=%s" % viz_params[0]

        labels.append(title)
        urls.append(settings.HTTP_HOST + url)

    return HttpResponse(json.dumps([
        query,
        labels,
        [],
        urls
    ]))
def explore(
        request,
        app_name,
        trade_flow,
        country1,
        country2,
        product,
        year="2011"):

    request.session['app_name'] = app_name

    was_redirected = request.GET.get("redirect", False)
    lang = helpers.get_language(request)['code']
    crawler = request.GET.get("_escaped_fragment_", False)

    # Get session / request vars
    prod_class = request.GET.get("prod_class",
                                 request.session.get('product_classification',
                                                     'hs4'))

    options = request.GET.copy()
    options["lang"] = lang
    options["product_classification"] = prod_class
    options = options.urlencode()

    # Setup the hash dictionary
    request_hash_dictionary = collections.OrderedDict()

    # Add prod class to request hash dictionary
    request_hash_dictionary['app_name'] = app_name
    request_hash_dictionary['lang'] = lang
    request_hash_dictionary['prod_class'] = prod_class

    # Add the arguments to the request hash dictionary
    request_hash_dictionary['trade_flow'] = trade_flow
    request_hash_dictionary['country1'] = country1
    request_hash_dictionary['country2'] = country2
    request_hash_dictionary['product_type'] = product
    request_hash_dictionary['year'] = year

    # We are here, so let us store this data somewhere
    request_hash_string = "_".join(request_hash_dictionary.values())

    # Code for showing a static image or not
    static_image_name = helpers.url_to_hash(request.path, request.GET)
    if os.path.exists(os.path.join(settings.STATIC_IMAGE_PATH,
                                   static_image_name + ".png")):
        displayviz = True
        displayImage = static_image_name + ".png"
    else:
        displayviz = False
        displayImage = settings.STATIC_URL + "img/all/loader.gif"

    # Verify countries from DB
    countries = [None, None]
    alert = None
    for i, country in enumerate([country1, country2]):
        if country != "show" and country != "all":
            try:
                countries[i] = Country.objects.get(name_3char=country)
            except Country.DoesNotExist:
                alert = {
                    "title": "Country could not be found",
                    "text": """There was no country with the 3 letter
                    abbreviation <strong>%s</strong>. Please double check the
                    <a href='about/data/country/'>list of countries</a>.""" %
                    (country)}

    # The years of data available tends to vary based on the dataset used (Hs4
    # vs Sitc4) and the specific country.
    years_available_model = Sitc4_cpy if prod_class == "sitc4" else Hs4_cpy
    years_available = years_available_model.objects\
        .values_list("year", flat=True)\
        .order_by("year")\
        .distinct()
    # Sometimes the query is not about a specific country (e.g. "all countries"
    # queries) in which case filtering by country is not necessary
    if countries[0]:
        years_available = years_available.filter(country=countries[0].id)
    # Force lazy queryset to hit the DB to reduce number of DB queries later
    years_available = list(years_available)

    if len(years_available) == 0:
        alert = {"title": """The product classification you're using (%s) does
               not seem to include data for the country code (%s) you selected.
               Please try a different product classification or country.""" %
                 (prod_class, countries[0].name)}
        years_available = range(1995, 2012)  # Dummy

    year1_list, year2_list = None, None
    warning, title = None, None
    data_as_text = {}

    prod_or_partner = "partner"

    # To make sure it cannot be another product class
    if prod_class != "hs4" and prod_class != "sitc4":
        prod_class = "sitc4"

    # Test for country exceptions
    if prod_class == "hs4":
        # redirect if and exception country
        if country1 == "bel" or country1 == "lux":
            return redirect(
                HTTP_HOST+'explore/%s/%s/blx/%s/%s/%s/?redirect=true' %
                (app_name, trade_flow, country2, product, year))
        if country1 == "bwa" or country1 == "lso" or country1 == "nam" or country1 == "swz":
            return redirect(
                HTTP_HOST+'explore/%s/%s/zaf/%s/%s/%s/?redirect=true' %
                (app_name, trade_flow, country2, product, year))
    if was_redirected:
        # display warning is redirected from exception
        if country1 == "blx":
            warning = {
                "title": "Country Substitution", "text":
                "In the Harmonized System (HS) classification, trade for Belgium and Luxembourg is reported as 'Belgium-Luxembourg'."}
        if country1 == "zaf":
            warning = {
                "title": "Country Substitution", "text":
                "In the Harmonized System (HS) classification, trade for Namibia, Republic of South Africa, Botswana, Lesotho and Swaziland is reported under 'South African Customs Union'."}

    trade_flow_list = [
        ("export", _("Export")), ("import", _("Import")),
        ("net_export", _("Net Export")), ("net_import", _("Net Import"))]
    if (app_name == "product_space" or app_name == "country_space" or app_name == "rings"):
        trade_flow_list = [trade_flow_list[0]]

    year1_list = range(
        years_available[0],
        years_available[
            len(years_available) -
            1] +
        1,
        1)

    if app_name == "stacked" and year == "2009":
        year = "1969.2011.10"

    if "." in year:
        y = [int(x) for x in year.split(".")]
        year_start = y[0]
        year_end = y[1]
        year2_list = year1_list
    else:
        year_start, year_end = None, None
        year = int(year)
        # Check that year is within bounds
        if year > years_available[len(years_available)-1]:
            year = years_available[len(years_available)-1]
        elif year < years_available[0]:
            year = years_available[0]

    api_uri = "api/%s/%s/%s/%s/%s/?%s" % (trade_flow,
                                          country1,
                                          country2,
                                          product,
                                          year,
                                          options)

    redesign_api_uri = "redesign/api/%s/%s/%s/%s/%s/%s" % (prod_class,
                                                           trade_flow,
                                                           country1,
                                                           country2,
                                                           product,
                                                           year)

    country_code = None
    if country1 != "show" and country1 != "all":
        country_code = country1

    if crawler == "":
        view, args, kwargs = resolve(
            "api/%s/%s/%s/%s/%s/" %
            (trade_flow, country1, country2, product, year))
        kwargs['request'] = request
        view_response = view(*args, **kwargs)
        raise Exception(view_response)
        data_as_text["data"] = view_response[0]
        data_as_text["total_value"] = view_response[1]
        data_as_text["columns"] = view_response[2]

    app_type = helpers.get_app_type(country1, country2, product, year)

    # What is actually being shown on the page
    if app_type == "csay" or app_type == "sapy":
      item_type = "country"
    else:
      item_type = "product"


    # Some countries need "the" before their names
    list_countries_the = set(
        ("Cayman Islands",
         "Central African Republic",
         "Channel Islands",
         "Congo, Dem. Rep.",
         "Czech Republic",
         "Dominican Republic",
         "Faeroe Islands",
         "Falkland Islands",
         "Fm Yemen Dm",
         "Lao PDR",
         "Marshall Islands",
         "Philippines",
         "Seychelles",
         "Slovak Republic",
         "Syrian Arab Republic",
         "Turks and Caicos Islands",
         "United Arab Emirates",
         "United Kingdom",
         "Virgin Islands, U.S.",
         "United States"))
    if countries[0] and countries[0].name in list_countries_the:
        countries[0].name = "the "+countries[0].name

    if product not in ("show", "all"):
        p_code = product
        product = helpers.get_product_by_code(p_code, prod_class)

    if not alert:

        # Generate page title depending on visualization being used
        years = [year_start, year_end] if year_start is not None else [year]
        product_name = product.name_en if not isinstance(
            product,
            basestring) else product
        country_names = [getattr(x, "name", None) for x in countries]
        title = helpers.get_title(app_type, app_name,
                                  country_names=country_names,
                                  trade_flow=trade_flow.replace('_', ' '),
                                  years=years,
                                  product_name=product_name
                                  )

        if app_type in ("ccsy", "cspy"):
            if _("net_export") in trade_flow_list:
                del trade_flow_list[trade_flow_list.index(_("net_export"))]
            if _("net_import") in trade_flow_list:
                del trade_flow_list[trade_flow_list.index(_("net_import"))]

        # Should we show the product or partner tab pane?
        # quick fix should be merged with item_type
        if app_type in ["cspy", "sapy"]:
            prod_or_partner = "product"
        elif app_type == "casy":
            if app_name in ("stacked", "map", "tree_map", "pie_scatter", "product_space", "country_space", "rankings", "scatterplot"):
                prod_or_partner = "product"

    # Record views in redis for "newest viewed pages" visualization
    if isinstance(cache, RedisCache):
        views_image_path = settings.STATIC_URL + \
            "data/" + request_hash_string + ".png"
        view_data = {
            "timestamp": time.time(),
            "image": views_image_path,
            "title": title,
            "url": request.build_absolute_uri()
        }
        r = cache.raw_client
        r.rpush("views", json.dumps(view_data))

    previous_page = request.META.get('HTTP_REFERER',
                                           None),

    if previous_page[0] is not None:

      previous_page = previous_page[0]
      previous_image = helpers.url_to_hash(urlparse(previous_page).path, {})

      if os.path.exists(os.path.join(settings.STATIC_IMAGE_PATH,
                                     previous_image + ".png")):
        previous_image = previous_image + ".png"
      else:
        previous_image = settings.STATIC_URL + "img/all/loader.gif"

    else:
      previous_image = settings.STATIC_URL + "img/all/loader.gif"
      previous_page = None

    return render_to_response(
        "explore/index.html",
        {"lang": lang,
         "warning": warning,
         "alert": alert,
         "prod_class": prod_class,
         "data_as_text": data_as_text,
         "app_name": app_name,
         "title": title,
         "trade_flow": trade_flow,
         "country1": countries[0] or country1,
         "country2": countries[1] or country2,
         "country1_3char": countries[0].name_3char if countries[0] else "",
         "country2_3char": countries[1].name_3char if countries[1] else "",
         "product": product,
         "product_code": product.code if not isinstance(product, basestring) else product,
         "years_available": years_available,
         "year": year,
         "year_start": year_start,
         "year_end": year_end,
         "year1_list": year1_list,
         "year2_list": year2_list,
         "trade_flow_list": trade_flow_list,
         "api_uri": api_uri,
         "app_type": app_type,
         "redesign_api_uri": redesign_api_uri,
         "country_code": country_code,
         "prod_or_partner": prod_or_partner,
         "version": VERSION,
         "previous_page": previous_page,
         "previous_image": previous_image,
         "item_type": item_type,
         "displayviz": displayviz,
         "displayImage": displayImage,
         "display_iframe": request.GET.get("display_iframe", False),
         "static_image": request.GET.get("static_image", "loading"),
         },
        context_instance=RequestContext(request))
def explore(request,
            app_name,
            trade_flow,
            country1,
            country2,
            product,
            year="2011"):

    request.session['app_name'] = app_name

    was_redirected = request.GET.get("redirect", False)
    lang = helpers.get_language(request)['code']
    crawler = request.GET.get("_escaped_fragment_", False)

    # Get session / request vars
    prod_class = request.GET.get(
        "prod_class", request.session.get('product_classification', 'hs4'))

    options = request.GET.copy()
    options["lang"] = lang
    options["product_classification"] = prod_class
    options = options.urlencode()

    # Setup the hash dictionary
    request_hash_dictionary = collections.OrderedDict()

    # Add prod class to request hash dictionary
    request_hash_dictionary['app_name'] = app_name
    request_hash_dictionary['lang'] = lang
    request_hash_dictionary['prod_class'] = prod_class

    # Add the arguments to the request hash dictionary
    request_hash_dictionary['trade_flow'] = trade_flow
    request_hash_dictionary['country1'] = country1
    request_hash_dictionary['country2'] = country2
    request_hash_dictionary['product_type'] = product
    request_hash_dictionary['year'] = year

    # We are here, so let us store this data somewhere
    request_hash_string = "_".join(request_hash_dictionary.values())

    # Code for showing a static image or not
    static_image_name = helpers.url_to_hash(request.path, request.GET)
    if os.path.exists(
            os.path.join(settings.STATIC_IMAGE_PATH,
                         static_image_name + ".png")):
        displayviz = True
        displayImage = static_image_name + ".png"
    else:
        displayviz = False
        displayImage = settings.STATIC_URL + "img/all/loader.gif"

    # Verify countries from DB
    countries = [None, None]
    alert = None
    for i, country in enumerate([country1, country2]):
        if country != "show" and country != "all":
            try:
                countries[i] = Country.objects.get(name_3char=country)
            except Country.DoesNotExist:
                alert = {
                    "title":
                    "Country could not be found",
                    "text":
                    """There was no country with the 3 letter
                    abbreviation <strong>%s</strong>. Please double check the
                    <a href='about/data/country/'>list of countries</a>.""" %
                    (country)
                }

    # The years of data available tends to vary based on the dataset used (Hs4
    # vs Sitc4) and the specific country.
    years_available_model = Sitc4_cpy if prod_class == "sitc4" else Hs4_cpy
    years_available = years_available_model.objects\
        .values_list("year", flat=True)\
        .order_by("year")\
        .distinct()
    # Sometimes the query is not about a specific country (e.g. "all countries"
    # queries) in which case filtering by country is not necessary
    if countries[0]:
        years_available = years_available.filter(country=countries[0].id)
    # Force lazy queryset to hit the DB to reduce number of DB queries later
    years_available = list(years_available)

    if len(years_available) == 0:
        alert = {
            "title":
            """The product classification you're using (%s) does
               not seem to include data for the country code (%s) you selected.
               Please try a different product classification or country.""" %
            (prod_class, countries[0].name)
        }
        years_available = range(1995, 2012)  # Dummy

    year1_list, year2_list = None, None
    warning, title = None, None
    data_as_text = {}

    prod_or_partner = "partner"

    # To make sure it cannot be another product class
    if prod_class != "hs4" and prod_class != "sitc4":
        prod_class = "sitc4"

    # Test for country exceptions
    if prod_class == "hs4":
        # redirect if and exception country
        if country1 == "blx" or country1 == "lux":
            return redirect(HTTP_HOST +
                            'explore/%s/%s/bel/%s/%s/%s/?redirect=true' %
                            (app_name, trade_flow, country2, product, year))
        if country1 == "bwa" or country1 == "lso" or country1 == "nam" or country1 == "swz":
            return redirect(HTTP_HOST +
                            'explore/%s/%s/zaf/%s/%s/%s/?redirect=true' %
                            (app_name, trade_flow, country2, product, year))
    if was_redirected:
        # display warning is redirected from exception
        if country1 == "bel":
            warning = {
                "title":
                "Country Substitution",
                "text":
                "In the Harmonized System (HS) classification, trade for Belgium and Luxembourg is reported under 'BEL' ."
            }
        if country1 == "zaf":
            warning = {
                "title":
                "Country Substitution",
                "text":
                "In the Harmonized System (HS) classification, trade for Namibia, Republic of South Africa, Botswana, Lesotho and Swaziland is reported under 'South African Customs Union'."
            }

    trade_flow_list = [("export", _("Export")), ("import", _("Import")),
                       ("net_export", _("Net Export")),
                       ("net_import", _("Net Import"))]
    if (app_name == "product_space" or app_name == "country_space"
            or app_name == "rings"):
        trade_flow_list = [trade_flow_list[0]]

    year1_list = range(years_available[0],
                       years_available[len(years_available) - 1] + 1, 1)

    if app_name == "stacked" and year == "2009":
        year = "1969.2011.10"

    if "." in year:
        y = [int(x) for x in year.split(".")]
        year_start = y[0]
        year_end = y[1]
        year2_list = year1_list
    else:
        year_start, year_end = None, None
        year = int(year)
        # Check that year is within bounds
        if year > years_available[len(years_available) - 1]:
            year = years_available[len(years_available) - 1]
        elif year < years_available[0]:
            year = years_available[0]

    api_uri = "api/%s/%s/%s/%s/%s/?%s" % (trade_flow, country1, country2,
                                          product, year, options)

    redesign_api_uri = "redesign/api/%s/%s/%s/%s/%s/%s" % (
        prod_class, trade_flow, country1, country2, product, year)

    country_code = None
    if country1 != "show" and country1 != "all":
        country_code = country1

    if crawler == "":
        view, args, kwargs = resolve(
            "api/%s/%s/%s/%s/%s/" %
            (trade_flow, country1, country2, product, year))
        kwargs['request'] = request
        view_response = view(*args, **kwargs)
        raise Exception(view_response)
        data_as_text["data"] = view_response[0]
        data_as_text["total_value"] = view_response[1]
        data_as_text["columns"] = view_response[2]

    app_type = helpers.get_app_type(country1, country2, product, year)

    # What is actually being shown on the page
    if app_type == "csay" or app_type == "sapy":
        item_type = "country"
    else:
        item_type = "product"

    # Some countries need "the" before their names
    list_countries_the = set(
        ("Cayman Islands", "Central African Republic", "Channel Islands",
         "Congo, Dem. Rep.", "Czech Republic", "Dominican Republic",
         "Faeroe Islands", "Falkland Islands", "Fm Yemen Dm", "Lao PDR",
         "Marshall Islands", "Philippines", "Seychelles", "Slovak Republic",
         "Syrian Arab Republic", "Turks and Caicos Islands",
         "United Arab Emirates", "United Kingdom", "Virgin Islands, U.S.",
         "United States"))
    if countries[0] and countries[0].name in list_countries_the:
        countries[0].name = "the " + countries[0].name

    if product not in ("show", "all"):
        p_code = product
        product = helpers.get_product_by_code(p_code, prod_class)

    if not alert:

        # Generate page title depending on visualization being used
        years = [year_start, year_end] if year_start is not None else [year]
        product_name = product.name_en if not isinstance(
            product, basestring) else product
        country_names = [getattr(x, "name", None) for x in countries]
        title = helpers.get_title(app_type,
                                  app_name,
                                  country_names=country_names,
                                  trade_flow=trade_flow.replace('_', ' '),
                                  years=years,
                                  product_name=product_name)

        if app_type in ("ccsy", "cspy"):
            if _("net_export") in trade_flow_list:
                del trade_flow_list[trade_flow_list.index(_("net_export"))]
            if _("net_import") in trade_flow_list:
                del trade_flow_list[trade_flow_list.index(_("net_import"))]

        # Should we show the product or partner tab pane?
        # quick fix should be merged with item_type
        if app_type in ["cspy", "sapy"]:
            prod_or_partner = "product"
        elif app_type == "casy":
            if app_name in ("stacked", "map", "tree_map", "pie_scatter",
                            "product_space", "country_space", "rankings",
                            "scatterplot"):
                prod_or_partner = "product"

    # Record views in redis for "newest viewed pages" visualization
    if isinstance(cache, RedisCache):
        views_image_path = settings.STATIC_URL + \
            "data/" + request_hash_string + ".png"
        view_data = {
            "timestamp": time.time(),
            "image": views_image_path,
            "title": title,
            "url": request.build_absolute_uri()
        }
        r = cache.raw_client
        r.rpush("views", json.dumps(view_data))

    previous_page = request.META.get('HTTP_REFERER', None),

    if previous_page[0] is not None:

        previous_page = previous_page[0]
        previous_image = helpers.url_to_hash(urlparse(previous_page).path, {})

        if os.path.exists(
                os.path.join(settings.STATIC_IMAGE_PATH,
                             previous_image + ".png")):
            previous_image = previous_image + ".png"
        else:
            previous_image = settings.STATIC_URL + "img/all/loader.gif"

    else:
        previous_image = settings.STATIC_URL + "img/all/loader.gif"
        previous_page = None

    return render_to_response("explore/index.html", {
        "lang":
        lang,
        "warning":
        warning,
        "alert":
        alert,
        "prod_class":
        prod_class,
        "data_as_text":
        data_as_text,
        "app_name":
        app_name,
        "title":
        title,
        "trade_flow":
        trade_flow,
        "country1":
        countries[0] or country1,
        "country2":
        countries[1] or country2,
        "country1_3char":
        countries[0].name_3char if countries[0] else "",
        "country2_3char":
        countries[1].name_3char if countries[1] else "",
        "product":
        product,
        "product_code":
        product.code if not isinstance(product, basestring) else product,
        "years_available":
        years_available,
        "year":
        year,
        "year_start":
        year_start,
        "year_end":
        year_end,
        "year1_list":
        year1_list,
        "year2_list":
        year2_list,
        "trade_flow_list":
        trade_flow_list,
        "api_uri":
        api_uri,
        "app_type":
        app_type,
        "redesign_api_uri":
        redesign_api_uri,
        "country_code":
        country_code,
        "prod_or_partner":
        prod_or_partner,
        "version":
        VERSION,
        "previous_page":
        previous_page,
        "previous_image":
        previous_image,
        "item_type":
        item_type,
        "displayviz":
        displayviz,
        "displayImage":
        displayImage,
        "display_iframe":
        request.GET.get("display_iframe", False),
        "static_image":
        request.GET.get("static_image", "loading"),
    },
                              context_instance=RequestContext(request))