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 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