def clean(self):
        cleaned_data = self.cleaned_data
        query = cleaned_data.get("query")
        countries = cleaned_data.get("countries", [])
        regions = cleaned_data.get("regions")

        # adds query countries to country list
        countries_from_query = []
        if query:
            for country in COUNTRY.keys():
                pattern = re.compile(re.escape(strip_accents(country)), re.IGNORECASE)
                if pattern.search(query):
                    countries_from_query.append(country_to_iso(country.lower()))
        countries.extend(countries_from_query)

        # adds region countries to country list
        if regions:
            country_set = set(countries)
            subregion_set = set(WorldBorder.objects.filter(subregion__in=regions).values_list("iso2", flat=True))
            countries = list(country_set.union(subregion_set))

        cleaned_data["countries_from_query"] = countries_from_query
        cleaned_data["countries"] = countries

        return cleaned_data
    def clean_query(self):
        data = strip_accents(self.cleaned_data["query"])

        for country in COUNTRY.keys():
            pattern = re.compile(re.escape(strip_accents(country)), re.IGNORECASE)
            if pattern.search(data):
                data = pattern.sub("", data, count=1)
        data = " ".join(data.split())

        return data
Example #3
0
def country_to_iso(value):
    countries = dict(zip(map(string.lower, COUNTRY.keys()), COUNTRY.values()))
    return countries.get(value.lower(), "Unknown")
Example #4
0
def country_to_iso(value):
    countries = dict(zip(map(string.lower,COUNTRY.keys()),COUNTRY.values()))
    return countries.get(value.lower(), "Unknown")