1212
1313from opensemantic .base .v1 import Organization , User
1414
15+ from .config import PROTECTED_FIELDS
1516from .mapping import ProposedOrganization , ProposedUser
1617
1718# The concrete DisplayName type used by the label field.
@@ -22,12 +23,20 @@ def _labels(text: str) -> List[Any]:
2223 return [_DISPLAY_NAME (text = text )]
2324
2425
26+ def _strip_protected (entity : Any ) -> None :
27+ """Remove protected relation defaults (e.g. employment_contract_status)."""
28+ iris = getattr (entity , "__iris__" , None )
29+ if isinstance (iris , dict ):
30+ for name in PROTECTED_FIELDS :
31+ iris .pop (name , None )
32+
33+
2534def build_organization (proposed : ProposedOrganization ) -> Organization :
2635 return Organization (uuid = proposed .uuid , label = _labels (proposed .name ))
2736
2837
2938def build_user (proposed : ProposedUser ) -> User :
30- """Build a full User entity for creation."""
39+ """Build a full User entity for creation (never carries protected fields) ."""
3140 args : dict = {
3241 "uuid" : proposed .uuid ,
3342 "username" : proposed .username ,
@@ -43,11 +52,17 @@ def build_user(proposed: ProposedUser) -> User:
4352 args ["website" ] = set (proposed .websites )
4453 if proposed .organizations :
4554 args ["organization" ] = list (proposed .organizations )
46- return User (** args )
55+ entity = User (** args )
56+ _strip_protected (entity )
57+ return entity
4758
4859
4960def apply_update (entity : Any , proposed : ProposedUser , apply_fields : Set [str ]) -> Any :
50- """Apply the accepted fields onto a loaded existing entity, in place."""
61+ """Apply the accepted fields onto a loaded existing entity, in place.
62+
63+ Fields whose proposal is empty (REMOVE) are cleared; protected fields in
64+ ``apply_fields`` are stripped from the entity's ``__iris__`` map.
65+ """
5166 if "label" in apply_fields :
5267 entity .label = _labels (proposed .label )
5368 if "first_name" in apply_fields :
@@ -61,5 +76,10 @@ def apply_update(entity: Any, proposed: ProposedUser, apply_fields: Set[str]) ->
6176 if "websites" in apply_fields :
6277 entity .website = set (proposed .websites )
6378 if "organizations" in apply_fields :
64- entity .__iris__ ["organization" ] = list (proposed .organizations )
79+ if proposed .organizations :
80+ entity .__iris__ ["organization" ] = list (proposed .organizations )
81+ else :
82+ entity .__iris__ .pop ("organization" , None )
83+ if any (name in apply_fields for name in PROTECTED_FIELDS ):
84+ _strip_protected (entity )
6585 return entity
0 commit comments