Exemple #1
0
    def test_nested(self):
        attrs = {
            'fields': (':local', ('book_set', ':local')),
            'exclude': (':pk', 'libraries', 'tags')
        }

        HackerResource, created = get_or_create_resource(
            models.Hacker, **attrs)

        self.assertEqual(
            HackerResource.resolve_fields(self.jresig), {
                'name':
                'John Resig',
                'website':
                'http://ejohn.org',
                'book_set': [{
                    'id': 1,
                    'title': 'Secrets of a JavaScript Ninja',
                    'author': {
                        'id': 1
                    },
                    'pub_date': date(2011, 6, 1),
                    'tags': [{
                        'id': 1
                    }],
                }]
            })
Exemple #2
0
    def test_pk(self):
        attrs = {'fields': (':local',), 'exclude': (':pk',)}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(TagResource.resolve_fields(self.tag1), {
            'name': 'javascript',
        })
Exemple #3
0
    def test_pk(self):
        attrs = {'fields': (':pk',)}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(TagResource.resolve_fields(self.tag1), {
            'id': 1,
        })
Exemple #4
0
    def test_pk(self):
        attrs = {'fields': (':pk', )}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(TagResource.resolve_fields(self.tag1), {
            'id': 1,
        })
Exemple #5
0
    def test_pk(self):
        attrs = {'fields': (':local', ), 'exclude': (':pk', )}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(TagResource.resolve_fields(self.tag1), {
            'name': 'javascript',
        })
Exemple #6
0
    def test_related(self):
        attrs = {'fields': (':local', ':related'), 'exclude': (':local',)}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(TagResource.resolve_fields(self.tag1), {
            'book_set': [{'id': 1}],
            'hacker_set': [{'id': 1}],
            'library_set': [{'id': 1}],
        })
Exemple #7
0
    def test_local(self):

        attrs = {'fields': (':local', ':related'), 'exclude': (':related',)}

        BookResource, created = get_or_create_resource(models.Book, **attrs)

        self.assertEqual(BookResource.resolve_fields(self.book), {
            'id': 1,
            'title': 'Secrets of a JavaScript Ninja',
            'author': {'id': 1},
            'pub_date': date(2011, 6, 1),
            'tags': [{'id': 1}],
        })
Exemple #8
0
def get_resource_for_model(model, fields=None):
    """Gets the default resource for this model. This should only be used when
    dealing with related objects. This is invoked when a resource associated
    with this model does not exist.
    """
    from restlib.resources.model import get_or_create_resource

    # these two lines may seem confusing, but if fields are already defined,
    # this will force the creation of a new resource since it explicitly
    # defines fields. the second line is to fallback to just the ``pk``
    # field of the ``model``
    force = True if fields else False
    fields = fields or (':pk',)

    return get_or_create_resource(model, force=force,
        fields=fields)
Exemple #9
0
    def test_related(self):
        attrs = {'fields': (':related', )}

        TagResource, created = get_or_create_resource(models.Tag, **attrs)

        self.assertEqual(
            TagResource.resolve_fields(self.tag1), {
                'book_set': [{
                    'id': 1
                }],
                'hacker_set': [{
                    'id': 1
                }],
                'library_set': [{
                    'id': 1
                }],
            })
Exemple #10
0
    def test_local(self):

        attrs = {'fields': (':local', ':related'), 'exclude': (':related', )}

        BookResource, created = get_or_create_resource(models.Book, **attrs)

        self.assertEqual(
            BookResource.resolve_fields(self.book), {
                'id': 1,
                'title': 'Secrets of a JavaScript Ninja',
                'author': {
                    'id': 1
                },
                'pub_date': date(2011, 6, 1),
                'tags': [{
                    'id': 1
                }],
            })
Exemple #11
0
    def test_nested(self):
        attrs = {
            'fields': (':local', ('book_set', ':local')),
            'exclude': (':pk', 'libraries', 'tags')
        }

        HackerResource, created = get_or_create_resource(models.Hacker, **attrs)

        self.assertEqual(HackerResource.resolve_fields(self.jresig), {
            'name': 'John Resig',
            'website': 'http://ejohn.org',
            'book_set': [{
                'id': 1,
                'title': 'Secrets of a JavaScript Ninja',
                'author': {'id': 1},
                'pub_date': date(2011, 6, 1),
                'tags': [{'id': 1}],
            }]
        })