Subversion Repositories SmartDukaan

Rev

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