def get_frbr_uri(self, country, row): frbr_uri = FrbrUri(country=row['country'], locality=row['locality'], doctype='act', subtype=row['subtype'], date=row['year'], number=row['number'], actor=None) # check country matches (and that it's all one country) if country.code != row['country'].lower(): raise ValueError( 'The country in the spreadsheet (%s) doesn\'t match the country selected previously (%s)' % (row['country'], country)) # check all frbr uri fields have been filled in and that no spaces were accidentally included if ' ' in frbr_uri.work_uri(): raise ValueError( 'Check for spaces in country, locality, subtype, year, number – none allowed' ) elif not frbr_uri.country: raise ValueError('A country must be given') elif not frbr_uri.date: raise ValueError('A year must be given') elif not frbr_uri.number: raise ValueError('A number must be given') # check that necessary work fields have been filled in elif not row['title']: raise ValueError('A title must be given') elif not row['publication_date']: raise ValueError('A publication date must be given') return frbr_uri.work_uri().lower()
def create_amendments(apps, schema_editor): Document = apps.get_model("indigo_api", "Document") Work = apps.get_model("indigo_api", "Work") Amendment = apps.get_model("indigo_api", "Amendment") db_alias = schema_editor.connection.alias # ensure works existing for all amending documents documents = Document.objects.filter(deleted=False).using(db_alias).all() works = {w.frbr_uri: w for w in Work.objects.using(db_alias).all()} for doc in documents: for amendment in (doc.amendment_events or []): work = works.get(amendment['amending_uri']) date = parse_date(amendment['date']).date() if not work: # validate frbr_uri try: FrbrUri.parse(amendment['amending_uri']) except ValueError: continue work = Work( title=amendment['amending_title'], frbr_uri=amendment['amending_uri'], publication_date=date, ) work.save() works[work.frbr_uri] = work # hard link the amendment to the work if not doc.work.amendments.filter(amending_work=work, date=date).exists(): a = Amendment(date=date, amending_work=work, amended_work=doc.work) a.save()
def random_frbr_uri(country=None): today = datetime.datetime.now() number = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in xrange(5)) country = country or DEFAULT_COUNTRY return FrbrUri(country=country.lower(), locality=None, doctype="act", subtype=None, actor=None, date=str(today.year), expression_date=today.strftime("%Y-%m-%d"), number=number.lower())
def randomized(cls, frbr_uri, **kwargs): """ Helper to return a new document with a random FRBR URI """ frbr_uri = FrbrUri.parse(frbr_uri) kwargs['work'] = Work.objects.get_for_frbr_uri(frbr_uri.work_uri()) kwargs['language'] = Country.for_frbr_uri(frbr_uri).primary_language doc = cls(frbr_uri=frbr_uri.work_uri(False), expression_date=frbr_uri.expression_date, **kwargs) doc.copy_attributes() return doc
def clean(self): # validate and clean the frbr_uri try: frbr_uri = FrbrUri.parse(self.frbr_uri).work_uri(work_component=False) except ValueError: raise ValidationError("Invalid FRBR URI") # force country and locality codes in frbr uri prefix = '/' + self.country.code if self.locality: prefix = prefix + '-' + self.locality.code self.frbr_uri = ('%s/%s' % (prefix, frbr_uri.split('/', 2)[2])).lower()
def work_uri(self): """ The FRBR Work URI as a :class:`FrbrUri` instance that uniquely identifies this work universally. """ if self._work_uri is None: self._work_uri = FrbrUri.parse(self.frbr_uri) return self._work_uri
def get_frbr_uri(self, country, locality, row): row_country = row.get('country') row_locality = row.get('locality') row_subtype = row.get('subtype') row_year = row.get('year') row_number = row.get('number') frbr_uri = FrbrUri(country=row_country, locality=row_locality, doctype='act', subtype=row_subtype, date=row_year, number=row_number, actor=None) # if the country doesn't match # (but ignore if no country given – dealt with separately) if row_country and country.code != row_country.lower(): raise ValueError( 'The country code given in the spreadsheet ("%s") ' 'doesn\'t match the code for the country you\'re working in ("%s")' % (row.get('country'), country.code.upper())) # if you're working on the country level but the spreadsheet gives a locality if not locality and row_locality: raise ValueError( 'You are working in a country (%s), ' 'but the spreadsheet gives a locality code ("%s")' % (country, row_locality)) # if you're working in a locality but the spreadsheet doesn't give one if locality and not row_locality: raise ValueError( 'There\'s no locality code given in the spreadsheet, ' 'but you\'re working in %s ("%s")' % (locality, locality.code.upper())) # if the locality doesn't match # (only if you're in a locality) if locality and locality.code != row_locality.lower(): raise ValueError( 'The locality code given in the spreadsheet ("%s") ' 'doesn\'t match the code for the locality you\'re working in ("%s")' % (row_locality, locality.code.upper())) # check all frbr uri fields have been filled in and that no spaces were accidentally included if ' ' in frbr_uri.work_uri(): raise ValueError( 'Check for spaces in country, locality, subtype, year, number – none allowed' ) elif not frbr_uri.country: raise ValueError('A country must be given') elif not frbr_uri.date: raise ValueError('A year must be given') elif not frbr_uri.number: raise ValueError('A number must be given') # check that necessary work fields have been filled in elif not row.get('title'): raise ValueError('A title must be given') elif not row.get('publication_date'): raise ValueError('A publication date must be given') return frbr_uri.work_uri().lower()