Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dd4d4ec
Support polymorphic models (from django-polymorphic and django-typed-…
leo-naeka Feb 21, 2016
8c73d95
Polymorphic ancestors must now be defined in Django's settings
leo-naeka Mar 14, 2016
681b5aa
Adds the following features:
ograycode May 13, 2016
96cbab0
Fix example migration and tests
leo-naeka May 15, 2016
5c63425
Polymorphic serializers refactor
leo-naeka May 16, 2016
fddb06b
Basic support of write operations on polymorphic relations
leo-naeka May 16, 2016
22829d1
Improve polymorphism documentation
leo-naeka May 17, 2016
d565334
Improve polymorphic relations and tests.
leo-naeka May 17, 2016
e840438
Add django-polymorphic as test dependency
leo-naeka Sep 8, 2016
19b0238
Avoid type list comparison in polymorphic tests
leo-naeka Sep 8, 2016
0ddf5ca
Merge remote-tracking branch 'origin/develop' into polymorphism
AstraLuma May 17, 2017
b8bf612
Flake8
AstraLuma May 17, 2017
8fd4617
Flake8
AstraLuma May 17, 2017
275793c
Better handle imports?
AstraLuma May 17, 2017
a26df13
Resolve circular reference
AstraLuma May 17, 2017
2278976
Really break up import loop
AstraLuma May 17, 2017
8563b65
Missed something in the merge
AstraLuma May 17, 2017
4aaeac2
Redo migrations
AstraLuma May 17, 2017
030f6c8
Wrong indentation
AstraLuma May 17, 2017
ca23885
Fix a deprecation
AstraLuma May 24, 2017
ae759e5
Fix polymorphic type resolution in relations
leo-naeka May 25, 2017
37c5ae6
Fix tests among different environments
leo-naeka May 25, 2017
f36821b
Update tox.ini environment list
leo-naeka May 25, 2017
4eec4aa
Add packaging module as requirement for old python versions
leo-naeka May 25, 2017
bc12e0f
Remove the POLYMORPHIC_ANCESTOR code
leo-naeka May 26, 2017
6b4f45b
Fix some typos and little errors
leo-naeka May 29, 2017
36f3b6a
Administrivia
AstraLuma May 30, 2017
05cdb51
Restore generic relation support
AstraLuma May 30, 2017
c1afe35
Add Leo to authors
AstraLuma May 30, 2017
8ff5465
PEP8
AstraLuma May 30, 2017
35c90d4
Merge branch 'develop' into polymorphism
AstraLuma May 30, 2017
c5599c0
Really bad writing.
AstraLuma May 31, 2017
8d94efb
Merge branch 'polymorphism' of github.com:leo-naeka/django-rest-frame…
AstraLuma May 31, 2017
89ad607
Editing
AstraLuma Jun 1, 2017
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/develop' into polymorphism
Conflicts:
	.travis.yml
	example/factories/__init__.py
	example/models.py
	example/serializers.py
	example/tests/conftest.py
	example/urls_test.py
	example/views.py
	requirements-development.txt
	rest_framework_json_api/parsers.py
	rest_framework_json_api/relations.py
	rest_framework_json_api/renderers.py
	rest_framework_json_api/serializers.py
	rest_framework_json_api/utils.py
	setup.py
  • Loading branch information
AstraLuma committed May 17, 2017
commit 0ddf5ca15867d017cce46bd1fa65990820b9b2de
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ before_install:
# Force an upgrade of py & pytest to avoid VersionConflict
- pip install --upgrade py
- pip install "pytest>=2.8,<3"
- pip install codecov
- pip install codecov flake8
install:
- pip install Django${DJANGO} djangorestframework${DRF}
- python setup.py install
Expand Down
28 changes: 18 additions & 10 deletions example/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

import factory
from faker import Factory as FakerFactory
from example import models

from example.models import Blog, Author, AuthorBio, Entry, Comment, TaggedItem, ArtProject, ResearchProject, Company

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


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

name = factory.LazyAttribute(lambda x: faker.name())


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

name = factory.LazyAttribute(lambda x: faker.name())
email = factory.LazyAttribute(lambda x: faker.email())
Expand All @@ -28,15 +27,15 @@ class Meta:

class AuthorBioFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.AuthorBio
model = AuthorBio

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


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

headline = factory.LazyAttribute(lambda x: faker.sentence(nb_words=4))
body_text = factory.LazyAttribute(lambda x: faker.text())
Expand All @@ -55,32 +54,41 @@ def authors(self, create, extracted, **kwargs):

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

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


class TaggedItemFactory(factory.django.DjangoModelFactory):

class Meta:
model = TaggedItem

content_object = factory.SubFactory(EntryFactory)
tag = factory.LazyAttribute(lambda x: faker.word())


class ArtProjectFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.ArtProject
model = ArtProject

topic = factory.LazyAttribute(lambda x: faker.catch_phrase())
artist = factory.LazyAttribute(lambda x: faker.name())


class ResearchProjectFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.ResearchProject
model = ResearchProject

topic = factory.LazyAttribute(lambda x: faker.catch_phrase())
supervisor = factory.LazyAttribute(lambda x: faker.name())


class CompanyFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Company
model = Company

name = factory.LazyAttribute(lambda x: faker.company())
current_project = factory.SubFactory(ArtProjectFactory)
Expand Down
59 changes: 36 additions & 23 deletions example/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from datetime import datetime
from rest_framework_json_api import serializers, relations
from example import models
from example.models import Blog, Entry, Author, AuthorBio, Comment, TaggedItemm, Project, ArtProject, ResearchProject, Company


class TaggedItemSerializer(serializers.ModelSerializer):

class Meta:
model = TaggedItem
fields = ('tag', )


class BlogSerializer(serializers.ModelSerializer):
Expand All @@ -21,20 +28,21 @@ def get_root_meta(self, resource, many):
}

