views.py 1.34 KB
Newer Older
1
2
from django.http import JsonResponse, HttpResponse
import json
3
import jsonpickle
4
5
6
7
from django.views.decorators.csrf import csrf_exempt

import sys
sys.path.append('/Users/joeloksanen/individual_project/ADA')
8
9
from dataloader import DataLoader
from communicator import Communicator
10
11

dl = DataLoader()
12
13

def index(request):
14
15
16
    return HttpResponse("OK")

def product(request):
17
    id = request.GET.get('id', '')
18

19
20
    name = dl.get_product_name(id)
    star_rating = dl.get_avg_star_rating(id)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    image_url = 'https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&MarketPlace=US&ASIN=' + id + '&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=SL250'

    communicator = Communicator(id, dl)
    init_message = communicator.get_init_message()

    class Empty:
        pass

    product_info = Empty()
    product_info.id = id
    product_info.name = name
    product_info.starRating = star_rating
    product_info.imageURL = image_url
    init_response = Empty()
    init_response.productInfo = product_info
    init_response.message = init_message

    return HttpResponse(jsonpickle.encode(init_response, unpicklable=False), content_type="application/json")
39
40
41
42
43
44
45
46
47

@csrf_exempt
def message(request):
    parsed = json.loads(request.body)
    arg_id = parsed['argumentID']
    query_id = parsed['queryID']
    print(arg_id)
    print(query_id)
    return HttpResponse("OK")