Exemple #1
0
 def test_alpha3_override(self):
     with self.settings(
         COUNTRIES_OVERRIDE={
             "AU": None,
             "NZ": {"alpha3": ""},
             "US": {"alpha3": "XXX"},
         }
     ):
         self.assertEqual(countries.alpha3("AU"), "")
         self.assertEqual(countries.alpha3("NZ"), "")
         self.assertEqual(countries.alpha3("US"), "XXX")
Exemple #2
0
 def test_override_complex(self):
     with self.settings(
         COUNTRIES_OVERRIDE={
             "XX": {"names": ["New", "Newer"], "alpha3": "XXX", "numeric": 900},
             "YY": {"name": "y", "numeric": 950},
         }
     ):
         self.assertEqual(countries.name("XX"), "New")
         self.assertEqual(countries.alpha3("XX"), "XXX")
         self.assertEqual(countries.numeric("XX"), 900)
         self.assertEqual(countries.name("YY"), "y")
         self.assertEqual(countries.alpha3("YY"), None)
         self.assertEqual(countries.numeric("YY"), 950)
Exemple #3
0
    def test_changelist_view_context_countries_count_present(self):
        """
        The ``get_request_count`` context contains a dataset for countries
        requests when not filtering by country.
        """
        url = reverse('admin:tracking_analyzer_tracker_changelist')
        request = RequestFactory().get(url)
        request.user = self.user

        response = self.tracker_admin.changelist_view(request)
        self.assertEqual(
            response.context_data['countries_count'],
            '[["{0}", 1], ["{1}", 1], ["{2}", 1]]'.format(
                countries.alpha3(self.tracker_1.ip_country),
                countries.alpha3(self.tracker_3.ip_country),
                countries.alpha3(self.tracker_2.ip_country),
            ))
Exemple #4
0
def load_initial_coutries(apps, schema_editor):
    Country = apps.get_model('coutries', 'Country')
    from django_countries import countries
    for code, name in list(countries):
        alpha3 = countries.alpha3(code)
        Country.objects.create(
            code=alpha3,
            name=name,
        )
Exemple #5
0
def countrycode_from_alpha3(code3):
    """Converts from a three-letter country code to a 2-letter country code if such a
     matching exists.
  """
    for country in countries:
        if countries.alpha3(country[0]) == code3:
            return country[0]

    return None
    def test_changelist_view_context_countries_count_present(self):
        """
        The ``get_request_count`` context contains a dataset for countries
        requests when not filtering by country.
        """
        url = reverse('admin:tracking_analyzer_tracker_changelist')
        request = RequestFactory().get(url)
        request.user = self.user

        response = self.tracker_admin.changelist_view(request)
        self.assertEqual(
            response.context_data['countries_count'],
            '[["{0}", 1], ["{1}", 1], ["{2}", 1]]'.format(
                countries.alpha3(self.tracker_1.ip_country),
                countries.alpha3(self.tracker_3.ip_country),
                countries.alpha3(self.tracker_2.ip_country),
            )
        )
Exemple #7
0
    def changelist_view(self, request, extra_context=None):
        """
        Overrides base ``changelist_view`` method to add analytics datasets to
        the response.
        """
        extra_context = extra_context or {}
        countries_count = []
        response = super(TrackerAdmin,
                         self).changelist_view(request, extra_context)

        if request.method == 'GET':
            # Get the current objects queryset to analyze data from it.
            queryset = response.context_data['cl'].queryset

            # Requests by country (when no filtering by country).
            if 'ip_country__exact' not in request.GET:
                trackers = queryset.values('ip_country').annotate(
                    trackers=Count('id')).order_by()
                for track in trackers:
                    countries_count.append([
                        countries.alpha3(track['ip_country']),
                        track['trackers']
                    ])

                extra_context['countries_count'] = json.dumps(countries_count)

            # Requests by device (when not filtering by device).
            if 'device_type__exact' not in request.GET:
                devices_count = list(
                    queryset.values('device_type').annotate(
                        count=Count('id')).order_by())

                extra_context['devices_count'] = json.dumps(devices_count)

            # Requests time line for the current page changelist.
            current_page = response.context_data['cl'].page_num
            current_pks = response.context_data['cl'].paginator.page(
                current_page + 1).object_list.values_list('pk', flat=True)

            current_results = get_requests_count(
                Tracker.objects.filter(pk__in=current_pks))

            for item in current_results:
                item['date'] = '{date}T{hour:02d}:{minute:02d}'.format(
                    date=item.pop('date'),
                    hour=item.pop('hour'),
                    minute=item.pop('minute'))

            extra_context['requests_count'] = json.dumps(list(current_results))

            response.context_data.update(extra_context)

        return response
    def changelist_view(self, request, extra_context=None):
        """
        Overrides base ``changelist_view`` method to add analytics datasets to
        the response.
        """
        extra_context = extra_context or {}
        countries_count = []
        response = super(TrackerAdmin, self).changelist_view(
            request, extra_context)

        if request.method == 'GET':
            # Get the current objects queryset to analyze data from it.
            queryset = response.context_data['cl'].queryset

            # Requests by country (when no filtering by country).
            if 'ip_country__exact' not in request.GET:
                trackers = queryset.values('ip_country').annotate(
                    trackers=Count('id')).order_by()
                for track in trackers:
                    countries_count.append(
                        [countries.alpha3(track['ip_country']), track['trackers']])

                extra_context['countries_count'] = json.dumps(countries_count)

            # Requests by device (when not filtering by device).
            if 'device_type__exact' not in request.GET:
                devices_count = list(queryset.values('device_type').annotate(
                    count=Count('id')).order_by())

                extra_context['devices_count'] = json.dumps(devices_count)

            # Requests time line for the current page changelist.
            current_page = response.context_data['cl'].page_num
            current_pks = response.context_data['cl'].paginator.page(
                current_page + 1).object_list.values_list('pk', flat=True)

            current_results = get_requests_count(
                Tracker.objects.filter(pk__in=current_pks))

            for item in current_results:
                item['date'] = '{date}T{hour:02d}:{minute:02d}'.format(
                    date=item.pop('date'),
                    hour=item.pop('hour'),
                    minute=item.pop('minute')
                )

            extra_context['requests_count'] = json.dumps(list(current_results))

            response.context_data.update(extra_context)

        return response
