def test_manifestation_uri(self): uri = FrbrUri.parse("/za/act/1980/02/eng") uri.expression_date = '@2014-01-01' uri.expression_component = 'main' uri.format = 'html' assert_equal("/za/act/1980/02/eng@2014-01-01/main.html", uri.manifestation_uri())
def resolve(request, frbr_uri): try: frbr_uri = FrbrUri.parse(frbr_uri) except ValueError: return HttpResponseBadRequest("Invalid FRBR URI") # TODO: handle expression URIs and dates? references = list( AuthorityReference.objects.filter( frbr_uri__in=[frbr_uri.work_uri( ), frbr_uri.expression_uri()]).prefetch_related('authority').all()) shuffle(references) return render( request, 'resolve.html', { 'query': { 'frbr_uri': frbr_uri, # TODO: more generic 'title': "%s %s of %s" % (frbr_uri.doctype.title(), frbr_uri.number, frbr_uri.date), 'type': frbr_uri.subtype or frbr_uri.doctype, }, 'references': references, 'settings': settings, })
def test_parse_expression(self): uri = FrbrUri.parse("/za/act/1980/02/afr@") assert_equal(uri.language, "afr") assert_equal(uri.expression_date, '@') assert_equal("/za/act/1980/02", uri.work_uri()) assert_equal("/za/act/1980/02/afr@", uri.expression_uri()) uri = FrbrUri.parse("/za/act/1980/02/afr@2014-01-01") assert_equal(uri.language, "afr") assert_equal(uri.expression_date, "@2014-01-01") assert_equal("/za/act/1980/02", uri.work_uri()) assert_equal("/za/act/1980/02/afr@2014-01-01", uri.expression_uri()) uri = FrbrUri.parse("/za/act/1980/02/afr.html") assert_equal(uri.language, "afr") assert_equal(uri.format, 'html')
def test_with_work_and_expression_component(self): uri = FrbrUri.parse("/za/act/1980/2") uri.work_component = "main" uri.expression_component = "schedule1" uri.expression_subcomponent = "chapter/2" assert_equal("/za/act/1980/2", uri.uri()) assert_equal("/za/act/1980/2/main", uri.work_uri()) assert_equal("/za/act/1980/2/eng/schedule1/chapter/2", uri.expression_uri())
def test_expression_uri(self): uri = FrbrUri.parse("/za/act/1980/02/eng") uri.expression_date = '@2014-01-01' uri.expression_component = 'main' uri.format = 'html' assert_equal("/za/act/1980/02/eng@2014-01-01/main", uri.expression_uri()) uri.expression_subcomponent = "chapter/2" assert_equal("/za/act/1980/02/eng@2014-01-01/main/chapter/2", uri.expression_uri())
def test_with_non_numeric_number(self): uri = FrbrUri.parse("/za/act/1980/nn") assert_equal(uri.country, "za") assert_equal(uri.doctype, "act") assert_equal(uri.subtype, None) assert_equal(uri.actor, None) assert_equal(uri.date, "1980") assert_equal(uri.number, "nn") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal("/za/act/1980/nn", uri.work_uri())
def test_with_long_date(self): uri = FrbrUri.parse("/za/act/1980-02-01/01") assert_equal(uri.country, "za") assert_equal(uri.doctype, "act") assert_equal(uri.subtype, None) assert_equal(uri.actor, None) assert_equal(uri.date, "1980-02-01") assert_equal(uri.number, "01") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal("/za/act/1980-02-01/01", uri.work_uri())
def test_with_subtype_and_actor(self): uri = FrbrUri.parse("/za/act/by-law/actor/1980/01") assert_equal(uri.country, "za") assert_equal(uri.doctype, "act") assert_equal(uri.subtype, "by-law") assert_equal(uri.actor, "actor") assert_equal(uri.date, "1980") assert_equal(uri.number, "01") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal("/za/act/by-law/actor/1980/01", uri.work_uri())
def test_parse_expression_date(self): # A dangling @ indicates the very FIRST expression date, which # we represent with an empty string (''). # A URI without an @ at all, indicates the most recent # expression date, which is None. uri = FrbrUri.parse("/za/act/1980/02/eng") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal(uri.expression_uri(), '/za/act/1980/02/eng') uri = FrbrUri.parse("/za/act/1980/02/eng/main") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) uri = FrbrUri.parse("/za/act/1980/02/eng@") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, '@') assert_equal(uri.expression_uri(), '/za/act/1980/02/eng@') uri = FrbrUri.parse("/za/act/1980/02/eng@/main") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, '@') uri = FrbrUri.parse("/za/act/1980/02/eng:/main") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, ':') uri = FrbrUri.parse("/za/act/1980/02/eng:2012-01-01/main") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, ':2012-01-01')
def test_with_work_component(self): uri = FrbrUri.parse("/za/act/1980/2/schedule1") assert_equal(uri.country, "za") assert_equal(uri.doctype, "act") assert_equal(uri.subtype, None) assert_equal(uri.actor, None) assert_equal(uri.date, "1980") assert_equal(uri.number, "2") assert_equal(uri.language, "eng") assert_equal(uri.work_component, "schedule1") assert_equal(uri.expression_date, None) assert_equal(uri.expression_component, None) assert_equal("/za/act/1980/2", uri.uri()) assert_equal("/za/act/1980/2/schedule1", uri.work_uri()) assert_equal("/za/act/1980/2/eng/schedule1", uri.expression_uri())
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)
def test_parse_expression_component(self): uri = FrbrUri.parse("/za/act/1980/02/eng/main") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal(uri.expression_component, "main") uri = FrbrUri.parse("/za/act/1980/02/eng/main/chapter/2") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal(uri.expression_component, "main") assert_equal(uri.expression_subcomponent, "chapter/2") uri = FrbrUri.parse("/za/act/1980/02/eng@/main/chapter/2") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, '@') assert_equal(uri.expression_component, "main") assert_equal(uri.expression_subcomponent, "chapter/2") uri = FrbrUri.parse("/za/act/1980/02/eng@2014-01-01/main/schedule1") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, "@2014-01-01") assert_equal(uri.expression_component, "main") assert_equal(uri.expression_subcomponent, "schedule1") uri = FrbrUri.parse("/za/act/1980/02/eng@2014-01-01/main/schedule1") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, "@2014-01-01") assert_equal(uri.expression_component, "main") assert_equal(uri.expression_subcomponent, "schedule1") uri = FrbrUri.parse("/za/act/1980/02/eng/main/chapter/2") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal(uri.expression_component, "main") assert_equal(uri.expression_subcomponent, "chapter/2") # this is a weird edge case uri = FrbrUri.parse("/za/act/1980/02/eng/chapter/2") assert_equal(uri.language, "eng") assert_equal(uri.expression_date, None) assert_equal(uri.expression_component, "chapter") assert_equal(uri.expression_subcomponent, "2")
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)