# Path: mod/models/cookie_cache.py
# class CookieCache(Base):
#     __tablename__ = 'cookie'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(256), nullable=False)
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/cookie.py
# def getCookie(db,cardnum,card_pwd):
# 	state = 1
# 	ret = {'code':200,'content':''}
# 	try:
# 		result = db.query(CookieCache).filter(CookieCache.cardnum==cardnum).one()
# 		if (result.date+COOKIE_TIMEOUT<int(time.time())):
# 			state = 0
# 		else:
# 			ret['content'] = result.cookie
# 	except NoResultFound:
# 		result = CookieCache(cardnum=cardnum,cookie="",date=int(time.time()))
# 		state = 0
# 	except Exception,e:
# 		ret['code'] = 500
# 		ret['content'] = str(e)
# 	if state==0:
# 		res = authApi(cardnum,card_pwd)	
# 		if res['code']==200:
# 			cookie = res['content']
# 			ret['content'] = cookie
# 			result.cookie = cookie
# 			try:
# 				db.add(res)
# 				db.commit()
# 			except:
# 				db.rollback()
# 		else:
# 			ret['code'] = 500
# 	return ret


from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPClient
from tornado.httputil import url_concat
from sqlalchemy.orm.exc import NoResultFound
from tornado.web import MissingArgumentError
from ..models.cookie_cache import CookieCache
from ..auth.cookie import getCookie
import tornado.web
import tornado.gen
import urllib, json
import traceback

class YuyueHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        return self.application.db

    def on_finish(self):
        self.db.close()

    def get(self):
        self.write('Herald Web Service')

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum')
        password = self.get_argument('password')
	method_type = self.get_argument('method')
        retjson = {'code':200, 'content':''}
        try:
            if method_type not in method.keys():
                retjson['code'] = 500
                retjson['content'] = 'method is not allowed'
            else:
                cookie = getCookie(self.db,cardnum,password)
                if cookie['code'] == 200:
                    meth = method[method_type]
                    data = []
                    for i in meth['param']:

# Path: mod/models/cookie_cache.py
# class CookieCache(Base):
#     __tablename__ = 'cookie'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(256), nullable=False)
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/cookie.py
# def getCookie(db,cardnum,card_pwd):
# 	state = 1
# 	ret = {'code':200,'content':''}
# 	try:
# 		result = db.query(CookieCache).filter(CookieCache.cardnum==cardnum).one()
# 		if (result.date+COOKIE_TIMEOUT<int(time.time())):
# 			state = 0
# 		else:
# 			ret['content'] = result.cookie
# 	except NoResultFound:
# 		result = CookieCache(cardnum=cardnum,cookie="",date=int(time.time()))
# 		state = 0
# 	except Exception,e:
# 		ret['code'] = 500
# 		ret['content'] = str(e)
# 	if state==0:
# 		res = authApi(cardnum,card_pwd)	
# 		if res['code']==200:
# 			cookie = res['content']
# 			ret['content'] = cookie
# 			result.cookie = cookie
# 			try:
# 				db.add(res)
# 				db.commit()
# 			except:
# 				db.rollback()
# 		else:
# 			ret['code'] = 500
# 	return ret


# Path: mod/yuyue/handler.py
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPClient
from tornado.httputil import url_concat
from sqlalchemy.orm.exc import NoResultFound
from tornado.web import MissingArgumentError
from ..models.cookie_cache import CookieCache
from ..auth.cookie import getCookie
import tornado.web
import tornado.gen
import urllib, json
import traceback


class YuyueHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        return self.application.db

    def on_finish(self):
        self.db.close()

    def get(self):
        self.write('Herald Web Service')

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum')
        password = self.get_argument('password')
	method_type = self.get_argument('method')
        retjson = {'code':200, 'content':''}
        try:
            if method_type not in method.keys():
                retjson['code'] = 500
                retjson['content'] = 'method is not allowed'
            else:
                cookie = getCookie(self.db,cardnum,password)
                if cookie['code'] == 200:
                    meth = method[method_type]
                    data = []
                    for i in meth['param']:

                        if method_type=='new' and i=='useUserIds':