Exemple #9
0
    def get_top_countries(self, start_date, end_date):
        trackers = self.trackers \
            .filter(timestamp__gte=start_date, timestamp__lte=end_date) \
            .exclude(country='') \
            .values('country') \
            .annotate(trackers=Count('id')) \
            .order_by('-trackers')

        countries_count = []
        for track in trackers:
            countries_count.append(
                [countries.alpha3(track['country']), track['trackers']])

        return countries_count
Exemple #10
0
    def __init__(self):
        self.countries_name = {name.lower(): code for code, name in countries}
        with override('ru'):
            self.countries_name.update({name.lower(): code for code, name in countries})
        self.countries_name.update({code.lower(): code for code, name in countries})
        self.countries_name.update({countries.alpha3(code).lower(): code for code, name in countries})

        d = defaultdict(list)
        for code, name in countries:
            k = name[:3].lower().strip()
            if k in self.countries_name or len(k) < 3:
                continue
            d[k].append(code)
        for k, v in d.items():
            if len(v) == 1:
                self.countries_name[k] = v[0]

        self.missed_countries = defaultdict(int)
        self.logger = getLogger('ranking.parse.countrier')
Exemple #11
0
 def alpha3(self):
     return countries.alpha3(self.code)
Exemple #12
0
 def alpha3(self):
     return countries.alpha3(self.code)
    def handle(self, *args, **options):
        # Mapping from mispelled country names to the correct ones.
        COUNTRY_NAME_MAP = {
            'European Community': 'European Union',
            # 'European Union': None,
            'Global': None,
            'Korea': 'North Korea',
            'Kosovo': None,
            'Morrocco': 'Morocco',
            'Netherland': 'Netherlands',
            'The Netherlands': 'Netherlands',
            'Russian Federation': 'Russia',
            'UK': 'United Kingdom',
            'England': 'United Kingdom',
            'United States': 'United States of America',
            'Vatican City': None
        }

        EUROPEAN_UNION_COUNTRIES = ('Austria', 'Belgium', 'Bulgaria',
                                    'Croatia', 'Cyprus', 'Czech Republic',
                                    'Denmark', 'Estonia', 'Finland', 'France',
                                    'Germany', 'Greece', 'Hungary', 'Ireland',
                                    'Italy', 'Latvia', 'Lithuania',
                                    'Luxembourg', 'Malta', 'Netherlands',
                                    'Poland', 'Portugal', 'Romania',
                                    'Slovakia', 'Slovenia', 'Spain', 'Sweden',
                                    'United Kingdom')

        with transaction.atomic():
            print 'Correcting country names'
            for country in models.Country.objects.all():
                if country.label in COUNTRY_NAME_MAP:
                    new_label = COUNTRY_NAME_MAP[country.label]
                    print '%s -> %s' % (country.label, new_label)
                    if new_label is not None:
                        # Move all studies to the new country.
                        new_country, _ = models.Country.objects.get_or_create(
                            label=new_label)
                        for study in country.study_set.all():
                            new_country.study_set.add(study)
                    country.delete()

            print 'Splitting European Union'
            eu = models.Country.objects.get(label='European Union')
            for label in EUROPEAN_UNION_COUNTRIES:
                country, _ = models.Country.objects.get_or_create(label=label)
                for study in eu.study_set.all():
                    country.study_set.add(study)
            eu.delete()

            print 'Adding country codes'
            for country in models.Country.objects.all():
                code = countries.by_name(country.label)
                if code == '':
                    print 'Invalid: %s (%d)' % (country,
                                                country.study_set.count())
                    country.delete()
                    continue
                alpha3 = countries.alpha3(code)
                assert alpha3 is not None
                country.alpha3 = alpha3
                country.save()