Code Duplication    Length = 31-33 lines in 2 locations

bika/lims/content/samplepoint.py 1 location

@@ 149-181 (lines=33) @@
146
    def Title(self):
147
        return safe_unicode(self.getField('title').get(self)).encode('utf-8')
148
149
    def setSampleTypes(self, value, **kw):
150
        """ For the moment, we're manually trimming the sampletype<>samplepoint
151
            relation to be equal on both sides, here.
152
            It's done strangely, because it may be required to behave strangely.
153
        """
154
        bsc = getToolByName(self, 'bika_setup_catalog')
155
        # convert value to objects
156
        if value and type(value) == str:
157
            value = [bsc(UID=value)[0].getObject(), ]
158
        elif value and type(value) in (list, tuple) and type(value[0]) == str:
159
            value = [bsc(UID=uid)[0].getObject() for uid in value if uid]
160
        if not type(value) in (list, tuple):
161
            value = [value, ]
162
        # Find all SampleTypes that were removed
163
        existing = self.Schema()['SampleTypes'].get(self)
164
        removed = existing and [s for s in existing if s not in value] or []
165
        added = value and [s for s in value if s not in existing] or []
166
        ret = self.Schema()['SampleTypes'].set(self, value)
167
168
        # finally be sure that we aren't trying to set None values here.
169
        removed = [x for x in removed if x]
170
        added = [x for x in added if x]
171
172
        for st in removed:
173
            samplepoints = st.getSamplePoints()
174
            if self in samplepoints:
175
                samplepoints.remove(self)
176
                st.setSamplePoints(samplepoints)
177
178
        for st in added:
179
            st.setSamplePoints(list(st.getSamplePoints()) + [self, ])
180
181
        return ret
182
183
184
registerType(SamplePoint, PROJECTNAME)

bika/lims/content/sampletype.py 1 location

@@ 285-315 (lines=31) @@
282
        settings = getToolByName(self, 'bika_setup')
283
        return settings.getDefaultSampleLifetime()
284
285
    def setSamplePoints(self, value, **kw):
286
        """ For the moment, we're manually trimming the sampletype<>samplepoint
287
            relation to be equal on both sides, here.
288
            It's done strangely, because it may be required to behave strangely.
289
        """
290
        bsc = getToolByName(self, 'bika_setup_catalog')
291
        ## convert value to objects
292
        if value and type(value) == str:
293
            value = [bsc(UID=value)[0].getObject(),]
294
        elif value and type(value) in (list, tuple) and type(value[0]) == str:
295
            value = [bsc(UID=uid)[0].getObject() for uid in value if uid]
296
        ## Find all SamplePoints that were removed
297
        existing = self.Schema()['SamplePoints'].get(self)
298
        removed = existing and [s for s in existing if s not in value] or []
299
        added = value and [s for s in value if s not in existing] or []
300
        ret = self.Schema()['SamplePoints'].set(self, value)
301
302
        # finally be sure that we aren't trying to set None values here.
303
        removed = [x for x in removed if x]
304
        added = [x for x in added if x]
305
306
        for sp in removed:
307
            sampletypes = sp.getSampleTypes()
308
            if self in sampletypes:
309
                sampletypes.remove(self)
310
                sp.setSampleTypes(sampletypes)
311
312
        for sp in added:
313
            sp.setSampleTypes(list(sp.getSampleTypes()) + [self,])
314
315
        return ret
316
317
    def SampleMatricesVocabulary(self):
318
        from bika.lims.content.samplematrix import SampleMatrices