Subversion Repositories SmartDukaan

Rev

Rev 14058 | Rev 14087 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13804 amit.gupta 1
'''
2
Created on Feb 12, 2015
3
 
4
@author: amit
5
'''
6
from dtr import main
7
from dtr.storage import Mongo
14058 amit.gupta 8
import datetime
13804 amit.gupta 9
import falcon
10
import json
14014 amit.gupta 11
import urlparse
13804 amit.gupta 12
class StoreOrder():
13
    def on_post(self, req, resp):
14
 
15
        try:
16
            string1 = req.stream.read()
17
            req_json = json.loads(string1, encoding='utf-8')
18
        except ValueError:
19
            raise falcon.HTTPError(falcon.HTTP_400,
20
                'Malformed JSON',
21
                'Could not decode the request body. The '
22
                'JSON was incorrect.')
23
 
24
        store = main.getStore(int(req_json['sourceId']))
25
        result = store.parseOrderRawHtml(int(req_json['orderId']), req_json['subTagId'], int(req_json['userId']), req_json['rawHtml'], req_json['orderSuccessUrl'])
26
        resp.body = json.dumps(result, encoding='utf-8')
27
 
28
    def on_get(self, req, resp, userId):
29
        page = req.get_param_as_int("page")
30
        window = req.get_param_as_int("window")
14003 amit.gupta 31
        string1 = req.get_param("searchMap")
14004 amit.gupta 32
        searchMap={}
14006 amit.gupta 33
        if string1 is None or string1 == "":
14007 amit.gupta 34
            pass
35
        else:
14006 amit.gupta 36
            try:
14007 amit.gupta 37
                searchMap = json.loads(string1, encoding='utf-8')
14006 amit.gupta 38
            except:
39
                raise falcon.HTTPError(falcon.HTTP_400,
40
                    'Malformed JSON',
41
                    'Could not decode the request body. The '
42
                    'JSON was incorrect.')
14003 amit.gupta 43
        result = Mongo.getMerchantOrdersByUser(int(userId), page, window, searchMap)
13868 amit.gupta 44
        resp.body = json.dumps(result, encoding='utf-8')
45
 
46
class Track():
13939 amit.gupta 47
    def on_post(self, req, resp, userId):
13868 amit.gupta 48
        try:
14013 amit.gupta 49
            string1 = req.stream.read()
14014 amit.gupta 50
            req_obj = urlparse.parse_qs(string1)
14013 amit.gupta 51
        except ValueError:
52
            raise falcon.HTTPError(falcon.HTTP_400,
53
                'Malformed JSON',
54
                'Could not decode the request body. The '
55
                'JSON was incorrect.')
56
        try:
13927 amit.gupta 57
            store = main.getStore(req.get_param_as_int("storeId"))
58
            if req.get_param_as_int("storeId") == 1:
14058 amit.gupta 59
                f = open("/tmp/order"+str(datetime.datetime.now()),'w')
14063 amit.gupta 60
                f.write(req_obj['html'][0]) # python will convert \n to os.linesep
14058 amit.gupta 61
                f.close() # you can omit in most cases as the destructor will call if
62
 
14015 amit.gupta 63
                result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
13927 amit.gupta 64
                resp.body = json.dumps({'result':result}, encoding='utf-8')
65
            else:
66
                resp.body = json.dumps({'result':'NOT_IMPLEMENTED'}, encoding='utf-8')
67
        except:
68
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
13868 amit.gupta 69
 
13939 amit.gupta 70
    def on_get(self, req, resp, userId):
13868 amit.gupta 71
        try:
72
            storeId = req.get_param_as_int("storeId")
13939 amit.gupta 73
            result = main.getStore(storeId).getTrackingUrls(int(userId))
13887 amit.gupta 74
            resp.body = json.dumps({'result':result}, encoding='utf-8')
13868 amit.gupta 75
        except ValueError:
76
            raise falcon.HTTPError(falcon.HTTP_400,
77
                'Malformed JSON',
78
                'Could not decode the request body. The '
79
                'JSON was incorrect.')
80
 
13927 amit.gupta 81
class Refunds():
13990 amit.gupta 82
    def on_get(self, req, resp, userId):
13927 amit.gupta 83
        try:
84
            page = req.get_param_as_int("page")
85
            window = req.get_param_as_int("window")
13990 amit.gupta 86
            result = Mongo.getRefunds(int(userId),page,window)
13927 amit.gupta 87
            resp.body = json.dumps(result, encoding='utf-8')
88
        except ValueError:
89
            raise falcon.HTTPError(falcon.HTTP_400,
90
                'Malformed JSON',
91
                'Could not decode the request body. The '
92
                'JSON was incorrect.')
93
 
94
class PendingRefunds():
95
    def on_get(self, req, resp,userId):
96
        try:
97
            result = Mongo.getPendingRefunds(int(userId))
98
            resp.body = json.dumps(result, encoding='utf-8')
99
        except ValueError:
100
            raise falcon.HTTPError(falcon.HTTP_400,
101
                'Malformed JSON',
102
                'Could not decode the request body. The '
103
                'JSON was incorrect.')