def overwrite_content(self, work): """ Updates only TEI, METS, the HTML for sections contained in an ingest package. """ assert work.pk is not None warnings, errors = works_utils.validate_ingest_package(self.context.archive) self.work = work sections = self.get_sections() self._commit_sections(sections) self._write_meta() return {"work": work, "sections": sections, "warnings": warnings, "errors": errors}
def execute(self, work=None, commit=False): """ Executes the action. Keyword parameters: `work` : the work to be modified. If one is not supplied, a new one will be created. `commit` : whether to save anything to the database and filesystem. """ archive = self.context.archive meta = self.context.mets.metadata tei = self.context.tei warnings, errors = works_utils.validate_ingest_package(self.context.archive) if work is None: work = models.Work() work.license = getattr(self.context, "license", models.License.objects.all()[0]) work.genre = self.find_genre(meta.genre_name) work.title = meta.title work.slug = unique_slug(models.Work, "slug", meta.title) work.subtitle = meta.subtitle and meta.subtitle or "" authors = self.get_authors(meta) editors = [x for x in authors if x.editor] author_names = [x.display_name for x in authors] auth_display = works_utils.get_text_list(author_names, "and") if len(editors) > 0: auth_display += ", Editor" if len(editors) > 1: auth_display += "s" work.author_display = auth_display try: work.description = tei.get_abstract_html() except Exception: work.description = meta.description work.page_count = self.context.tei.page_count() self.work = work publisher = self.get_publisher(meta) if meta.isbn is not None: work.isbn = meta.isbn if not work.isbn.isdigit(): dre = re.compile(r"^\s*(\d+)") m = dre.search(work.isbn) if m: work.isbn = m.group(1) else: work.isbn = None if hasattr(meta, "doi") and meta.doi is not None: work.doi = meta.doi if "publishedYear" in meta: pubYear = int(meta.publishedYear) work.published = datetime(pubYear, 1, 1) subjects = self.get_subjects(meta) sections = self.get_sections() work.license = models.License.objects.all()[0] self.media = [Image(archive, m["href"], m["mimetype"]) for m in self.context.mets.media] links = self.get_links() keywords = self.get_keywords() if commit: for s in subjects: if s.pk is None: s.save() if publisher.pk is None: publisher.save() work.publisher = publisher kwstring = ",".join(keywords) work.tags = kwstring work.save() for s in subjects: work.subjects.add(s) for a in authors: if a.pk is None: a.save() authoring = models.WorkAuthoring(work=work, author=a) authoring.save() work.save() for link in links: link.content_object = work link.save() self._commit_sections(sections) self._write_media() self._write_meta() archive_path = archive.file_path shutil.copyfile(archive.file_path, os.path.join(work.get_content_directory(), "ingest.zip")) ctx = { "work": work, "sections": sections, "authors": authors, "media": self.media, "subjects": subjects, "keywords": keywords, "publisher": publisher, "links": links, "saved": commit, "warnings": warnings, "errors": "errors", } return ctx