class Meta:
model = models.Blog
fields = ('name', 'url',)
model = Blog
fields = ('name', 'url', 'tags')
read_only_fields = ('tags', )
meta_fields = ('copyright',)


class EntrySerializer(serializers.ModelSerializer):

def __init__(self, *args, **kwargs):
super(EntrySerializer, self).__init__(*args, **kwargs)
# to make testing more concise we'll only output the
# `featured` field when it's requested via `include`
request = kwargs.get('context', {}).get('request')
if request and 'featured' not in request.query_params.get('include', []):
self.fields.pop('featured', None)
self.fields.pop('featured')
super(EntrySerializer, self).__init__(*args, **kwargs)

included_serializers = {
'authors': 'example.serializers.AuthorSerializer',
Expand All @@ -45,27 +53,32 @@ def __init__(self, *args, **kwargs):
}

body_format = serializers.SerializerMethodField()
# Many related from model
# many related from model
comments = relations.ResourceRelatedField(
source='comment_set', many=True, read_only=True)
# Many related from serializer
many=True, read_only=True)
# many related from serializer
suggested = relations.SerializerMethodResourceRelatedField(
source='get_suggested', model=models.Entry, many=True, read_only=True)
# Single related from serializer
source='get_suggested', model=Entry, many=True, read_only=True,
related_link_view_name='entry-suggested',
related_link_url_kwarg='entry_pk',
self_link_view_name='entry-relationships',
)
# single related from serializer
featured = relations.SerializerMethodResourceRelatedField(
source='get_featured', model=models.Entry, read_only=True)
source='get_featured', model=Entry, read_only=True)
tags = TaggedItemSerializer(many=True, read_only=True)

def get_suggested(self, obj):
return models.Entry.objects.exclude(pk=obj.pk)
return Entry.objects.exclude(pk=obj.pk)

def get_featured(self, obj):
return models.Entry.objects.exclude(pk=obj.pk).first()
return Entry.objects.exclude(pk=obj.pk).first()

def get_body_format(self, obj):
return 'text'

class Meta:
model = models.Entry
model = Entry
fields = ('blog', 'headline', 'body_text', 'pub_date', 'mod_date',
'authors', 'comments', 'featured', 'suggested', 'tags')
read_only_fields = ('tags', )
Expand All @@ -78,7 +91,7 @@ class JSONAPIMeta:
class AuthorBioSerializer(serializers.ModelSerializer):

class Meta:
model = models.AuthorBio
model = AuthorBio
fields = ('author', 'body',)


Expand All @@ -88,7 +101,7 @@ class AuthorSerializer(serializers.ModelSerializer):
}

class Meta:
model = models.Author
model = Author
fields = ('name', 'email', 'bio')


