[entities] Harden `TrInfo` for empty/null relations

Returning None is clearer than having a raised KeyError.

authorNicola Spanti <nicola.spanti@logilab.fr>
changeset10f2772dfc88
branch3.26
phasedraft
hiddenno
parent revision#f783f7d1d455 Added tag 3.26.19, debian/3.26.19-1 for changeset d5fb8ecdd5c0
child revision<not specified>
files modified by this revision
cubicweb/entities/wfobjs.py
# HG changeset patch
# User Nicola Spanti <nicola.spanti@logilab.fr>
# Date 1607078375 -3600
# Fri Dec 04 11:39:35 2020 +0100
# Branch 3.26
# Node ID 10f2772dfc88b6ed78a1ffebe779e761c0e69b39
# Parent f783f7d1d45598bc7f606b14ef313e552f5b3f07
[entities] Harden `TrInfo` for empty/null relations

Returning `None` is clearer than having a raised `KeyError`.

diff --git a/cubicweb/entities/wfobjs.py b/cubicweb/entities/wfobjs.py
@@ -387,19 +387,19 @@
1      __regid__ = 'TrInfo'
2      fetch_attrs, cw_fetch_order = fetch_config(['creation_date', 'comment'],
3                                                 pclass=None) # don't want modification_date
4      @property
5      def for_entity(self):
6 -        return self.wf_info_for[0]
7 +        return self.wf_info_for and self.wf_info_for[0] or None
8 
9      @property
10      def previous_state(self):
11 -        return self.from_state[0]
12 +        return self.from_state and self.from_state[0] or None
13 
14      @property
15      def new_state(self):
16 -        return self.to_state[0]
17 +        return self.to_state and self.to_state[0] or None
18 
19      @property
20      def transition(self):
21          return self.by_transition and self.by_transition[0] or None
22