示例#1
0
    def import_file(self, json_file, user):
        countries_json = json.loads(json_file.read())
        all_countries_codes = countries.countries

        for country in countries_json:
            name = countries_json.get(country, None)
            country = country.upper()
            if name and country in all_countries_codes:
                CountryAlias.get_or_create(country, name, user)
示例#2
0
    def import_file(self, json_file, user):
        countries_json = json.loads(json_file.read())
        all_countries_codes = countries.countries

        for country in countries_json:
            name = countries_json.get(country, None)
            country = country.upper()
            if name and country in all_countries_codes:
                CountryAlias.get_or_create(country, name, user)
示例#3
0
文件: tests.py 项目: Ilhasoft/ureport
    def test_country_alias_get_or_create(self):
        self.assertFalse(CountryAlias.objects.all())

        alias1 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)

        alias2 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)
        self.assertEqual(alias1, alias2)

        alias3 = CountryAlias.get_or_create('RW', '1kHills', self.admin)
        self.assertEqual(CountryAlias.objects.all().count(), 2)
示例#4
0
    def test_country_alias_get_or_create(self):
        self.assertFalse(CountryAlias.objects.all())

        alias1 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)

        alias2 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)
        self.assertEqual(alias1, alias2)

        CountryAlias.get_or_create('RW', '1kHills', self.admin)
        self.assertEqual(CountryAlias.objects.all().count(), 2)
示例#5
0
文件: tests.py 项目: rapidpro/ureport
    def test_country_alias_get_or_create(self):
        self.assertFalse(CountryAlias.objects.all())

        alias1 = CountryAlias.get_or_create("RW", "Awesome", self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)

        alias2 = CountryAlias.get_or_create("RW", "Awesome", self.admin)

        self.assertEqual(CountryAlias.objects.all().count(), 1)
        self.assertEqual(CountryAlias.objects.all().first(), alias1)
        self.assertEqual(alias1, alias2)

        CountryAlias.get_or_create("RW", "1kHills", self.admin)
        self.assertEqual(CountryAlias.objects.all().count(), 2)
示例#6
0
    def get(self, request, *args, **kwargs):
        json_dict = dict(exists='invalid')

        whole_text = request.GET.get('text', '')
        text = CountryAlias.normalize_name(whole_text)

        text_length = len(text)

        country = None
        if text_length == 2:
            try:
                country = pycountry.countries.get(alpha2=text.upper())
            except KeyError:
                pass

        elif text_length == 3:
            try:
                country = pycountry.countries.get(alpha3=text.upper())
            except KeyError:
                pass

        if not country:
            try:
                country = pycountry.countries.get(name=text.title())
            except KeyError:
                pass

        country_code = None
        if not country:
            country = CountryAlias.is_valid(text)
            if country:
                country_code = country.code

        if country and country_code:
            json_dict = dict(exists='valid', country_code=country_code)
        elif country:
            json_dict = dict(exists='valid', country_code=country.alpha2)
        else:
            json_dict['text'] = whole_text

        return HttpResponse(json.dumps(json_dict),
                            status=200,
                            content_type='application/json')
示例#7
0
    def get(self, request, *args, **kwargs):
        json_dict = dict(exists='invalid')

        whole_text = request.GET.get('text', '')
        text = CountryAlias.normalize_name(whole_text)

        text_length = len(text)

        country = None
        if text_length == 2:
            try:
                country = pycountry.countries.get(alpha2=text.upper())
            except KeyError:
                pass

        elif text_length == 3:
            try:
                country = pycountry.countries.get(alpha3=text.upper())
            except KeyError:
                pass

        if not country:
            try:
                country = pycountry.countries.get(name=text.title())
            except KeyError:
                pass

        country_code = None
        if not country:
            country = CountryAlias.is_valid(text)
            if country:
                country_code = country.code

        if country and country_code:
            json_dict = dict(exists='valid', country_code=country_code)
        elif country:
            json_dict = dict(exists='valid', country_code=country.alpha2)
        else:
            json_dict['text'] = whole_text

        return HttpResponse(json.dumps(json_dict), status=200, content_type='application/json')
示例#8
0
文件: tests.py 项目: Ilhasoft/ureport
    def test_list(self):
        list_url = reverse('countries.countryalias_list')

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertLoginRedirect(response)

        self.login(self.superuser)

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 0)

        alias1 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 1)
        self.assertContains(response, "Rwanda (RW)")
        self.assertTrue(alias1 in response.context['object_list'])
示例#9
0
    def test_list(self):
        list_url = reverse('countries.countryalias_list')

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertLoginRedirect(response)

        self.login(self.superuser)

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 0)

        alias1 = CountryAlias.get_or_create('RW', 'Awesome', self.admin)

        response = self.client.get(list_url, SERVER_NAME='nigeria.ureport.io')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['object_list']), 1)
        self.assertContains(response, "Rwanda (RW)")
        self.assertTrue(alias1 in response.context['object_list'])