コード例 #1
0
    def test_is_valid(self):
        valid = Validation()
        bundle = Bundle()
        self.assertEqual(valid.is_valid(bundle), {})

        bundle = Bundle(data={
            'title': 'Foo.',
            'slug': 'bar',
            'content': '',
            'is_active': True,
        })
        self.assertEqual(valid.is_valid(bundle), {})
コード例 #2
0
    def test_is_valid(self):
        valid = FormValidation(form_class=NoteForm)
        bundle = Bundle()
        self.assertEqual(
            valid.is_valid(bundle), {
                'is_active': [u'This field is required.'],
                'slug': [u'This field is required.'],
                '__all__':
                [u'Having no content makes for a very boring note.'],
                'title': [u'This field is required.'],
            })

        bundle = Bundle(
            data={
                'title': 'Foo.',
                'slug':
                '123456789012345678901234567890123456789012345678901234567890',
                'content': '',
                'is_active': True,
            })
        self.assertEqual(
            valid.is_valid(bundle), {
                'slug':
                [u'Ensure this value has at most 50 characters (it has 60).'],
                '__all__':
                [u'Having no content makes for a very boring note.'],
            })

        bundle = Bundle(data={
            'title': 'Foo.',
            'slug': 'bar',
            'content': '',
            'is_active': True,
        })
        self.assertEqual(valid.is_valid(bundle), {
            '__all__': [u'Having no content makes for a very boring note.'],
        })

        bundle = Bundle(
            data={
                'title': 'Foo.',
                'slug': 'bar',
                'content': 'This! Is! CONTENT!',
                'is_active': True,
            })
        self.assertEqual(valid.is_valid(bundle), {})
        # NOTE: Bundle data is left untouched!
        self.assertEqual(bundle.data['title'], 'Foo.')
コード例 #3
0
ファイル: fields.py プロジェクト: vituocgia/boxme-api
    def dehydrate(self, bundle, for_list=True):
        foreign_obj = None

        if callable(self.attribute):
            previous_obj = bundle.obj
            foreign_obj = self.attribute(bundle)
        elif isinstance(self.attribute, six.string_types):
            foreign_obj = bundle.obj

            for attr in self._attrs:
                previous_obj = foreign_obj
                try:
                    foreign_obj = getattr(foreign_obj, attr, None)
                except ObjectDoesNotExist:
                    foreign_obj = None

        if not foreign_obj:
            if not self.null:
                if callable(self.attribute):
                    raise ApiFieldError(
                        "The related resource for resource %s could not be found."
                        % (previous_obj))
                else:
                    raise ApiFieldError(
                        "The model '%r' has an empty attribute '%s' and doesn't allow a null value."
                        % (previous_obj, attr))
            return None

        fk_resource = self.get_related_resource(foreign_obj)
        fk_bundle = Bundle(obj=foreign_obj, request=bundle.request)
        return self.dehydrate_related(fk_bundle,
                                      fk_resource,
                                      for_list=for_list)
コード例 #4
0
    def get_via_uri(self, uri, request=None):
        """
        This pulls apart the salient bits of the URI and populates the
        resource via a ``obj_get``.

        Optionally accepts a ``request``.

        If you need custom behavior based on other portions of the URI,
        simply override this method.
        """
        prefix = get_script_prefix()
        chomped_uri = uri

        if prefix and chomped_uri.startswith(prefix):
            chomped_uri = chomped_uri[len(prefix) - 1:]

        try:
            view, args, kwargs = resolve(chomped_uri)
            resource_name = kwargs['resource_name']
            resource_class = self.resource_mapping[resource_name]
        except (Resolver404, KeyError):
            raise NotFound(
                "The URL provided '%s' was not a link to a valid resource." %
                uri)

        parent_resource = resource_class(api_name=self._meta.api_name)
        kwargs = parent_resource.remove_api_resource_names(kwargs)
        bundle = Bundle(request=request)
        return parent_resource.obj_get(bundle, **kwargs)
コード例 #5
0
ファイル: serializers.py プロジェクト: vituocgia/boxme-api
 def setUp(self):
     super(ResourceSerializationTestCase, self).setUp()
     self.resource = NoteResource()
     base_bundle = Bundle()
     self.obj_list = [
         self.resource.full_dehydrate(self.resource.build_bundle(obj=obj))
         for obj in self.resource.obj_get_list(base_bundle)
     ]
     self.another_resource = AnotherNoteResource()
     self.another_obj_list = [
         self.another_resource.full_dehydrate(
             self.resource.build_bundle(obj=obj))
         for obj in self.another_resource.obj_get_list(base_bundle)
     ]
コード例 #6
0
ファイル: fields.py プロジェクト: vituocgia/boxme-api
    def dehydrate(self, bundle, for_list=True):
        if not bundle.obj or not bundle.obj.pk:
            if not self.null:
                raise ApiFieldError(
                    "The model '%r' does not have a primary key and can not be used in a ToMany context."
                    % bundle.obj)

            return []

        the_m2ms = None
        previous_obj = bundle.obj
        attr = self.attribute

        if callable(self.attribute):
            the_m2ms = self.attribute(bundle)
        elif isinstance(self.attribute, six.string_types):
            the_m2ms = bundle.obj

            for attr in self._attrs:
                previous_obj = the_m2ms
                try:
                    the_m2ms = getattr(the_m2ms, attr, None)
                except ObjectDoesNotExist:
                    the_m2ms = None

                if the_m2ms is None:
                    break

        if the_m2ms is None:
            if not self.null:
                raise ApiFieldError(
                    "The model '%r' has an empty attribute '%s' and doesn't allow a null value."
                    % (previous_obj, attr))

        if isinstance(the_m2ms, models.Manager):
            the_m2ms = the_m2ms.all()

        m2m_dehydrated = [
            self.dehydrate_related(Bundle(obj=m2m, request=bundle.request),
                                   self.get_related_resource(m2m),
                                   for_list=for_list) for m2m in the_m2ms
        ]

        return m2m_dehydrated
コード例 #7
0
ファイル: serializers.py プロジェクト: vituocgia/boxme-api
 def test__to_simple__bundle(self):
     serializer = Serializer()
     val = Bundle(data={'foo': True})
     self.assertEqual(serializer.to_simple(val, None), {'foo': True})