Ejemplo n.º 1
0
def populate_daily_placemetrics(apps, schema_editor):
    Action = apps.get_model("actstream", "Action")
    DailyPlaceMetrics = apps.get_model("indigo_metrics", "DailyPlaceMetrics")
    Country = apps.get_model("indigo_api", "Country")
    Locality = apps.get_model("indigo_api", "Locality")
    db_alias = schema_editor.connection.alias

    places = {}

    for action in Action.objects.using(db_alias).all():
        if action.data and action.data.get('place_code'):
            place_code = action.data['place_code']
            if place_code not in places:
                try:
                    country, locality = RealCountry.get_country_locality(
                        place_code)
                    country = Country.objects.get(pk=country.pk)
                    if locality:
                        locality = Locality.objects.get(pk=locality.pk)
                    places[place_code] = (country, locality)
                except ObjectDoesNotExist:
                    continue

            country, locality = places[place_code]
            metrics, created = DailyPlaceMetrics.objects.get_or_create(
                date=action.timestamp.date(),
                country=country,
                locality=locality,
                place_code=place_code,
            )
            metrics.n_activities += 1
            metrics.save()
Ejemplo n.º 2
0
    def determine_place(self):
        parts = self.kwargs['frbr_uri'].split('/')
        # TODO: split this up into v1 and v2
        place = parts[2] if parts[1] == 'akn' else parts[1]
        try:
            self.country, self.locality = Country.get_country_locality(place)
        except (Country.DoesNotExist, Locality.DoesNotExist):
            raise Http404

        super(FrbrUriViewMixin, self).determine_place()
Ejemplo n.º 3
0
    def record_activity(cls, action):
        place_code = action.data.get('place_code')
        if not place_code:
            return

        try:
            country, locality = Country.get_country_locality(place_code)
        except ObjectDoesNotExist:
            return

        metrics, created = cls.objects.get_or_create(
            date=action.timestamp.date(),
            country=country,
            locality=locality,
            place_code=place_code)

        metrics.n_activities += 1
        metrics.save()
Ejemplo n.º 4
0
 def setUp(self):
     self.za, self.cpt = Country.get_country_locality('za-cpt')
     self.eng = Language.for_code('eng')