Commit 14d9f3f6 authored by ashish's avatar ashish

Merge pull request #9 from ashish-gore/modelcache

Support for count in response, misc fixes
parents 96e1bec4 75fcac6a
......@@ -30,7 +30,6 @@ class APIModelBuilder(object):
router = routers.SimpleRouter()
for model in self.models:
manager = type(
str('{}Manager'.format(model.__name__)),
self.base_managers + (AlchemyModelManager,),
......
......@@ -28,7 +28,7 @@ class AlchemyModelManager(object):
try:
pk = primary_key(self.cls)
except KeyNotFoundException:
return list()
pk = None
filter_dict = dict()
......@@ -48,14 +48,28 @@ class AlchemyModelManager(object):
query_pks[key] = other_pks[key]
query_pks.update(filter_dict)
queryset = self.session.query(self.cls.__dict__[pk]).filter_by(
**query_pks).all()
if pk:
queryset = self.session.query(self.cls.__dict__[pk]).filter_by(
**query_pks).all()
else:
queryset = self.session.query(self.cls).filter_by(
**query_pks).all()
else:
if filter_dict:
queryset = self.session.query(self.cls.__dict__[pk]).filter_by(
**filter_dict).all()
if pk:
queryset = self.session.query(
self.cls.__dict__[pk]).filter_by(
**filter_dict).all()
else:
queryset = self.session.query(self.cls).filter_by(
**filter_dict).all()
else:
queryset = self.session.query(self.cls.__dict__[pk]).all()
if pk:
queryset = self.session.query(self.cls.__dict__[pk]).all()
else:
# Limit to 1000 rows, this is worst case scenario
queryset = self.session.query(self.cls).limit(1000).all()
return queryset
......
......@@ -4,10 +4,10 @@ SQLALchemy and DRF fields to serialize/deserialize objects
'''
from rest_framework import serializers
from rest_framework.fields import (CharField, IntegerField, DateTimeField,
FloatField, BooleanField)
FloatField, BooleanField, DecimalField)
from sqlalchemy.types import (String, INTEGER, SMALLINT, BIGINT, VARCHAR,
CHAR, TIMESTAMP, DATE, Float, BigInteger,
Numeric, DateTime, Boolean, CLOB)
Numeric, DateTime, Boolean, CLOB, DECIMAL)
from django.utils.datastructures import SortedDict
from djangorest_alchemy.fields import AlchemyRelatedField, AlchemyUriField
# inspect introduced in 0.8
......@@ -36,12 +36,15 @@ class AlchemyModelSerializer(serializers.Serializer):
Numeric: IntegerField,
DateTime: DateTimeField,
Boolean: BooleanField,
CLOB: CharField
CLOB: CharField,
DECIMAL: DecimalField,
}
def __init__(self, *args, **kwargs):
assert "model_class" in kwargs, \
"model_class should be passed"
assert 'request' in kwargs['context'], \
"Context must contain request object"
self.cls = kwargs.pop('model_class')
super(AlchemyModelSerializer, self).__init__(*args, **kwargs)
......@@ -52,15 +55,14 @@ class AlchemyModelSerializer(serializers.Serializer):
mapper = class_mapper(self.cls.__class__)
r = self.context['request']
try:
# URI field for get pk field
pk_field = primary_key(self.cls.__class__)
ret['href'] = AlchemyUriField(source=pk_field,
path=r.build_absolute_uri(r.path))
except KeyNotFoundException:
return ret
r = self.context['request']
ret['href'] = AlchemyUriField(source=pk_field,
path=r.build_absolute_uri(r.path))
pass
# Get all the Column fields
for col_prop in mapper.iterate_properties:
......@@ -94,12 +96,12 @@ class AlchemyListSerializer(AlchemyModelSerializer):
try:
# URI field for get pk field
pk_field = primary_key(self.cls.__class__)
except KeyNotFoundException:
return ret
request = self.context['request']
ret["href"] = AlchemyUriField(source=pk_field,
path=request.build_absolute_uri(
request.path))
request = self.context['request']
ret["href"] = AlchemyUriField(source=pk_field,
path=request.build_absolute_uri
(request.path))
except KeyNotFoundException:
return super(AlchemyListSerializer, self).get_default_fields()
return ret
......@@ -22,6 +22,8 @@ from rest_framework.decorators import action
RESULTS_KEY = "results"
COUNT_KEY = "count"
PAGE_KEY = "page"
class PrimaryKeyMixin(object):
......@@ -104,6 +106,8 @@ class TestAlchemyViewSetIntegration(TestCase):
self.assertTrue(resp.status_code is status.HTTP_200_OK)
self.assertTrue(type(resp.data) is dict)
self.assertTrue(len(resp.data[RESULTS_KEY]) == 1)
self.assertTrue(resp.data[COUNT_KEY] == 1)
self.assertTrue(resp.data[PAGE_KEY] == 25)
def test_decl_retrieve(self):
resp = self.client.get('/api/declmodels/1/')
......
......@@ -84,6 +84,8 @@ class AlchemyModelViewSet(MultipleObjectMixin, ManagerMixin, viewsets.ViewSet):
queryset = mgr.list(other_pks=self.get_other_pks(request),
filters=request.QUERY_PARAMS)
count = len(queryset)
if self.paginate_by:
try:
queryset = self.get_page(queryset)
......@@ -94,7 +96,9 @@ class AlchemyModelViewSet(MultipleObjectMixin, ManagerMixin, viewsets.ViewSet):
mgr.model_class(),
{'request': request})
return Response({"results": serializer.data})
return Response({"count": count,
"page": self.paginate_by,
"results": serializer.data})
def retrieve(self, request, **kwargs):
'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment