Code Duplication    Length = 31-31 lines in 2 locations

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

@@ 104-134 (lines=31) @@
101
            form = form.parentForm
102
        return form
103
104
    def get_context(self):
105
        """Get the current context
106
107
        NOTE: If we are in the ++add++ form, `self.context` is the container!
108
              Therefore, we create one here to have access to the methods.
109
        """
110
        schema_iface = self.field.interface
111
        if schema_iface and schema_iface.providedBy(self.context):
112
            return self.context
113
114
        # we might be in a subform, so try first to retrieve the object from
115
        # the base form itself first
116
        form = self.get_form()
117
        portal_type = getattr(form, "portal_type", None)
118
        context = getattr(form, "context", None)
119
        if api.is_object(context):
120
            if api.get_portal_type(context) == portal_type:
121
                return context
122
123
        # Hack alert!
124
        # we are in ++add++ form and have no context!
125
        # Create a temporary object to be able to access class methods
126
        if not portal_type:
127
            portal_type = api.get_portal_type(self.context)
128
        portal_types = api.get_tool("portal_types")
129
        fti = portal_types[portal_type]
130
        factory = getUtility(IFactory, fti.factory)
131
        context = factory("temporary")
132
        # hook into acquisition chain
133
        context = context.__of__(self.context)
134
        return context
135
136
    def attr(self, name, default=None):
137
        """Get the named attribute of the widget or the field

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

@@ 95-125 (lines=31) @@
92
            form = form.parentForm
93
        return form
94
95
    def get_context(self):
96
        """Get the current context
97
98
        NOTE: If we are in the ++add++ form, `self.context` is the container!
99
              Therefore, we create one here to have access to the methods.
100
        """
101
        schema_iface = self.field.interface
102
        if schema_iface and schema_iface.providedBy(self.context):
103
            return self.context
104
105
        # we might be in a subform, so try first to retrieve the object from
106
        # the base form itself first
107
        form = self.get_form()
108
        portal_type = getattr(form, "portal_type", None)
109
        context = getattr(form, "context", None)
110
        if api.is_object(context):
111
            if api.get_portal_type(context) == portal_type:
112
                return context
113
114
        # Hack alert!
115
        # we are in ++add++ form and have no context!
116
        # Create a temporary object to be able to access class methods
117
        if not portal_type:
118
            portal_type = api.get_portal_type(self.context)
119
        portal_types = api.get_tool("portal_types")
120
        fti = portal_types[portal_type]
121
        factory = getUtility(IFactory, fti.factory)
122
        context = factory("temporary")
123
        # hook into acquisition chain
124
        context = context.__of__(self.context)
125
        return context
126
127
    def lookup(self, name, field, context, default=None):
128
        """Check if the context has an override for the given named property