Subversion Repositories SmartDukaan

Rev

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