Subversion Repositories SmartDukaan

Rev

Rev 16393 | Rev 16790 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 16393 Rev 16789
Line 2... Line 2...
2
Created on Feb 12, 2015
2
Created on Feb 12, 2015
3
 
3
 
4
@author: amit
4
@author: amit
5
'''
5
'''
6
from dtr import main
6
from dtr import main
-
 
7
from datetime import datetime
-
 
8
from dtr.main import tprint
7
from dtr.storage import Mongo, Mysql
9
from dtr.storage import Mongo, Mysql
8
from dtr.utils import utils
10
from dtr.utils import utils
9
import falcon
11
import falcon
10
import json
12
import json
-
 
13
import os.path
11
import re
14
import re
12
import urlparse
-
 
13
import traceback
15
import traceback
-
 
16
import urlparse
-
 
17
import xlrd
14
class StoreOrder():
18
class StoreOrder():
15
    def on_post(self, req, resp):
19
    def on_post(self, req, resp):
16
        
20
        
17
        try:
21
        try:
18
            string1 = req.stream.read()
22
            string1 = req.stream.read()
Line 136... Line 140...
136
        except ValueError:
140
        except ValueError:
137
            raise falcon.HTTPError(falcon.HTTP_400,
141
            raise falcon.HTTPError(falcon.HTTP_400,
138
                'Malformed JSON',
142
                'Malformed JSON',
139
                'Could not decode the request body. The '
143
                'Could not decode the request body. The '
140
                'JSON was incorrect.')
144
                'JSON was incorrect.')
-
 
145
            
-
 
146
class Rejects():
-
 
147
    def on_get(self, req, resp):
-
 
148
        try:
-
 
149
            result="""<html><head><title>Upload Rejects</title></head><body>
-
 
150
            <h1>Upload rejects</h1>
-
 
151
            <form action="demo_form.asp">
-
 
152
              <input type="file" name="rejects" accept="application/*">
-
 
153
              <input type="submit">
-
 
154
            </form>
-
 
155
   
-
 
156
            </body></html>"""
-
 
157
            resp.body = result
-
 
158
            resp.content_type = 'text/html'
-
 
159
        except ValueError:
-
 
160
            raise falcon.HTTPError(falcon.HTTP_400,
-
 
161
                'Malformed JSON',
-
 
162
                'Could not decode the request body. The '
-
 
163
                'JSON was incorrect.')
-
 
164
            
-
 
165
            
-
 
166
    def on_post(self, request, resp):
-
 
167
        try:
-
 
168
            uploadfile = request.files.get('fileToUpload')
-
 
169
            ext = uploadfile.filename.split(".")[1]
-
 
170
            if ext not in ['.xls','.xlsx']:
-
 
171
                raise falcon.HTTPError(falcon.HTTP_400, 'Invalid Extension use xls/xlsx', 'Invalid Extension use xls/xlsx')
-
 
172
            
-
 
173
            filepath = '/tmp/'+uploadfile.filename
-
 
174
            uploadfile.save(filepath)
-
 
175
            rejectCount = process_rejects(filepath) #this function is not yet implemented
-
 
176
            
-
 
177
            resp = """
-
 
178
                <html><head><title>Upload Rejects</title></head><body>
-
 
179
            <h1>Reject Successful</h1>
-
 
180
            <h2> %d orders rejected</h2>
-
 
181
   
-
 
182
            </body></html>
-
 
183
                """%(rejectCount) 
-
 
184
        except ValueError:
-
 
185
            raise falcon.HTTPError(falcon.HTTP_400,
-
 
186
                'Malformed JSON',
-
 
187
                'Could not decode the request body. The '
-
 
188
                'JSON was incorrect.')
-
 
189
 
-
 
190
 
-
 
191
def process_rejects(filepath):
-
 
192
    workbook = xlrd.open_workbook(filepath)
-
 
193
    sheet = workbook.sheet_by_index(0)
-
 
194
    num_rows = sheet.nrows
-
 
195
    for rownum in range(1, num_rows):
-
 
196
        try:
-
 
197
            orderId, userId, merchantOrderId, subOrderId  = sheet.row_values(rownum)[0:3]
-
 
198
            Mongo.rejectCashback(orderId, subOrderId)
-
 
199
        except:
-
 
200
            tprint("Could not reject " + str("orderId " + orderId + ", userId " + userId + ", merchantOrderId " + merchantOrderId + ", subOrderId "+ subOrderId))
-
 
201
            traceback.print_exc()
-
 
202
              
141
 
203
 
142
class SnapShot():
204
class SnapShot():
143
    def on_get(self, req, resp):
205
    def on_get(self, req, resp):
144
        try:
206
        try:
145
            resp.content_type = 'text/html'
207
            resp.content_type = 'text/html'
Line 193... Line 255...
193
            resp.body = json.dumps(result, encoding='utf-8')
255
            resp.body = json.dumps(result, encoding='utf-8')
194
        except ValueError:
256
        except ValueError:
195
            raise falcon.HTTPError(falcon.HTTP_400,
257
            raise falcon.HTTPError(falcon.HTTP_400,
196
                'Malformed JSON',
258
                'Malformed JSON',
197
                'Could not decode the request body. The '
259
                'Could not decode the request body. The '
198
                'JSON was incorrect.')
-
 
199
260
                'JSON was incorrect.')
-
 
261
            
-
 
262
def tprint(*msg):
-
 
263
    print datetime.now(), "-", msg
-
 
264
200
265