auth mechanism start

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

@ -53,6 +53,7 @@ class ApiView(View):
""" """
if 'pk' in kwargs: if 'pk' in kwargs:
try: try:
qs = self.resource.to_qs(pk=kwargs.get('pk'))
count, data = self.resource.to_json(pk=kwargs.get('pk')) count, data = self.resource.to_json(pk=kwargs.get('pk'))
response = { response = {
'response': data, 'response': data,

Loading…
Cancel
Save