Skip to content

Commit

Permalink
fully independent from wq.db
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Mar 7, 2016
1 parent 6897b99 commit 80f158e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 35 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ python:
- "3.4"
env:
global:
- DRF="https://github.com/tomchristie/django-rest-framework/archive/master.zip"
- DRF="djangorestframework==3.3.2"
- DJANGO="django==1.9.3"
- WQDB=https://github.com/wq/wq.db/archive/master.zip"
matrix:
- LINT=
- LINT=1
install:
- pip install $DJANGO
- pip install $DRF
- pip install $WQDB
- pip install flake8
script:
- ./runtests.sh
25 changes: 16 additions & 9 deletions natural_keys/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from wq.db.rest.serializers import BaseModelSerializer
from wq.db.patterns.base.serializers import AttachedModelSerializer
from rest_framework import serializers
from rest_framework.utils import model_meta
from html_json_forms.serializers import JSONFormModelSerializer
from .models import NaturalKeyModel


Expand Down Expand Up @@ -35,7 +34,7 @@ def filter_queryset(self, attrs, queryset):
)


class NaturalKeySerializer(BaseModelSerializer):
class NaturalKeySerializer(JSONFormModelSerializer):
"""
Self-nesting Serializer for NaturalKeyModels
"""
Expand Down Expand Up @@ -90,7 +89,7 @@ class Meta:
depth = 1


class NaturalKeyModelSerializer(AttachedModelSerializer):
class NaturalKeyModelSerializer(JSONFormModelSerializer):
"""
Serializer for models with one or more foreign keys to a NaturalKeyModel
"""
Expand Down Expand Up @@ -123,11 +122,8 @@ def build_relational_field(self, field_name, relation_info):
def get_fields(self):
fields = super(NaturalKeyModelSerializer, self).get_fields()
info = model_meta.get_field_info(self.Meta.model)
for key in fields:
if not key.endswith('_id'):
continue
field = key[:-3]
if field in fields or field not in info.relations:
for field in fields:
if field not in info.relations:
continue
relation_info = info.relations[field]
if not issubclass(relation_info.related_model, NaturalKeyModel):
Expand Down Expand Up @@ -163,3 +159,14 @@ def convert_natural_keys(self, validated_data):
validated_data[name] = fields[name].create(
validated_data[name]
)

@classmethod
def for_model(cls, model_class):
# c.f. wq.db.rest.serializers.ModelSerializer
class Serializer(cls):
class Meta(cls.Meta):
model = model_class
return Serializer

class Meta:
pass
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def parse_markdown_readme():
packages=['natural_keys'],
description=LONG_DESCRIPTION.strip(),
long_description=parse_markdown_readme(),
install_requires=[
'html-json-forms>=0.1.1',
],
classifiers=[
'Framework :: Django',
'Development Status :: 4 - Beta',
Expand Down
3 changes: 0 additions & 3 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
}

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'wq.db.rest',
'tests.test_app',
]
13 changes: 0 additions & 13 deletions tests/test_app/rest.py

This file was deleted.

9 changes: 9 additions & 0 deletions tests/test_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import routers
from .views import NaturalKeyChildViewSet, ModelWithNaturalKeyViewSet


router = routers.DefaultRouter()
router.register(r'naturalkeychilds', NaturalKeyChildViewSet)
router.register(r'modelwithnaturalkeys', ModelWithNaturalKeyViewSet)

urlpatterns = router.urls
17 changes: 17 additions & 0 deletions tests/test_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import viewsets
from .models import NaturalKeyChild, ModelWithNaturalKey
from natural_keys import NaturalKeySerializer, NaturalKeyModelSerializer


class NaturalKeyChildViewSet(viewsets.ModelViewSet):
queryset = NaturalKeyChild.objects.all()
serializer_class = NaturalKeySerializer.for_model(
NaturalKeyChild
)


class ModelWithNaturalKeyViewSet(viewsets.ModelViewSet):
queryset = ModelWithNaturalKey.objects.all()
serializer_class = NaturalKeyModelSerializer.for_model(
ModelWithNaturalKey
)
5 changes: 0 additions & 5 deletions tests/test_naturalkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
NaturalKeyParent, NaturalKeyChild, ModelWithNaturalKey
)
from natural_keys import NaturalKeySerializer
from django.contrib.auth.models import User
from django.db.utils import IntegrityError

# Tests for natural key models
Expand Down Expand Up @@ -74,10 +73,6 @@ def test_naturalkey_duplicate(self):


class NaturalKeyRestTestCase(APITestCase):
def setUp(self):
self.user = User.objects.create(username='testuser', is_superuser=True)
self.client.force_authenticate(user=self.user)

def test_naturalkey_rest_serializer(self):
# Serializer should include validator
serializer = NaturalKeySerializer.for_model(NaturalKeyChild)
Expand Down
3 changes: 1 addition & 2 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.conf.urls import patterns, url, include
from wq.db import rest


urlpatterns = patterns(
'',
url('', include(rest.router.urls)),
url('', include('tests.test_app.urls')),
)

0 comments on commit 80f158e

Please sign in to comment.