Skip to content

Commit

Permalink
Merge pull request #9 from kckaiwei/master
Browse files Browse the repository at this point in the history
adding support for manager class w/ test
  • Loading branch information
jiaaro authored Sep 18, 2018
2 parents 5930843 + 285dc1c commit 8f096cf
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
11 changes: 8 additions & 3 deletions jsonate/json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.db.models.query import ValuesQuerySet

from django.db.models.query import QuerySet
from django.db.models import Model
from django.db.models import Model, Manager
from django.db.models.fields.related import ForeignKey
from django.db.models.fields.files import FieldFile

Expand Down Expand Up @@ -54,8 +54,9 @@ def jsonate_fields(model):
fields = getattr(model._meta, 'jsonate_fields', all_fields)

serialize = set(fields).difference(set(excluded))

return tuple(field for field in model._meta.fields

# Getting all fields, including hidden fields as options
return tuple(field for field in model._meta.get_fields()
if field.name in serialize)

#########################
Expand Down Expand Up @@ -98,6 +99,10 @@ def map_queryset(obj):
fields = jsonate_fields(obj.model)
return obj.values(*[field.name for field in fields])

# Managers are typically hidden fields, and must be specified via meta fields
@register_typemap(Manager)
def map_manager(obj):
return obj.get_queryset()

@register_typemap(Model)
def map_model_instance(obj):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='jsonate',
version='0.5.0',
version='0.5.1',

author='James Robert',
author_email='[email protected]',
Expand Down
30 changes: 30 additions & 0 deletions test_project/test_app/migrations/0004_auto_20180914_1341.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-09-14 13:41
from __future__ import unicode_literals

from django.db import migrations, models
import jsonate.fields
import test_app.models


class Migration(migrations.Migration):

dependencies = [
('test_app', '0003_withjsonatefieldexpectinglist'),
]

operations = [
migrations.CreateModel(
name='MyModelWithRelation',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('many_to_many', models.ManyToManyField(related_name='many_to_my_model', to='test_app.MyModel')),
],
),
migrations.AlterField(
model_name='withjsonatefieldexpectinglist',
name='some_json_data',
field=jsonate.fields.JsonateField(default=[], validators=[test_app.models.validate_list]),
),
]
26 changes: 26 additions & 0 deletions test_project/test_app/migrations/0005_auto_20180914_1343.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-09-14 13:43
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('test_app', '0004_auto_20180914_1341'),
]

operations = [
migrations.RemoveField(
model_name='mymodelwithrelation',
name='many_to_many',
),
migrations.AddField(
model_name='mymodelwithrelation',
name='many_to_many',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='many_to_my_model', to='test_app.MyModel'),
preserve_default=False,
),
]
20 changes: 20 additions & 0 deletions test_project/test_app/migrations/0006_auto_20180914_1352.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-09-14 13:52
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('test_app', '0005_auto_20180914_1343'),
]

operations = [
migrations.RenameField(
model_name='mymodelwithrelation',
old_name='many_to_many',
new_name='to_many',
),
]
24 changes: 24 additions & 0 deletions test_project/test_app/migrations/0007_auto_20180914_1355.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-09-14 13:55
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('test_app', '0006_auto_20180914_1352'),
]

operations = [
migrations.RemoveField(
model_name='mymodelwithrelation',
name='to_many',
),
migrations.AddField(
model_name='mymodelwithrelation',
name='to_many',
field=models.ManyToManyField(related_name='many_to_my_model', to='test_app.MyModel'),
),
]
6 changes: 5 additions & 1 deletion test_project/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from jsonate.fields import JsonateField

class MyModel(models.Model):
foreign_key = models.ForeignKey(User)
foreign_key = models.ForeignKey(User, on_delete=models.CASCADE)
normal_field1 = models.CharField(max_length=25, default="field1")
normal_field2 = models.CharField(max_length=25, default='field2')

Expand All @@ -29,6 +29,10 @@ class MyModel(models.Model):
class Meta(object):
jsonate_exclude = ('sensitive_field1',)

class MyModelWithRelation(models.Model):
name = models.CharField(max_length=100)
to_many = models.ManyToManyField(MyModel, related_name="many_to_my_model")

class MyModelWithJsonateField(models.Model):
some_name = models.CharField(max_length=255)
some_json_data = JsonateField(null=True, blank=True)
Expand Down
7 changes: 6 additions & 1 deletion test_project/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from jsonate import jsonate
from jsonate.django_ver import django_18

from .models import MyModel, MyModelWithJsonateField, WithJsonateFieldExpectingList
from .models import MyModel, MyModelWithJsonateField, WithJsonateFieldExpectingList, MyModelWithRelation


def destroy_media_folder(folder):
Expand All @@ -41,6 +41,9 @@ def setUp(self):
self.model.file_field.save("text_file.txt", ContentFile("Any Old Content"))
self.model.image_field.save("image_file.wbm", ContentFile('\x00\x00\x01\x01\x80'))
self.model.save()
self.related_model = MyModelWithRelation(name="related_model")
self.related_model.save()
self.related_model.to_many.add(self.model)

def tearDown(self):
destroy_media_folder("files")
Expand Down Expand Up @@ -140,6 +143,8 @@ def test_basic_serialization(self):
"file_field": "files/text_file.txt"
}

self.assertJsonEqual(jsonate(self.model.many_to_my_model), [{"id": 1, "name": "related_model"}])

self.assertJsonEqual(jsonate(self.model), mymodel_data)
self.assertJsonEqual(jsonate(MyModel.objects.all()), [mymodel_data])

Expand Down

0 comments on commit 8f096cf

Please sign in to comment.