Subversion Repositories SmartDukaan

Rev

Rev 15081 | Rev 15544 | 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")
120
            if user == "" :
121
                user = None
122
            if status == "" :
123
                status = None
124
            result = Mysql.getOrders(page, window,status,user)
14427 amit.gupta 125
            resp.body = result
126
            resp.content_type = 'text/html'
127
        except ValueError:
128
            raise falcon.HTTPError(falcon.HTTP_400,
129
                'Malformed JSON',
130
                'Could not decode the request body. The '
131
                'JSON was incorrect.')
15081 amit.gupta 132
 
133
class SnapShot():
134
    def on_get(self, req, resp):
135
        try:
136
            resp.content_type = 'text/html'
137
            user = req.get_param_as_int("user")
138
            file = req.get_param("file") 
139
            if  file is not None:
140
                #fetch the page and return html
141
                with open ("/AmazonTrack/User%d/%s"%(user, file), "r") as myfile:
142
                    resp.body=myfile.read() 
143
            else:
144
                #return user specific urls
145
                resp.body=getUserUrls(user)
146
        except ValueError:
147
            raise falcon.HTTPError(falcon.HTTP_400,
148
                'Malformed JSON',
149
                'Could not decode the request body. The '
150
                'JSON was incorrect.')
151
 
152
def getUserUrls(user):
153
    pass
14427 amit.gupta 154
 
155
class RawHtml():
156
    def on_get(self, req, resp, orderId):
157
        try:
158
            result = Mysql.getOrderHtml(orderId)
15229 amit.gupta 159
            result = re.subn(r'(src|href|style)=\s?(\'|").*?\2(?s)', '', re.subn(r'<(script|style).*?</\1>(?s)', '', result)[0])[0]
14427 amit.gupta 160
            resp.body = result
161
            resp.content_type = 'text/html'
162
        except ValueError:
163
            raise falcon.HTTPError(falcon.HTTP_400,
164
                'Malformed JSON',
165
                'Could not decode the request body. The '
166
                'JSON was incorrect.')
167
 
13927 amit.gupta 168
class PendingRefunds():
169
    def on_get(self, req, resp,userId):
170
        try:
171
            result = Mongo.getPendingRefunds(int(userId))
172
            resp.body = json.dumps(result, encoding='utf-8')
173
        except ValueError:
174
            raise falcon.HTTPError(falcon.HTTP_400,
175
                'Malformed JSON',
176
                'Could not decode the request body. The '
14671 amit.gupta 177
                'JSON was incorrect.')
178
 
179
class PendingCashBacks():
180
    def on_get(self, req, resp,userId):
181
        try:
182
            result = Mongo.getPendingCashbacks(int(userId))
183
            resp.body = json.dumps(result, encoding='utf-8')
184
        except ValueError:
185
            raise falcon.HTTPError(falcon.HTTP_400,
186
                'Malformed JSON',
187
                'Could not decode the request body. The '
13927 amit.gupta 188
                'JSON was incorrect.')