Subversion Repositories SmartDukaan

Rev

Rev 14096 | Rev 14427 | 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
 
14352 amit.gupta 46
class Orders():
47
    def on_get(self, req, resp):
48
        page = req.get_param_as_int("page")
49
        window = req.get_param_as_int("window")
50
        string1 = req.get_param("searchMap")
51
        searchMap={}
52
        if string1 is None or string1 == "":
53
            pass
54
        else:
55
            try:
56
                searchMap = json.loads(string1, encoding='utf-8')
57
            except:
58
                raise falcon.HTTPError(falcon.HTTP_400,
59
                    'Malformed JSON',
60
                    'Could not decode the request body. The '
61
                    'JSON was incorrect.')
62
        result = Mongo.getMerchantOrdersByUser(None, page, window, searchMap)
63
        resp.body = json.dumps(result, encoding='utf-8')
64
 
13868 amit.gupta 65
class Track():
13939 amit.gupta 66
    def on_post(self, req, resp, userId):
13868 amit.gupta 67
        try:
14013 amit.gupta 68
            string1 = req.stream.read()
14014 amit.gupta 69
            req_obj = urlparse.parse_qs(string1)
14013 amit.gupta 70
        except ValueError:
71
            raise falcon.HTTPError(falcon.HTTP_400,
72
                'Malformed JSON',
73
                'Could not decode the request body. The '
74
                'JSON was incorrect.')
75
        try:
13927 amit.gupta 76
            store = main.getStore(req.get_param_as_int("storeId"))
77
            if req.get_param_as_int("storeId") == 1:
14058 amit.gupta 78
                f = open("/tmp/order"+str(datetime.datetime.now()),'w')
14063 amit.gupta 79
                f.write(req_obj['html'][0]) # python will convert \n to os.linesep
14058 amit.gupta 80
                f.close() # you can omit in most cases as the destructor will call if
81
 
14015 amit.gupta 82
                result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
13927 amit.gupta 83
                resp.body = json.dumps({'result':result}, encoding='utf-8')
84
            else:
85
                resp.body = json.dumps({'result':'NOT_IMPLEMENTED'}, encoding='utf-8')
86
        except:
87
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
14087 amit.gupta 88
 
14096 amit.gupta 89
        print "Returned from track POST", resp.body
90
        resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
91
 
13868 amit.gupta 92
 
13939 amit.gupta 93
    def on_get(self, req, resp, userId):
13868 amit.gupta 94
        try:
95
            storeId = req.get_param_as_int("storeId")
13939 amit.gupta 96
            result = main.getStore(storeId).getTrackingUrls(int(userId))
13887 amit.gupta 97
            resp.body = json.dumps({'result':result}, encoding='utf-8')
13868 amit.gupta 98
        except ValueError:
99
            raise falcon.HTTPError(falcon.HTTP_400,
100
                'Malformed JSON',
101
                'Could not decode the request body. The '
102
                'JSON was incorrect.')
103
 
13927 amit.gupta 104
class Refunds():
13990 amit.gupta 105
    def on_get(self, req, resp, userId):
13927 amit.gupta 106
        try:
107
            page = req.get_param_as_int("page")
108
            window = req.get_param_as_int("window")
13990 amit.gupta 109
            result = Mongo.getRefunds(int(userId),page,window)
13927 amit.gupta 110
            resp.body = json.dumps(result, encoding='utf-8')
111
        except ValueError:
112
            raise falcon.HTTPError(falcon.HTTP_400,
113
                'Malformed JSON',
114
                'Could not decode the request body. The '
115
                'JSON was incorrect.')
116
 
117
class PendingRefunds():
118
    def on_get(self, req, resp,userId):
119
        try:
120
            result = Mongo.getPendingRefunds(int(userId))
121
            resp.body = json.dumps(result, encoding='utf-8')
122
        except ValueError:
123
            raise falcon.HTTPError(falcon.HTTP_400,
124
                'Malformed JSON',
125
                'Could not decode the request body. The '
126
                'JSON was incorrect.')