Subversion Repositories SmartDukaan

Rev

Rev 14960 | Rev 15229 | 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
14427 amit.gupta 7
from dtr.storage import Mongo, Mysql
13804 amit.gupta 8
import falcon
9
import json
14014 amit.gupta 10
import urlparse
13804 amit.gupta 11
class StoreOrder():
12
    def on_post(self, req, resp):
13
 
14
        try:
15
            string1 = req.stream.read()
16
            req_json = json.loads(string1, encoding='utf-8')
17
        except ValueError:
18
            raise falcon.HTTPError(falcon.HTTP_400,
19
                'Malformed JSON',
20
                'Could not decode the request body. The '
21
                'JSON was incorrect.')
22
 
23
        store = main.getStore(int(req_json['sourceId']))
24
        result = store.parseOrderRawHtml(int(req_json['orderId']), req_json['subTagId'], int(req_json['userId']), req_json['rawHtml'], req_json['orderSuccessUrl'])
25
        resp.body = json.dumps(result, encoding='utf-8')
26
 
27
    def on_get(self, req, resp, userId):
28
        page = req.get_param_as_int("page")
29
        window = req.get_param_as_int("window")
14003 amit.gupta 30
        string1 = req.get_param("searchMap")
14004 amit.gupta 31
        searchMap={}
14006 amit.gupta 32
        if string1 is None or string1 == "":
14007 amit.gupta 33
            pass
34
        else:
14006 amit.gupta 35
            try:
14007 amit.gupta 36
                searchMap = json.loads(string1, encoding='utf-8')
14006 amit.gupta 37
            except:
38
                raise falcon.HTTPError(falcon.HTTP_400,
39
                    'Malformed JSON',
40
                    'Could not decode the request body. The '
41
                    'JSON was incorrect.')
14003 amit.gupta 42
        result = Mongo.getMerchantOrdersByUser(int(userId), page, window, searchMap)
13868 amit.gupta 43
        resp.body = json.dumps(result, encoding='utf-8')
44
 
14352 amit.gupta 45
class Orders():
46
    def on_get(self, req, resp):
47
        page = req.get_param_as_int("page")
48
        window = req.get_param_as_int("window")
49
        string1 = req.get_param("searchMap")
50
        searchMap={}
51
        if string1 is None or string1 == "":
52
            pass
53
        else:
54
            try:
55
                searchMap = json.loads(string1, encoding='utf-8')
56
            except:
57
                raise falcon.HTTPError(falcon.HTTP_400,
58
                    'Malformed JSON',
59
                    'Could not decode the request body. The '
60
                    'JSON was incorrect.')
61
        result = Mongo.getMerchantOrdersByUser(None, page, window, searchMap)
62
        resp.body = json.dumps(result, encoding='utf-8')
63
 
13868 amit.gupta 64
class Track():
13939 amit.gupta 65
    def on_post(self, req, resp, userId):
13868 amit.gupta 66
        try:
14013 amit.gupta 67
            string1 = req.stream.read()
14014 amit.gupta 68
            req_obj = urlparse.parse_qs(string1)
14013 amit.gupta 69
        except ValueError:
70
            raise falcon.HTTPError(falcon.HTTP_400,
71
                'Malformed JSON',
72
                'Could not decode the request body. The '
73
                'JSON was incorrect.')
74
        try:
13927 amit.gupta 75
            store = main.getStore(req.get_param_as_int("storeId"))
76
            if req.get_param_as_int("storeId") == 1:
14015 amit.gupta 77
                result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
13927 amit.gupta 78
                resp.body = json.dumps({'result':result}, encoding='utf-8')
79
            else:
80
                resp.body = json.dumps({'result':'NOT_IMPLEMENTED'}, encoding='utf-8')
81
        except:
82
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
14087 amit.gupta 83
 
14096 amit.gupta 84
        print "Returned from track POST", resp.body
85
        resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
86
 
13868 amit.gupta 87
 
13939 amit.gupta 88
    def on_get(self, req, resp, userId):
13868 amit.gupta 89
        try:
