Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ sudo: false
install: pip install tox
script: tox
env:
- TOXENV=py27-django16-drf31
- TOXENV=py27-django16-drf32
- TOXENV=py32-django16-drf31
- TOXENV=py32-django16-drf32
- TOXENV=py33-django16-drf31
- TOXENV=py33-django16-drf32
- TOXENV=py27-django17-drf31
- TOXENV=py27-django17-drf32
- TOXENV=py32-django17-drf31
Expand Down
Binary file modified drf_example
Binary file not shown.
26 changes: 19 additions & 7 deletions example/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
from __future__ import unicode_literals

import factory
from faker import Factory as FakerFactory
from example.models import Blog, Author, Entry, Comment

from example.models import Blog, Author, Entry

faker = FakerFactory.create()
faker.seed(983843)

class BlogFactory(factory.django.DjangoModelFactory):
class Meta:
model = Blog

name = "Blog 1"
name = factory.LazyAttribute(lambda x: faker.name())


class AuthorFactory(factory.django.DjangoModelFactory):
class Meta:
model = Author

name = "Author 1"
email = "author1@blog1.com"
name = factory.LazyAttribute(lambda x: faker.name())
email = factory.LazyAttribute(lambda x: faker.email())


class EntryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Entry

headline = "Headline 1"
body_text = "Here goes the body text"
headline = factory.LazyAttribute(lambda x: faker.sentence(nb_words=4))
body_text = factory.LazyAttribute(lambda x: faker.text())

blog = factory.SubFactory(BlogFactory)

Expand All @@ -38,3 +40,13 @@ def authors(self, create, extracted, **kwargs):
self.authors.add(author)
else:
self.authors.add(extracted)


class CommentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Comment

entry = factory.SubFactory(EntryFactory)
body = factory.LazyAttribute(lambda x: faker.text())
author = factory.SubFactory(AuthorFactory)

12 changes: 10 additions & 2 deletions example/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework import serializers
from rest_framework_json_api import serializers, relations
from example.models import Blog, Entry, Author, Comment


Expand All @@ -10,10 +10,18 @@ class Meta:


class EntrySerializer(serializers.ModelSerializer):

included_serializers = {
'comments': 'example.serializers.CommentSerializer',
}

comments = relations.ResourceRelatedField(
source='comment_set', many=True, read_only=True)

class Meta:
model = Entry
fields = ('blog', 'headline', 'body_text', 'pub_date', 'mod_date',
'authors',)
'authors', 'comments',)


class AuthorSerializer(serializers.ModelSerializer):
Expand Down
34 changes: 16 additions & 18 deletions example/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import pytest
from pytest_factoryboy import register

from example.factories import BlogFactory, AuthorFactory, EntryFactory
from example.factories import BlogFactory, AuthorFactory, EntryFactory, CommentFactory

register(BlogFactory)
register(AuthorFactory)
register(EntryFactory)
register(CommentFactory)


@pytest.fixture
def single_entry(author_factory, entry_factory):
def single_entry(blog, author, entry_factory, comment_factory):

author = author_factory(name="Joel Spolsky")
entry = entry_factory(
headline=("The Absolute Minimum Every Software Developer"
"Absolutely, Positively Must Know About Unicode "
"and Character Sets (No Excuses!)"),
blog__name='Joel on Software',
authors=(author, )
)
entry = entry_factory(blog=blog, authors=(author,))
comment_factory(entry=entry)
return entry


@pytest.fixture
def multiple_entries(single_entry, author_factory, entry_factory):

author = author_factory(name="Ned Batchelder")
entry = entry_factory(
headline=("Pragmatic Unicode"),
blog__name='Ned Batchelder Blog',
authors=(author, )
)
def multiple_entries(blog_factory, author_factory, entry_factory, comment_factory):

entries = [
entry_factory(blog=blog_factory(), authors=(author_factory(),)),
entry_factory(blog=blog_factory(), authors=(author_factory(),)),
]
comment_factory(entry=entries[0])
comment_factory(entry=entries[1])
return entries

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

import pytest

from ..views import EntryViewSet
from example.views import EntryViewSet
from rest_framework_json_api.pagination import PageNumberPagination

from example.tests.utils import dump_json, redump_json

pytestmark = pytest.mark.django_db


def test_multiple_entries_no_pagination(rf, multiple_entries):
# rf == request_factory
def test_multiple_entries_no_pagination(multiple_entries, rf):

