|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import warnings |
| 4 | +from collections.abc import Iterable |
| 5 | + |
| 6 | +from django.conf import settings |
| 7 | +from django.contrib.auth import get_user_model |
| 8 | +from edc_sites.site import sites as site_sites |
| 9 | +from lxml import etree |
| 10 | + |
| 11 | +from ..constants import ( |
| 12 | + LOCATION, |
| 13 | + SITE_LOCATION_TYPE, |
| 14 | + USER, |
| 15 | + USER_TYPE_OTHER, |
| 16 | +) |
| 17 | +from ..utils import oid |
| 18 | + |
| 19 | +# settings map: edc_auth Role.name -> ODM User@UserType; anything not listed |
| 20 | +# (and any user with no listed role) defaults to USER_TYPE_OTHER. Field clinical |
| 21 | +# staff are "Other", not "Investigator". |
| 22 | +USER_TYPE_BY_ROLE_SETTING = "EDC_CDISC_USER_TYPE_BY_ROLE" |
| 23 | +# applied in priority order when a user holds several mapped roles |
| 24 | +USER_TYPE_PRIORITY = ("Sponsor", "Investigator", "Lab") |
| 25 | + |
| 26 | + |
| 27 | +class AdminDataMixin: |
| 28 | + """Builds ODM ``AdminData`` (``User`` + ``Location``) for the users and |
| 29 | + sites referenced by the exported audit records. |
| 30 | +
|
| 31 | + ODM 1.3.1 ``User``/``Location`` are closed types (no ``Alias``/extension), |
| 32 | + so edc_auth roles can only be expressed coarsely via ``User@UserType``. |
| 33 | + """ |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def user_type_for_roles(role_names: Iterable[str]) -> str: |
| 37 | + mapping = getattr(settings, USER_TYPE_BY_ROLE_SETTING, {}) or {} |
| 38 | + mapped = {mapping[name] for name in role_names if name in mapping} |
| 39 | + for user_type in USER_TYPE_PRIORITY: |
| 40 | + if user_type in mapped: |
| 41 | + return user_type |
| 42 | + return USER_TYPE_OTHER |
| 43 | + |
| 44 | + def build_admin_data( |
| 45 | + self, |
| 46 | + usernames: Iterable[str], |
| 47 | + site_ids: Iterable[int], |
| 48 | + metadata_version_oid: str, |
| 49 | + ) -> etree._Element: |
| 50 | + element = etree.Element("AdminData", StudyOID=self.protocol_oid) |
| 51 | + for username in sorted(usernames): |
| 52 | + element.append(self.build_user_element(username)) |
| 53 | + for site_id in sorted(site_ids): |
| 54 | + element.append(self.build_location_element(site_id, metadata_version_oid)) |
| 55 | + return element |
| 56 | + |
| 57 | + def build_user_element(self, username: str) -> etree._Element: |
| 58 | + user_model_cls = get_user_model() |
| 59 | + user = user_model_cls.objects.filter(username=username).first() |
| 60 | + if user is None: |
| 61 | + # still emit a minimal User so the AuditRecord UserRef resolves |
| 62 | + element = etree.Element("User", OID=oid(USER, username)) |
| 63 | + etree.SubElement(element, "LoginName").text = username |
| 64 | + return element |
| 65 | + |
| 66 | + profile = getattr(user, "userprofile", None) |
| 67 | + role_names = {role.name for role in profile.roles.all()} if profile else set() |
| 68 | + institution = (getattr(profile, "institution", "") if profile else "") or ( |
| 69 | + self.protocol_config.institution |
| 70 | + ) |
| 71 | + user_site_ids = sorted(site.id for site in profile.sites.all()) if profile else [] |
| 72 | + |
| 73 | + element = etree.Element( |
| 74 | + "User", |
| 75 | + OID=oid(USER, username), |
| 76 | + UserType=self.user_type_for_roles(role_names), |
| 77 | + ) |
| 78 | + # children in XSD order: LoginName, FirstName, LastName, Organization, |
| 79 | + # Email, LocationRef |
| 80 | + etree.SubElement(element, "LoginName").text = username |
| 81 | + if user.first_name: |
| 82 | + etree.SubElement(element, "FirstName").text = user.first_name |
| 83 | + if user.last_name: |
| 84 | + etree.SubElement(element, "LastName").text = user.last_name |
| 85 | + if institution: |
| 86 | + etree.SubElement(element, "Organization").text = institution |
| 87 | + if user.email: |
| 88 | + etree.SubElement(element, "Email").text = user.email |
| 89 | + for site_id in user_site_ids: |
| 90 | + etree.SubElement(element, "LocationRef", LocationOID=oid(LOCATION, str(site_id))) |
| 91 | + return element |
| 92 | + |
| 93 | + def build_location_element( |
| 94 | + self, site_id: int, metadata_version_oid: str |
| 95 | + ) -> etree._Element: |
| 96 | + try: |
| 97 | + name = site_sites.get(site_id).title |
| 98 | + except Exception: # unknown/unregistered site → fall back to the id |
| 99 | + warnings.warn( |
| 100 | + f"Site id {site_id} not in edc_sites registry; using id as Name.", |
| 101 | + UserWarning, |
| 102 | + stacklevel=2, |
| 103 | + ) |
| 104 | + name = str(site_id) |
| 105 | + element = etree.Element( |
| 106 | + "Location", |
| 107 | + OID=oid(LOCATION, str(site_id)), |
| 108 | + Name=str(name), |
| 109 | + LocationType=SITE_LOCATION_TYPE, |
| 110 | + ) |
| 111 | + etree.SubElement( |
| 112 | + element, |
| 113 | + "MetaDataVersionRef", |
| 114 | + StudyOID=self.protocol_oid, |
| 115 | + MetaDataVersionOID=metadata_version_oid, |
| 116 | + EffectiveDate=self.protocol_config.study_open_datetime.date().isoformat(), |
| 117 | + ) |
| 118 | + return element |
0 commit comments