Subversion Repositories SmartDukaan

Rev

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