Subversion Repositories SmartDukaan

Rev

Rev 17655 | Rev 17660 | 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
'''
17659 amit.gupta 6
from datetime import datetime
13804 amit.gupta 7
from dtr import main
16789 amit.gupta 8
from dtr.main import tprint
14427 amit.gupta 9
from dtr.storage import Mongo, Mysql
16167 amit.gupta 10
from dtr.utils import utils
17659 amit.gupta 11
from dtr.utils.utils import find_between
12
import cgi
13804 amit.gupta 13
import falcon
14
import json
17659 amit.gupta 15
import logging
16789 amit.gupta 16
import os.path
16167 amit.gupta 17
import re
16789 amit.gupta 18
import traceback
14014 amit.gupta 19
import urlparse
16789 amit.gupta 20
import xlrd
17276 amit.gupta 21
 
17659 amit.gupta 22
logging.basicConfig(filename='/var/log/uwsgi/order.log',level=logging.DEBUG)
17276 amit.gupta 23
 
17659 amit.gupta 24
 
17276 amit.gupta 25
order_fliters = {
26
                 "monitor":{
27
                    "trackingurlmissing":  {
28
                                                "displaylabel":"Tracking Url Missing",
29
                                                "query":{"subOrders.closed":False,"subOrders.trackingUrl":{"$exists":False}, "subOrders.trackMissing":True}
30
                                            }, 
31
                     "orderuntrackable":    {
32
                                                "displaylabel":"Untrackable Orders",   
33
                                                "query":{"trackError":True, "storeId":1, "subOrders.closed":False}
34
                                            },
35
                     "prematurelyclosed" :  {
36
                                                "displaylabel": "Prematurely Closed Orders",
37
                                                "query":{"closed":True, "subOrders.closed":False}
38
                                            },
39
 
40
                     "parsefailed":         {   
41
                                                "displaylabel": "Failed to Parse Orders",
17289 amit.gupta 42
                                                "query":{"subOrders":{"$exists": False}, "status":"parse_failed"}
17276 amit.gupta 43
                                            }
44
                     },
45
 
46
                 "user":{}
47
 
48
                 }
13804 amit.gupta 49
class StoreOrder():
50
    def on_post(self, req, resp):
51
 
52
        try:
53
            string1 = req.stream.read()
54
            req_json = json.loads(string1, encoding='utf-8')
17655 amit.gupta 55
            print "req_json",req_json
13804 amit.gupta 56
        except ValueError:
57
            raise falcon.HTTPError(falcon.HTTP_400,
58
                'Malformed JSON',
59
                'Could not decode the request body. The '
60
                'JSON was incorrect.')
61
 
62
        store = main.getStore(int(req_json['sourceId']))
63
        result = store.parseOrderRawHtml(int(req_json['orderId']), req_json['subTagId'], int(req_json['userId']), req_json['rawHtml'], req_json['orderSuccessUrl'])
64
        resp.body = json.dumps(result, encoding='utf-8')
65
 
66
    def on_get(self, req, resp, userId):
67
        page = req.get_param_as_int("page")
68
        window = req.get_param_as_int("window")
14003 amit.gupta 69
        string1 = req.get_param("searchMap")
15821 amit.gupta 70
        orderType = req.get_param("type")
14004 amit.gupta 71
        searchMap={}
15821 amit.gupta 72
        if orderType is not None and orderType != "":
15822 amit.gupta 73
            if orderType in ['needamazonorderdetail','needamazonordertrack']:
16167 amit.gupta 74
                searchMap={"$or":[{"subOrders.closed":False,"subOrders.trackingUrl":{"$exists":False},"subOrders.trackAfter":{"$lt":utils.getCurrTimeStamp()}}, {"status":"html_required"}], "storeId":1}
15821 amit.gupta 75
        elif string1 is not None and string1 != "":
14006 amit.gupta 76
            try:
14007 amit.gupta 77
                searchMap = json.loads(string1, encoding='utf-8')
14006 amit.gupta 78
            except:
79
                raise falcon.HTTPError(falcon.HTTP_400,
80
                    'Malformed JSON',
81
                    'Could not decode the request body. The '
82
                    'JSON was incorrect.')
14003 amit.gupta 83
        result = Mongo.getMerchantOrdersByUser(int(userId), page, window, searchMap)
13868 amit.gupta 84
        resp.body = json.dumps(result, encoding='utf-8')
85
 
17276 amit.gupta 86
 
87
class OrderFilters():
88
    def on_get(self, req, resp):
17282 amit.gupta 89
        filter_type = req.get_param("type")
90
        result = order_fliters.get(filter_type)
91
        resp.body = json.dumps(result, encoding='utf-8')
17276 amit.gupta 92
 
17282 amit.gupta 93
 
14352 amit.gupta 94
class Orders():
95
    def on_get(self, req, resp):
96
        page = req.get_param_as_int("page")
97
        window = req.get_param_as_int("window")
98
        string1 = req.get_param("searchMap")
17283 amit.gupta 99
        orderFilter = req.get_param("filter")
17282 amit.gupta 100
        filterType = req.get_param("filtertype")
14352 amit.gupta 101
        searchMap={}
17284 amit.gupta 102
        if filter and filterType:
17282 amit.gupta 103
            try:
17286 amit.gupta 104
                searchMap = order_fliters[filterType][orderFilter]['query']
17282 amit.gupta 105
            except:
106
                pass
17284 amit.gupta 107
        elif string1:
14352 amit.gupta 108
            try:
109
                searchMap = json.loads(string1, encoding='utf-8')
110
            except:
111
                raise falcon.HTTPError(falcon.HTTP_400,
112
                    'Malformed JSON',
113
                    'Could not decode the request body. The '
114
                    'JSON was incorrect.')
115
        result = Mongo.getMerchantOrdersByUser(None, page, window, searchMap)
116
        resp.body = json.dumps(result, encoding='utf-8')
117
 
13868 amit.gupta 118
class Track():
13939 amit.gupta 119
    def on_post(self, req, resp, userId):
13868 amit.gupta 120
        try:
14013 amit.gupta 121
            string1 = req.stream.read()
14014 amit.gupta 122
            req_obj = urlparse.parse_qs(string1)
17659 amit.gupta 123
            logging.info(string1)
14013 amit.gupta 124
        except ValueError:
125
            raise falcon.HTTPError(falcon.HTTP_400,
126
                'Malformed JSON',
127
                'Could not decode the request body. The '
128
                'JSON was incorrect.')
129
        try:
13927 amit.gupta 130
            store = main.getStore(req.get_param_as_int("storeId"))
17655 amit.gupta 131
            print "req_obj",req_obj
16371 amit.gupta 132
            result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
133
            resp.body = json.dumps({'result':result}, encoding='utf-8')
15791 manish.sha 134
            '''
