def set(self, instance, value, **kw):  # noqa
        """Set the value of the refernce field
        """
        ref = []

        # The value is an UID
        if api.is_uid(value):
            ref.append(api.get_object_by_uid(value))

        # The value is already an object
        if api.is_at_content(value):
            ref.append(value)

        # The value is a dictionary
        # -> handle it like a catalog query
        if u.is_dict(value):
            results = api.search(portal_type=self.allowed_types, **value)
            ref = map(api.get_object, results)

        # The value is a list
        if u.is_list(value):
            for item in value:
                # uid
                if api.is_uid(item):
                    ref.append(api.get_object_by_uid(item))
                    continue

                # object
                if api.is_at_content(item):
                    ref.append(api.get_object(item))
                    continue

                # path
                if api.is_path(item):
                    ref.append(api.get_object_by_path(item))
                    continue

                # dict (catalog query)
                if u.is_dict(item):
                    results = api.search(portal_type=self.allowed_types, **item)
                    objs = map(api.get_object, results)
                    ref.extend(objs)
                    continue

                # Plain string
                # -> do a catalog query for title
                if isinstance(item, basestring):
                    results = api.search(portal_type=self.allowed_types, title=item)
                    objs = map(api.get_object, results)
                    ref.extend(objs)
                    continue

        # The value is a physical path
        if api.is_path(value):
            ref = api.get_object_by_path(value)

        # Handle non multi valued fields
        if not self.multi_valued and len(ref) > 1:
            raise ValueError("Multiple values given for single valued field {}"
                             .format(self.field))

        return self._set(instance, ref, **kw)
Ejemplo n.º 2
0
 def test_get_object(self):
     obj = self.get_document_obj()
     brain = self.get_document_brain()
     self.assertEqual(api.get_object(obj), obj)
     self.assertEqual(api.get_object(brain), brain.getObject())
Ejemplo n.º 3
0
 def test_is_brain(self):
     brain = self.get_document_brain()
     self.assertTrue(api.is_brain(brain))
     self.assertFalse(api.is_brain(api.get_object(brain)))
Ejemplo n.º 4
0
 def test_get_object(self):
     obj = self.get_document_obj()
     brain = self.get_document_brain()
     self.assertEqual(api.get_object(obj), obj)
     self.assertEqual(api.get_object(brain), brain.getObject())
Ejemplo n.º 5
0
 def test_is_brain(self):
     brain = self.get_document_brain()
     self.assertTrue(api.is_brain(brain))
     self.assertFalse(api.is_brain(api.get_object(brain)))
Ejemplo n.º 6
0
    def set(self, instance, value, **kw):  # noqa
        """Set the value of the refernce field
        """
        ref = []

        # The value is an UID
        if api.is_uid(value):
            ref.append(api.get_object_by_uid(value))

        # The value is already an object
        if api.is_at_content(value):
            ref.append(value)

        # The value is a dictionary
        # -> handle it like a catalog query
        if u.is_dict(value):
            results = api.search(portal_type=self.allowed_types, **value)
            ref = map(api.get_object, results)

        # The value is a list
        if u.is_list(value):
            for item in value:
                # uid
                if api.is_uid(item):
                    ref.append(api.get_object_by_uid(item))
                    continue

                # object
                if api.is_at_content(item):
                    ref.append(api.get_object(item))
                    continue

                # path
                if api.is_path(item):
                    ref.append(api.get_object_by_path(item))
                    continue

                # dict (catalog query)
                if u.is_dict(item):
                    results = api.search(portal_type=self.allowed_types,
                                         **item)
                    objs = map(api.get_object, results)
                    ref.extend(objs)
                    continue

                # Plain string
                # -> do a catalog query for title
                if isinstance(item, basestring):
                    results = api.search(portal_type=self.allowed_types,
                                         title=item)
                    objs = map(api.get_object, results)
                    ref.extend(objs)
                    continue

        # The value is a physical path
        if api.is_path(value):
            ref = api.get_object_by_path(value)

        # Handle non multi valued fields
        if not self.multi_valued and len(ref) > 1:
            raise ValueError(
                "Multiple values given for single valued field {}".format(
                    self.field))

        return self._set(instance, ref, **kw)