File size: 2,255 Bytes
0bd6e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
785467e
 
 
 
0bd6e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 hello(request):
    return Response({"data":"Hello World"})

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