Expand All @@ -99,41 +112,41 @@ class CommentSerializer(serializers.ModelSerializer):
}

class Meta:
model = models.Comment
model = Comment
exclude = ('created_at', 'modified_at',)
# fields = ('entry', 'body', 'author',)


class ArtProjectSerializer(serializers.ModelSerializer):
class Meta:
model = models.ArtProject
model = ArtProject
exclude = ('polymorphic_ctype',)


class ResearchProjectSerializer(serializers.ModelSerializer):
class Meta:
model = models.ResearchProject
model = ResearchProject
exclude = ('polymorphic_ctype',)


class ProjectSerializer(serializers.PolymorphicModelSerializer):
polymorphic_serializers = [ArtProjectSerializer, ResearchProjectSerializer]

class Meta:
model = models.Project
model = Project
exclude = ('polymorphic_ctype',)


class CompanySerializer(serializers.ModelSerializer):
current_project = relations.PolymorphicResourceRelatedField(
ProjectSerializer, queryset=models.Project.objects.all())
ProjectSerializer, queryset=Project.objects.all())
future_projects = relations.PolymorphicResourceRelatedField(
ProjectSerializer, queryset=models.Project.objects.all(), many=True)
ProjectSerializer, queryset=Project.objects.all(), many=True)

included_serializers = {
'current_project': ProjectSerializer,
'future_projects': ProjectSerializer,
}

class Meta:
model = models.Company
model = Company
27 changes: 15 additions & 12 deletions example/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import pytest
from pytest_factoryboy import register

from example import factories

register(factories.BlogFactory)
register(factories.AuthorFactory)
register(factories.AuthorBioFactory)
register(factories.EntryFactory)
register(factories.CommentFactory)
register(factories.ArtProjectFactory)
register(factories.ResearchProjectFactory)
register(factories.CompanyFactory)

from example.factories import (
BlogFactory, AuthorFactory, AuthorBioFactory, EntryFactory, CommentFactory,
TaggedItemFactory
)

register(BlogFactory)
register(AuthorFactory)
register(AuthorBioFactory)
register(EntryFactory)
register(CommentFactory)
register(TaggedItemFactory)
register(ArtProjectFactory)
register(ResearchProjectFactory)
register(CompanyFactory)

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

entry = entry_factory(blog=blog, authors=(author,))
comment_factory(entry=entry)
Expand Down
6 changes: 4 additions & 2 deletions example/urls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from rest_framework import routers

from example.views import (
BlogViewSet, EntryViewSet, AuthorViewSet, CommentViewSet, CompanyViewset, ProjectViewset,
EntryRelationshipView, BlogRelationshipView, CommentRelationshipView, AuthorRelationshipView)
BlogViewSet, EntryViewSet, AuthorViewSet, CommentViewSet, EntryRelationshipView,
BlogRelationshipView, CommentRelationshipView, AuthorRelationshipView,
CompanyViewset, ProjectViewset,
)
from .api.resources.identity import Identity, GenericIdentity

router = routers.DefaultRouter(trailing_slash=False)
Expand Down
9 changes: 5 additions & 4 deletions example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import rest_framework_json_api.metadata
import rest_framework_json_api.parsers
import rest_framework_json_api.renderers
from rest_framework_json_api.views import RelationshipView
from rest_framework_json_api.views import ModelViewSet, RelationshipView
from example.models import Blog, Entry, Author, Comment, Company, Project
from example.serializers import (
BlogSerializer, EntrySerializer, AuthorSerializer, CommentSerializer, CompanySerializer,
ProjectSerializer)
ProjectSerializer,
)

from rest_framework_json_api.utils import format_drf_errors

Expand Down Expand Up @@ -72,12 +73,12 @@ class CommentViewSet(ModelViewSet):
serializer_class = CommentSerializer


class CompanyViewset(viewsets.ModelViewSet):
class CompanyViewset(ModelViewSet):
queryset = Company.objects.all()
serializer_class = CompanySerializer


class ProjectViewset(viewsets.ModelViewSet):
class ProjectViewset(ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer

Expand Down
5 changes: 4 additions & 1 deletion requirements-development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
pytest>=2.9.0,<3.0
pytest-django
pytest-factoryboy
fake-factory
Faker
recommonmark
Sphinx
sphinx_rtd_theme
django-polymorphic
tox
mock
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.