expected = {
"data": [
Expand All @@ -20,8 +21,8 @@ def test_multiple_entries_no_pagination(rf, multiple_entries):
"id": "1",
"attributes":
{
"headline": "The Absolute Minimum Every Software DeveloperAbsolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)",
"bodyText": "Here goes the body text",
"headline": multiple_entries[0].headline,
"bodyText": multiple_entries[0].body_text,
"pubDate": None,
"modDate": None
},
Expand All @@ -33,6 +34,10 @@ def test_multiple_entries_no_pagination(rf, multiple_entries):
"authors": {
"meta": {"count": 1},
"data": [{"type": "authors", "id": "1"}]
},
"comments": {
"meta": {"count": 1},
"data": [{"type": "comments", "id": "1"}]
}
}
},
Expand All @@ -41,8 +46,8 @@ def test_multiple_entries_no_pagination(rf, multiple_entries):
"id": "2",
"attributes":
{
"headline": "Pragmatic Unicode",
"bodyText": "Here goes the body text",
"headline": multiple_entries[1].headline,
"bodyText": multiple_entries[1].body_text,
"pubDate": None,
"modDate": None
},
Expand All @@ -54,6 +59,10 @@ def test_multiple_entries_no_pagination(rf, multiple_entries):
"authors": {
"meta": {"count": 1},
"data": [{"type": "authors", "id": "2"}]
},
"comments": {
"meta": {"count": 1},
"data": [{"type": "comments", "id": "2"}]
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_pagination_with_single_entry(single_entry, client):
"id": "1",
"attributes":
{
"headline": "The Absolute Minimum Every Software DeveloperAbsolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)",
"bodyText": "Here goes the body text",
"headline": single_entry.headline,
"bodyText": single_entry.body_text,
"pubDate": None,
"modDate": None
},
Expand All @@ -28,6 +28,10 @@ def test_pagination_with_single_entry(single_entry, client):
"authors": {
"meta": {"count": 1},
"data": [{"type": "authors", "id": "1"}]
},
"comments": {
"meta": {"count": 1},
"data": [{"type": "comments", "id": "1"}]
}
}
}],
Expand Down
Empty file added example/tests/unit/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ def test_model_instance(blog):
assert isinstance(blog, Blog)


def test_blog_name(blog):
assert blog.name == 'Blog 1'


def test_multiple_blog(blog_factory):
another_blog = blog_factory(name='Cool Blog')
new_blog = blog_factory(name='Awesome Blog')
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion example/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from django.conf.urls import include, url
from rest_framework import routers

from example.views import BlogViewSet, EntryViewSet, AuthorViewSet
from example.views import BlogViewSet, EntryViewSet, AuthorViewSet, CommentViewSet

router = routers.DefaultRouter(trailing_slash=False)

router.register(r'blogs', BlogViewSet)
router.register(r'entries', EntryViewSet)
router.register(r'authors', AuthorViewSet)
router.register(r'comments', CommentViewSet)

urlpatterns = [
url(r'^', include(router.urls)),
Expand Down
8 changes: 7 additions & 1 deletion example/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from rest_framework import viewsets
from rest_framework_json_api.views import RelationshipView
from example.models import Blog, Entry, Author, Comment
from example.serializers import BlogSerializer, EntrySerializer, AuthorSerializer
from example.serializers import (
BlogSerializer, EntrySerializer, AuthorSerializer, CommentSerializer)


class BlogViewSet(viewsets.ModelViewSet):
Expand All @@ -20,6 +21,11 @@ class AuthorViewSet(viewsets.ModelViewSet):
serializer_class = AuthorSerializer


class CommentViewSet(viewsets.ModelViewSet):
queryset = Comment.objects.all()
serializer_class = CommentSerializer


class EntryRelationshipView(RelationshipView):
queryset = Entry.objects.all()

Expand Down
1 change: 1 addition & 0 deletions requirements-development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
pytest==2.8.2
pytest-django
pytest-factoryboy
fake-factory
tox
6 changes: 5 additions & 1 deletion rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ def get_related_resource_type(relation):
parent_model_relation = getattr(parent_model, parent_serializer.field_name)

if hasattr(parent_model_relation, 'related'):
relation_model = parent_model_relation.related.related_model
try:
relation_model = parent_model_relation.related.related_model
except AttributeError:
# Django 1.7
relation_model = parent_model_relation.related.model
elif hasattr(parent_model_relation, 'field'):
relation_model = parent_model_relation.field.related.model
else:
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
[tox]
envlist =
py{27,32,33}-django16-drf{31,32},
py{27,32,33,34}-django17-drf{31,32},
py{27,32,33,34}-django18-drf{31,32},
py{27,34,35}-django19-drf{31,32},

[testenv]
deps =
django16: Django>=1.6,<1.7
django17: Django>=1.7,<1.8
django18: Django>=1.8,<1.9
django19: Django>=1.9a1
Expand Down