Coverage for src / mesh / ojs / import_xml.py: 0%
40 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-05-04 12:41 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-05-04 12:41 +0000
1import logging
2import traceback
4from alive_progress import alive_bar
5from django.db import transaction
6from lxml import etree
8from mesh.models import JournalSection
9from mesh.ojs import NSMAP, OJS_NAMESPACE
10from mesh.ojs.import_submission import import_ojs_article
11from mesh.ojs.import_user import import_ojs_user
13logger = logging.getLogger(__name__)
16@transaction.atomic
17def import_ojs_xml(path: str):
18 tree = etree.parse(path)
19 root = tree.getroot()
21 journal_name = root.attrib["url_path"]
22 if not root.tag == f"{{{OJS_NAMESPACE}}}journal":
23 raise ValueError('The root tag must be a journal with xmlns="http://pkp.sfu.ca"')
25 # Journal Section
26 journal, created = JournalSection.objects.get_or_create(name=journal_name)
27 if created:
28 journal.save()
29 logger.debug(f"Created journal {journal_name}")
30 else:
31 logger.debug(f"Created journal {journal_name}")
33 # Users
34 pkp_users = root.findall("PKPUsers/users/user", NSMAP)
35 with alive_bar(len(pkp_users), title="Loading users", enrich_print=False) as bar:
36 for ojs_user in pkp_users:
37 import_ojs_user(ojs_user)
38 bar()
40 # Dangling submissions
41 pkp_extended_articles = root.findall("extended_articles/extended_article", NSMAP)
42 pkp_extended_articles.extend(
43 root.findall("extended_issues/extended_issue/extended_articles/extended_article", NSMAP)
44 )
45 with alive_bar(
46 len(pkp_extended_articles), title="Loading submissions", enrich_print=False
47 ) as bar:
48 for ojs_article in pkp_extended_articles:
49 try:
50 with transaction.atomic():
51 import_ojs_article(ojs_article, journal, path)
52 except ValueError as e:
53 logger.warning("Submission import skipped: %s", e)
54 except BaseException:
55 logger.error(traceback.format_exc())
56 continue
57 finally:
58 bar()