|
|
|
@ -22,6 +22,7 @@ class Resource: |
|
|
|
|
meta: dict = {} |
|
|
|
|
as_pk: str = 'id' |
|
|
|
|
query_document: Document = None |
|
|
|
|
mongo_qs = None |
|
|
|
|
|
|
|
|
|
def __init__(self, model: Document): |
|
|
|
|
self.model = model |
|
|
|
@ -66,7 +67,7 @@ class Resource: |
|
|
|
|
|
|
|
|
|
self.qs = qs |
|
|
|
|
|
|
|
|
|
def to_json(self, pk: str = None) -> tuple: |
|
|
|
|
def to_qs(self, pk: str = None) -> tuple: |
|
|
|
|
query = {} |
|
|
|
|
if self.qs: |
|
|
|
|
for key, val in self.qs.items(): |
|
|
|
@ -99,10 +100,19 @@ class Resource: |
|
|
|
|
query.update({key: val}) |
|
|
|
|
print(query) |
|
|
|
|
data = self.model.objects.filter(**query) |
|
|
|
|
if pk: |
|
|
|
|
data.get(**{self.as_pk: pk}) |
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
def to_json(self, pk: str = None) -> tuple: |
|
|
|
|
|
|
|
|
|
if self.mongo_qs is None: |
|
|
|
|
self.mongo_qs = self.to_qs(pk) |
|
|
|
|
|
|
|
|
|
data = self.mongo_qs |
|
|
|
|
count = data.count() |
|
|
|
|
if pk: |
|
|
|
|
json_data = self.parse_fields(data.get(**{self.as_pk: pk}), self.model) |
|
|
|
|
json_data = self.parse_fields(self.model) |
|
|
|
|
else: |
|
|
|
|
data = data[self.offset:self.limit + self.offset] |
|
|
|
|
json_data = [] |
|
|
|
|