From c3d64a6232b5cc69eefd0d427ee77d26cd12d6cb Mon Sep 17 00:00:00 2001 From: Mustafa Yontar Date: Mon, 1 Feb 2021 14:03:39 +0300 Subject: [PATCH] auth mechanism start --- restapi/resource.py | 14 ++++++++++++-- restapi/views.py | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/restapi/resource.py b/restapi/resource.py index e6dc975..81b2364 100644 --- a/restapi/resource.py +++ b/restapi/resource.py @@ -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 = [] diff --git a/restapi/views.py b/restapi/views.py index e3811a9..e3681ef 100644 --- a/restapi/views.py +++ b/restapi/views.py @@ -53,6 +53,7 @@ class ApiView(View): """ if 'pk' in kwargs: try: + qs = self.resource.to_qs(pk=kwargs.get('pk')) count, data = self.resource.to_json(pk=kwargs.get('pk')) response = { 'response': data,