Commit 816ccf58 authored by Ashish's avatar Ashish

Added count, fixed issues with list and pk

parent 39dd5724
......@@ -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
......
......@@ -7,7 +7,7 @@ from rest_framework.fields import (CharField, IntegerField, DateTimeField,
FloatField, BooleanField)
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,7 +36,8 @@ class AlchemyModelSerializer(serializers.Serializer):
Numeric: IntegerField,
DateTime: DateTimeField,
Boolean: BooleanField,
CLOB: CharField
CLOB: CharField,
DECIMAL: FloatField,
}
def __init__(self, *args, **kwargs):
......@@ -52,15 +53,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 +94,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
......@@ -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