auth mechanism start

master
Mustafa Yontar 4 years ago
parent 55fe05c218
commit c3d64a6232
  1. 14
      restapi/resource.py
  2. 1
      restapi/views.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 = []

@ -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,

Loading…
Cancel
Save