from django.shortcuts import render from rest_framework.response import Response from rest_framework.decorators import api_view import json from .models import Products, Categories import os # Create your views here. @api_view(['GET']) def getProductData(request, category): category = category.replace("-", " ").lower() obj = Products.objects.filter(category=category).all() data = {"data": []} for i in obj: temp = {} temp["name"] = i.name temp["score"] = i.score temp["image"] = i.image temp["propGroups"] = i.propGroups temp["propScore"] = i.propScore data["data"].append(temp) return Response(data) @api_view(['GET']) def searchQuery(request): query = request.GET.get('query') obj = Products.objects.filter(name__contains=query).all() data = {"data": []} for i in obj: temp = {} temp["name"] = i.name temp["score"] = i.score temp["image"] = i.image temp["propGroups"] = i.propGroups temp["propScore"] = i.propScore data["data"].append(temp) return Response(data) @api_view(['GET']) def fetchCategory(request): obj = Categories.objects.all() data = {"data": []} for i in obj: temp = {} temp["name"] = i.name temp["link"] = i.link data["data"].append(temp) return Response(data) # path = "./data/" # dir_list = os.listdir(path) # print(dir_list) # for path in dir_list: # f = open('./data/'+path, "r") # data = json.loads(f.read()) # for i in data["data"]: # try: # obj = Products(name=i["name"], score=i["score"], image=i["image"], # propGroups=i["propGroups"], propScore=i["propScore"], category=path.split(".json")[0].replace("-", " ")) # obj.save() # except: # pass # print(path) # path = "./data/" # dir_list = os.listdir(path) # print(dir_list) # for path in dir_list: # obj = Categories(name=path.split(".json")[0].replace("-", " ").title()) # obj.save() # print(path)