90
            storeId = req.get_param_as_int("storeId")
13939 amit.gupta 91
            result = main.getStore(storeId).getTrackingUrls(int(userId))
13887 amit.gupta 92
            resp.body = json.dumps({'result':result}, encoding='utf-8')
13868 amit.gupta 93
        except ValueError:
94
            raise falcon.HTTPError(falcon.HTTP_400,
95
                'Malformed JSON',
96
                'Could not decode the request body. The '
97
                'JSON was incorrect.')
98
 
13927 amit.gupta 99
class Refunds():
13990 amit.gupta 100
    def on_get(self, req, resp, userId):
13927 amit.gupta 101
        try:
102
            page = req.get_param_as_int("page")
103
            window = req.get_param_as_int("window")
13990 amit.gupta 104
            result = Mongo.getRefunds(int(userId),page,window)
13927 amit.gupta 105
            resp.body = json.dumps(result, encoding='utf-8')
106
        except ValueError:
107
            raise falcon.HTTPError(falcon.HTTP_400,
108
                'Malformed JSON',
109
                'Could not decode the request body. The '
110
                'JSON was incorrect.')
111
 
14427 amit.gupta 112
class Transactions():
113
    def on_get(self, req, resp):
114
        try:
115
            page = req.get_param_as_int("page")
116
            window = req.get_param_as_int("window")
14725 amit.gupta 117
            user = req.get_param("user")
118
            status = req.get_param("status")
119
            if user == "" :
120
                user = None
121
            if status == "" :
122
                status = None
123
            result = Mysql.getOrders(page, window,status,user)
14427 amit.gupta 124
            resp.body = result
125
            resp.content_type = 'text/html'
126
        except ValueError:
127
            raise falcon.HTTPError(falcon.HTTP_400,
128
                'Malformed JSON',
129
                'Could not decode the request body. The '
130
                'JSON was incorrect.')
15081 amit.gupta 131
 
132
class SnapShot():
133
    def on_get(self, req, resp):
134
        try:
135
            resp.content_type = 'text/html'
136
            user = req.get_param_as_int("user")
137
            file = req.get_param("file") 
138
            if  file is not None:
139
                #fetch the page and return html
140
                with open ("/AmazonTrack/User%d/%s"%(user, file), "r") as myfile:
141
                    resp.body=myfile.read() 
142
            else:
143
                #return user specific urls
144
                resp.body=getUserUrls(user)
145
        except ValueError:
146
            raise falcon.HTTPError(falcon.HTTP_400,
147
                'Malformed JSON',
148
                'Could not decode the request body. The '
149
                'JSON was incorrect.')
150
 
151
def getUserUrls(user):
152
    pass
14427 amit.gupta 153
 
154
class RawHtml():
155
    def on_get(self, req, resp, orderId):
156
        try:
157
            result = Mysql.getOrderHtml(orderId)
158
            resp.body = result
159
            resp.content_type = 'text/html'
160
        except ValueError:
161
            raise falcon.HTTPError(falcon.HTTP_400,
162
                'Malformed JSON',
163
                'Could not decode the request body. The '
164
                'JSON was incorrect.')
165
 
13927 amit.gupta 166
class PendingRefunds():
167
    def on_get(self, req, resp,userId):
168
        try:
169
            result = Mongo.getPendingRefunds(int(userId))
170
            resp.body = json.dumps(result, encoding='utf-8')
171
        except ValueError:
172
            raise falcon.HTTPError(falcon.HTTP_400,
173
                'Malformed JSON',
174
                'Could not decode the request body. The '
14671 amit.gupta 175
                'JSON was incorrect.')
176
 
177
class PendingCashBacks():
178
    def on_get(self, req, resp,userId):
179
        try:
180
            result = Mongo.getPendingCashbacks(int(userId))
181
            resp.body = json.dumps(result, encoding='utf-8')
182
        except ValueError:
183
            raise falcon.HTTPError(falcon.HTTP_400,
184
                'Malformed JSON',
185
                'Could not decode the request body. The '
13927 amit.gupta 186
                'JSON was incorrect.')