Subversion Repositories SmartDukaan

Rev

Rev 14087 | Rev 14427 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on Feb 12, 2015

@author: amit
'''
from dtr import main
from dtr.storage import Mongo
import datetime
import falcon
import json
import urlparse
class StoreOrder():
    def on_post(self, req, resp):
        
        try:
            string1 = req.stream.read()
            req_json = json.loads(string1, encoding='utf-8')
        except ValueError:
            raise falcon.HTTPError(falcon.HTTP_400,
                'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect.')
            
        store = main.getStore(int(req_json['sourceId']))
        result = store.parseOrderRawHtml(int(req_json['orderId']), req_json['subTagId'], int(req_json['userId']), req_json['rawHtml'], req_json['orderSuccessUrl'])
        resp.body = json.dumps(result, encoding='utf-8')
    
    def on_get(self, req, resp, userId):
        page = req.get_param_as_int("page")
        window = req.get_param_as_int("window")
        string1 = req.get_param("searchMap")
        searchMap={}
        if string1 is None or string1 == "":
            pass
        else:
            try:
                searchMap = json.loads(string1, encoding='utf-8')
            except:
                raise falcon.HTTPError(falcon.HTTP_400,
                    'Malformed JSON',
                    'Could not decode the request body. The '
                    'JSON was incorrect.')
        result = Mongo.getMerchantOrdersByUser(int(userId), page, window, searchMap)
        resp.body = json.dumps(result, encoding='utf-8')
        
class Track():
    def on_post(self, req, resp, userId):
        try:
            string1 = req.stream.read()
            req_obj = urlparse.parse_qs(string1)
        except ValueError:
            raise falcon.HTTPError(falcon.HTTP_400,
                'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect.')
        try:
            store = main.getStore(req.get_param_as_int("storeId"))
            if req.get_param_as_int("storeId") == 1:
                f = open("/tmp/order"+str(datetime.datetime.now()),'w')
                f.write(req_obj['html'][0]) # python will convert \n to os.linesep
                f.close() # you can omit in most cases as the destructor will call if

                result = store.trackOrdersForUser(int(userId),req_obj['url'][0],req_obj['html'][0])
                resp.body = json.dumps({'result':result}, encoding='utf-8')
            else:
                resp.body = json.dumps({'result':'NOT_IMPLEMENTED'}, encoding='utf-8')
        except:
            resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
            
        print "Returned from track POST", resp.body
        resp.body = json.dumps({'result':'PARSE_ERROR'}, encoding='utf-8')
        
    
    def on_get(self, req, resp, userId):
        try:
            storeId = req.get_param_as_int("storeId")
            result = main.getStore(storeId).getTrackingUrls(int(userId))
            resp.body = json.dumps({'result':result}, encoding='utf-8')
        except ValueError:
            raise falcon.HTTPError(falcon.HTTP_400,
                'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect.')
        
class Refunds():
    def on_get(self, req, resp, userId):
        try:
            page = req.get_param_as_int("page")
            window = req.get_param_as_int("window")
            result = Mongo.getRefunds(int(userId),page,window)
            resp.body = json.dumps(result, encoding='utf-8')
        except ValueError:
            raise falcon.HTTPError(falcon.HTTP_400,
                'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect.')
        
class PendingRefunds():
    def on_get(self, req, resp,userId):
        try:
            result = Mongo.getPendingRefunds(int(userId))
            resp.body = json.dumps(result, encoding='utf-8')
        except ValueError:
            raise falcon.HTTPError(falcon.HTTP_400,
                'Malformed JSON',
                'Could not decode the request body. The '
                'JSON was incorrect.')