Subversion Repositories SmartDukaan

Rev

Rev 15544 | Rev 15791 | 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
15229 amit.gupta 11
import re
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:
14015 amit.gupta 78
                result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
13927 amit.gupta 79
                resp.body = json.dumps({'result':result}, encoding='utf-8')
80
            else:
81
                resp.body = json.dumps({'result':'NOT_IMPLEMENTED'}, encoding='utf-8')
82
        except:
83
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
14087 amit.gupta 84
 
14096 amit.gupta 85
        print "Returned from track POST", resp.body
86
        resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
87
 
13868 amit.gupta 88
 
13939 amit.gupta 89
    def on_get(self, req, resp, userId):
13868 amit.gupta 90
        try:
91
            storeId = req.get_param_as_int("storeId")
13939 amit.gupta 92
            result = main.getStore(storeId).getTrackingUrls(int(userId))
13887 amit.gupta 93
            resp.body = json.dumps({'result':result}, encoding='utf-8')
13868 amit.gupta 94
        except ValueError:
95
            raise falcon.HTTPError(falcon.HTTP_400,
96
                'Malformed JSON',
97
                'Could not decode the request body. The '
98
                'JSON was incorrect.')
99
 
13927 amit.gupta 100
class Refunds():
13990 amit.gupta 101
    def on_get(self, req, resp, userId):
13927 amit.gupta 102
        try:
103
            page = req.get_param_as_int("page")
104
            window = req.get_param_as_int("window")
13990 amit.gupta 105
            result = Mongo.getRefunds(int(userId),page,window)
13927 amit.gupta 106
            resp.body = json.dumps(result, encoding='utf-8')
107
        except ValueError:
108
            raise falcon.HTTPError(falcon.HTTP_400,
109
                'Malformed JSON',
110
                'Could not decode the request body. The '
111
                'JSON was incorrect.')
112
 
14427 amit.gupta 113
class Transactions():
114
    def on_get(self, req, resp):
115
        try:
116
            page = req.get_param_as_int("page")
117
            window = req.get_param_as_int("window")
14725 amit.gupta 118
            user = req.get_param("user")
119
            status = req.get_param("status")
15731 amit.gupta 120
            source = req.get_param("source")
14725 amit.gupta 121
            if user == "" :
122
                user = None
123
            if status == "" :
124
                status = None
15731 amit.gupta 125
            result = Mysql.getOrders(page, window,status,user,source)
14427 amit.gupta 126
            resp.body = result
127
            resp.content_type = 'text/html'
128
        except ValueError:
129
            raise falcon.HTTPError(falcon.HTTP_400,
130
                'Malformed JSON',
131
                'Could not decode the request body. The '
132
                'JSON was incorrect.')
15081 amit.gupta 133
 
134
class SnapShot():
135
    def on_get(self, req, resp):
136
        try:
137
            resp.content_type = 'text/html'
138
            user = req.get_param_as_int("user")
139
            file = req.get_param("file") 
140
            if  file is not None:
141
                #fetch the page and return html
142
                with open ("/AmazonTrack/User%d/%s"%(user, file), "r") as myfile:
143
                    resp.body=myfile.read() 
144
            else:
145
                #return user specific urls
146
                resp.body=getUserUrls(user)
147
        except ValueError:
148
            raise falcon.HTTPError(falcon.HTTP_400,
149
                'Malformed JSON',
150
                'Could not decode the request body. The '
151
                'JSON was incorrect.')
152
 
153
def getUserUrls(user):
154
    pass
14427 amit.gupta 155
 
156
class RawHtml():
157
    def on_get(self, req, resp, orderId):
158
        try:
159
            result = Mysql.getOrderHtml(orderId)
15544 amit.gupta 160
            if req.get_param("show") is None:
161
                result = re.subn(r'(src|href|style)=\s?(\'|").*?\2(?s)', '', re.subn(r'<(script|style).*?</\1>(?s)', '', result)[0])[0]
14427 amit.gupta 162
            resp.body = result
163
            resp.content_type = 'text/html'
164
        except ValueError:
165
            raise falcon.HTTPError(falcon.HTTP_400,
166
                'Malformed JSON',
167
                'Could not decode the request body. The '
168
                'JSON was incorrect.')
169
 
13927 amit.gupta 170
class PendingRefunds():
171
    def on_get(self, req, resp,userId):
172
        try:
173
            result = Mongo.getPendingRefunds(int(userId))
174
            resp.body = json.dumps(result, encoding='utf-8')
175
        except ValueError:
176
            raise falcon.HTTPError(falcon.HTTP_400,
177
                'Malformed JSON',
178
                'Could not decode the request body. The '
14671 amit.gupta 179
                'JSON was incorrect.')
180
 
181
class PendingCashBacks():
182
    def on_get(self, req, resp,userId):
183
        try:
184
            result = Mongo.getPendingCashbacks(int(userId))
185
            resp.body = json.dumps(result, encoding='utf-8')
186
        except ValueError:
187
            raise falcon.HTTPError(falcon.HTTP_400,
188
                'Malformed JSON',
189
                'Could not decode the request body. The '
13927 amit.gupta 190
                'JSON was incorrect.')