Code Duplication    Length = 31-31 lines in 2 locations

src/senaite/core/z3cform/widgets/uidreference/widget.py 1 location

@@ 86-116 (lines=31) @@
83
            form = form.parentForm
84
        return form
85
86
    def get_context(self):
87
        """Get the current context
88
89
        NOTE: If we are in the ++add++ form, `self.context` is the container!
90
              Therefore, we create one here to have access to the methods.
91
        """
92
        schema_iface = self.field.interface
93
        if schema_iface and schema_iface.providedBy(self.context):
94
            return self.context
95
96
        # we might be in a subform, so try first to retrieve the object from
97
        # the base form itself first
98
        form = self.get_form()
99
        portal_type = getattr(form, "portal_type", None)
100
        context = getattr(form, "context", None)
101
        if api.is_object(context):
102
            if api.get_portal_type(context) == portal_type:
103
                return context
104
105
        # Hack alert!
106
        # we are in ++add++ form and have no context!
107
        # Create a temporary object to be able to access class methods
108
        if not portal_type:
109
            portal_type = api.get_portal_type(self.context)
110
        portal_types = api.get_tool("portal_types")
111
        fti = portal_types[portal_type]
112
        factory = getUtility(IFactory, fti.factory)
113
        context = factory("temporary")
114
        # hook into acquisition chain
115
        context = context.__of__(self.context)
116
        return context
117
118
    def attr(self, name, default=None):
119
        """Get the named attribute of the widget or the field

src/senaite/core/z3cform/widgets/queryselect/widget.py 1 location

@@ 77-107 (lines=31) @@
74
            form = form.parentForm
75
        return form
76
77
    def get_context(self):
78
        """Get the current context
79
80
        NOTE: If we are in the ++add++ form, `self.context` is the container!
81
              Therefore, we create one here to have access to the methods.
82
        """
83
        schema_iface = self.field.interface
84
        if schema_iface and schema_iface.providedBy(self.context):
85
            return self.context
86
87
        # we might be in a subform, so try first to retrieve the object from
88
        # the base form itself first
89
        form = self.get_form()
90
        portal_type = getattr(form, "portal_type", None)
91
        context = getattr(form, "context", None)
92
        if api.is_object(context):
93
            if api.get_portal_type(context) == portal_type:
94
                return context
95
96
        # Hack alert!
97
        # we are in ++add++ form and have no context!
98
        # Create a temporary object to be able to access class methods
99
        if not portal_type:
100
            portal_type = api.get_portal_type(self.context)
101
        portal_types = api.get_tool("portal_types")
102
        fti = portal_types[portal_type]
103
        factory = getUtility(IFactory, fti.factory)
104
        context = factory("temporary")
105
        # hook into acquisition chain
106
        context = context.__of__(self.context)
107
        return context
108
109
    def attr(self, name, default=None):
110
        """Get the named attribute of the widget or the field