135
            elif req.get_param_as_int("storeId") == 7:
136
                if 'myprofile' in req_obj['url'][0]:
137
                    result = store.parseMyProfileForEmailId(int(userId),req_obj['url'][0],req_obj['html'][0])
138
                else:
139
                    result = store.parseMyOrdersForEmailId(int(userId),req_obj['url'][0],req_obj['html'][0])
140
                resp.body = json.dumps({'result':result}, encoding='utf-8')
141
            '''
13927 amit.gupta 142
        except:
16392 amit.gupta 143
            traceback.print_exc()
13927 amit.gupta 144
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
14087 amit.gupta 145
 
13868 amit.gupta 146
 
13939 amit.gupta 147
    def on_get(self, req, resp, userId):
13868 amit.gupta 148
        try:
149
            storeId = req.get_param_as_int("storeId")
13939 amit.gupta 150
            result = main.getStore(storeId).getTrackingUrls(int(userId))
13887 amit.gupta 151
            resp.body = json.dumps({'result':result}, encoding='utf-8')
13868 amit.gupta 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
 
13927 amit.gupta 158
class Refunds():
13990 amit.gupta 159
    def on_get(self, req, resp, userId):
13927 amit.gupta 160
        try:
161
            page = req.get_param_as_int("page")
162
            window = req.get_param_as_int("window")
13990 amit.gupta 163
            result = Mongo.getRefunds(int(userId),page,window)
13927 amit.gupta 164
            resp.body = json.dumps(result, encoding='utf-8')
165
        except ValueError:
166
            raise falcon.HTTPError(falcon.HTTP_400,
167
                'Malformed JSON',
168
                'Could not decode the request body. The '
169
                'JSON was incorrect.')
170
 
14427 amit.gupta 171
class Transactions():
172
    def on_get(self, req, resp):
173
        try:
174
            page = req.get_param_as_int("page")
175
            window = req.get_param_as_int("window")
14725 amit.gupta 176
            user = req.get_param("user")
177
            status = req.get_param("status")
15731 amit.gupta 178
            source = req.get_param("source")
14725 amit.gupta 179
            if user == "" :
180
                user = None
181
            if status == "" :
182
                status = None
15731 amit.gupta 183
            result = Mysql.getOrders(page, window,status,user,source)
14427 amit.gupta 184
            resp.body = result
185
            resp.content_type = 'text/html'
186
        except ValueError:
187
            raise falcon.HTTPError(falcon.HTTP_400,
188
                'Malformed JSON',
189
                'Could not decode the request body. The '
190
                'JSON was incorrect.')
16789 amit.gupta 191
 
192
class Rejects():
193
    def on_get(self, req, resp):
194
        try:
195
            result="""<html><head><title>Upload Rejects</title></head><body>
196
            <h1>Upload rejects</h1>
16799 amit.gupta 197
            <form action="rejects" method="POST" enctype="multipart/form-data">
198
              <input type="file" name="rejects" accept=".xlsx, .xls">
16789 amit.gupta 199
              <input type="submit">
200
            </form>
201
 
202
            </body></html>"""
203
            resp.body = result
204
            resp.content_type = 'text/html'
205
        except ValueError:
206
            raise falcon.HTTPError(falcon.HTTP_400,
207
                'Malformed JSON',
208
                'Could not decode the request body. The '
209
                'JSON was incorrect.')
210
 
211
 
212
    def on_post(self, request, resp):
16829 amit.gupta 213
        res = None
16789 amit.gupta 214
        try:
16814 amit.gupta 215
            boundary = request.content_type.split(";")[1].strip().split("=")[1]
16825 amit.gupta 216
            pdict= {'boundary':boundary}
16802 amit.gupta 217
            filepath = "/tmp/rejectfile"
16806 amit.gupta 218
            with open(filepath, 'wb') as image_file:
219
                while True:
220
                    chunk = request.stream.read(4096)
16826 amit.gupta 221
                    if not chunk:
222
                        break
16825 amit.gupta 223
                    image_file.write(chunk)
16828 amit.gupta 224
            with open(filepath, 'r') as image_file:
16829 amit.gupta 225
                res = cgi.parse_multipart(image_file, pdict)['rejects'][0]
226
            if res:
227
                with open(filepath, 'wb') as image_file:
228
                    image_file.write(res)
16838 amit.gupta 229
                rejectCount = process_rejects(filepath)
16829 amit.gupta 230
                resp.content_type = 'text/html'
231
                resp.body = """
232
                    <html><head><title>Upload Rejects</title></head><body>
233
                <h1>Reject Successful</h1>
234
                <h2> %d orders rejected</h2>
235
 
236
                </body></html>
237
                    """%(rejectCount) 
16789 amit.gupta 238
        except ValueError:
239
            raise falcon.HTTPError(falcon.HTTP_400,
240
                'Malformed JSON',
241
                'Could not decode the request body. The '
242
                'JSON was incorrect.')
15081 amit.gupta 243
 
16789 amit.gupta 244
 
245
def process_rejects(filepath):
16838 amit.gupta 246
    rejectCount = 0
16789 amit.gupta 247
    workbook = xlrd.open_workbook(filepath)
248
    sheet = workbook.sheet_by_index(0)
249
    num_rows = sheet.nrows
250
    for rownum in range(1, num_rows):
251
        try:
16831 amit.gupta 252
            orderId, userId, merchantOrderId, subOrderId  = sheet.row_values(rownum)[0:4]
16838 amit.gupta 253
            rejectCount += Mongo.rejectCashback(int(orderId), subOrderId)
16789 amit.gupta 254
        except:
255
            traceback.print_exc()
16838 amit.gupta 256
    return rejectCount
16789 amit.gupta 257
 
15081 amit.gupta 258
class SnapShot():
259
    def on_get(self, req, resp):
260
        try:
261
            resp.content_type = 'text/html'
262
            user = req.get_param_as_int("user")
263
            file = req.get_param("file") 
264
            if  file is not None:
265
                #fetch the page and return html
266
                with open ("/AmazonTrack/User%d/%s"%(user, file), "r") as myfile:
267
                    resp.body=myfile.read() 
268
            else:
269
                #return user specific urls
270
                resp.body=getUserUrls(user)
271
        except ValueError:
272
            raise falcon.HTTPError(falcon.HTTP_400,
273
                'Malformed JSON',
274
                'Could not decode the request body. The '
275
                'JSON was incorrect.')
276
 
277
def getUserUrls(user):
278
    pass
14427 amit.gupta 279
 
280
class RawHtml():
281
    def on_get(self, req, resp, orderId):
282
        try:
283
            result = Mysql.getOrderHtml(orderId)
15544 amit.gupta 284
            if req.get_param("show") is None:
285
                result = re.subn(r'(src|href|style)=\s?(\'|").*?\2(?s)', '', re.subn(r'<(script|style).*?</\1>(?s)', '', result)[0])[0]
14427 amit.gupta 286
            resp.body = result
287
            resp.content_type = 'text/html'
288
        except ValueError:
289
            raise falcon.HTTPError(falcon.HTTP_400,
290
                'Malformed JSON',
291
                'Could not decode the request body. The '
292
                'JSON was incorrect.')
293
 
13927 amit.gupta 294
class PendingRefunds():
295
    def on_get(self, req, resp,userId):
296
        try:
297
            result = Mongo.getPendingRefunds(int(userId))
298
            resp.body = json.dumps(result, encoding='utf-8')
299
        except ValueError:
300
            raise falcon.HTTPError(falcon.HTTP_400,
301
                'Malformed JSON',
302
                'Could not decode the request body. The '
14671 amit.gupta 303
                'JSON was incorrect.')
17307 amit.gupta 304
 
305
class AmazonSummary():
306
    def on_get(self, req, resp,userId):
307
        try:
17455 amit.gupta 308
            result = getSummary(userId, req)
17307 amit.gupta 309
            if result:
310
                resp.body = result
311
            else:
312
                resp.body = ''
313
            resp.content_type = 'text/html'
314
        except ValueError:
315
            raise falcon.HTTPError(falcon.HTTP_400,
316
                'Malformed JSON',
317
                'Could not decode the request body. The '
318
                'JSON was incorrect.')
319
 
320
 
17455 amit.gupta 321
def getSummary(userId, req):
17308 amit.gupta 322
    directory = "/AmazonTrack/User" + userId
17307 amit.gupta 323
    date1 = datetime(2015,1,1)
324
    finalFile = None
325
    str1 = None
326
    try:
327
        for file in os.listdir(directory):
328
            if file.startswith("orderSummary"):
329
                date2 = datetime.strptime("2015-" + file.split("orderSummary")[1].split(":")[0], "%Y-%d-%m")
330
                if date2 > date1:
331
                    date1 = date2
332
                    finalFile=file
17455 amit.gupta 333
        result = open(directory + "/" + finalFile).read()
334
        if req.get_param("show") is None:
335
            result = re.subn(r'(src|href|style)=\s?(\'|").*?\2(?s)', '', re.subn(r'<(script|style).*?</\1>(?s)', '', result)[0])[0]
336
 
337
        return result
17307 amit.gupta 338
    except:
339
        print "Missing directory"
340
    return str1
14671 amit.gupta 341
 
342
class PendingCashBacks():
343
    def on_get(self, req, resp,userId):
344
        try:
345
            result = Mongo.getPendingCashbacks(int(userId))
346
            resp.body = json.dumps(result, encoding='utf-8')
347
        except ValueError:
348
            raise falcon.HTTPError(falcon.HTTP_400,
349
                'Malformed JSON',
350
                'Could not decode the request body. The '
16789 amit.gupta 351
                'JSON was incorrect.')
352
 
353
def tprint(*msg):
354
    print datetime.now(), "-", msg