views.py 1.39 KB
Newer Older
1
from django.http import HttpResponse
2
import json
3
import jsonpickle
4
from django.views.decorators.csrf import csrf_exempt
5
6
from agent.dataloader import DataLoader
from agent.communicator import Communicator
7
8

dl = DataLoader()
9
communicator = Communicator(dl)
10

11

12
def index(request):
13
14
    return HttpResponse("OK")

15

16
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
    image_url = 'https://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&MarketPlace=US&ASIN=' + id + '&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=SL250'

23
    communicator.set_product(id)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    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
@csrf_exempt
def message(request):
    parsed = json.loads(request.body)
    query_id = parsed['queryID']
45
46
47
    arg_id = parsed['argumentID']
    response = communicator.get_response(query_id, arg_id)
    return HttpResponse(jsonpickle.encode(response, unpicklable=False), content_type="application/json")