Ejemplo n.º 1
0
    def determine_place(self):
        try:
            self.country = Country.for_code(self.kwargs['country'])
        except Country.DoesNotExist:
            raise Http404

        super(PublishedDocumentSearchView, self).determine_place()
Ejemplo n.º 2
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.º 3
0
    def get_context_data(self, country_code, **kwargs):
        context = super(LibraryView, self).get_context_data(**kwargs)

        country = Country.for_code(country_code)
        context['country'] = country
        context['country_code'] = country_code
        context['countries'] = Country.objects.select_related(
            'country').prefetch_related('localities', 'publication_set',
                                        'country').all()
        context['countries_json'] = json.dumps(
            {c.code: c.as_json()
             for c in context['countries']})

        serializer = DocumentSerializer(context={'request': self.request},
                                        many=True)
        docs = DocumentViewSet.queryset.filter(work__country=country)
        context['documents_json'] = json.dumps(
            serializer.to_representation(docs))

        serializer = WorkSerializer(context={'request': self.request},
                                    many=True)
        works = WorkViewSet.queryset.filter(country=country)
        context['works_json'] = json.dumps(serializer.to_representation(works))

        return context
Ejemplo n.º 4
0
    def determine_place(self):
        # TODO: this view should support localities, too
        try:
            self.country = Country.for_code(self.kwargs['country'])
        except Country.DoesNotExist:
            raise Http404

        super(PublishedDocumentSearchView, self).determine_place()
Ejemplo n.º 5
0
    def get_queryset(self):
        try:
            country = Country.for_code(self.kwargs['country'])
        except Country.DoesNotExist:
            raise Http404

        queryset = super().get_queryset()
        return queryset.published().filter(work__country=country)
Ejemplo n.º 6
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.º 7
0
    def test_inherit_from_work(self):
        w = Work.objects.create(frbr_uri='/za/act/2009/test',
                                title='Test document',
                                country=Country.for_code('za'))
        d = Document(work=w, expression_date='2011-02-01', language=self.eng)
        d.save()

        d = Document.objects.get(pk=d.id)
        assert_equal(w.frbr_uri, d.frbr_uri)
        assert_equal(w.title, d.title)
Ejemplo n.º 8
0
    def test_as_at_date_max_expression_date(self):
        """ The as-at date for an individual work with points in time after the as-at date,
        is the latest point in time date.
        """
        za = Country.for_code('za')
        za.settings.as_at_date = date(2009, 1, 1)
        za.settings.save()

        response = self.client.get(self.api_path + '/za/act/2010/1.json')
        assert_equal(response.data['as_at_date'], "2012-02-02")
Ejemplo n.º 9
0
    def determine_place(self):
        parts = self.kwargs['place'].split('-', 1)
        country = parts[0]
        locality = parts[1] if len(parts) > 1 else None

        try:
            self.country = Country.for_code(country)
        except Country.DoesNotExist:
            raise Http404

        if locality:
            self.locality = self.country.localities.filter(code=locality).first()
            if not self.locality:
                raise Http404

        self.place = self.locality or self.country
Ejemplo n.º 10
0
    def determine_place(self):
        parts = self.kwargs['frbr_uri'].split('/', 2)[1].split('-', 2)

        # country
        try:
            self.country = Country.for_code(parts[0])
        except Country.DoesNotExist:
            raise Http404

        # locality
        if len(parts) > 1:
            self.locality = self.country.localities.filter(code=parts[1]).first()
            if not self.locality:
                raise Http404

        super(PublishedDocumentDetailView, self).determine_place()
Ejemplo n.º 11
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.º 12
0
    def get(self, request, frbr_uri, authorities, *args, **kwargs):
        # strip /akn from v2 api
        if frbr_uri.startswith('/akn'):
            frbr_uri = frbr_uri[4:]

        try:
            FrbrUri.default_language = None
            self.frbr_uri = FrbrUri.parse(frbr_uri)
        except ValueError:
            return HttpResponseBadRequest("Invalid FRBR URI")

        if not self.frbr_uri.language:
            try:
                country = Country.for_code(self.frbr_uri.country)
                self.frbr_uri.language = country.primary_language.code
            except Country.DoesNotExist:
                self.frbr_uri.language = 'eng'

        self.authorities = self.get_authorities(authorities)
        self.references = self.get_references()

        # redirect if there's only one match,
        # or if the user provided an explicit list of authorities
        # to try
        if self.references and (len(self.references) == 1 or authorities):
            return redirect(self.references[0].url)

        # custom 404?
        if not self.references and authorities:
            try:
                not_found_url = next(a.not_found_url for a in self.authorities
                                     if a.not_found_url)
                return redirect(not_found_url)
            except StopIteration:
                pass

        return super(ResolveView, self).get(self,
                                            frbr_uri=frbr_uri,
                                            authorities=authorities,
                                            *args,
                                            **kwargs)
Ejemplo n.º 13
0
    def get(self, request, frbr_uri, authorities, *args, **kwargs):
        try:
            FrbrUri.default_language = None
            self.frbr_uri = FrbrUri.parse(frbr_uri)
        except ValueError:
            return HttpResponseBadRequest("Invalid FRBR URI")

        if not self.frbr_uri.language:
            try:
                country = Country.for_code(self.frbr_uri.country)
                self.frbr_uri.language = country.primary_language.code
            except Country.DoesNotExist:
                self.frbr_uri.language = 'eng'

        self.authorities = self.get_authorities(authorities)
        self.references = self.get_references()

        # redirect if there's only one match,
        # or if the user provided an explicit list of authorities
        # to try
        if self.references and (len(self.references) == 1 or authorities):
            return redirect(self.references[0].url)

        return super(ResolveView, self).get(self, frbr_uri=frbr_uri, authorities=authorities, *args, **kwargs)
Ejemplo n.º 14
0
 def setUp(self):
     self.za, self.cpt = Country.get_country_locality('za-cpt')
     self.eng = Language.for_code('eng')
Ejemplo n.º 15
0
 def validate_country(self, value):
     try:
         return Country.for_code(value)
     except Country.DoesNotExist:
         raise ValidationError("Invalid country: %s" % value)