# Path: mod/models/library_auth_cache.py
# class LibraryAuthCache(Base):
#     __tablename__ = 'library_auth'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(4096))
#     captcha = Column(String(4096))
#     date = Column(Integer, nullable=False)


from config import *
from tornado.httpclient import HTTPClient
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from bs4 import BeautifulSoup
from ..models.library_auth_cache import LibraryAuthCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
import re
import json
import urllib
import base64
import tornado.web, tornado.gen
            cookie = response.headers['Set-Cookie'].split(';')[0]
            try:
                status = self.db.query(LibraryAuthCache).filter(LibraryAuthCache.cardnum == cardnum).one()
                status.cookie = cookie
                print status.cookie
            except NoResultFound:
                status = LibraryAuthCache(cardnum=cardnum, cookie=cookie, captcha='*', date=int(time()))
            self.db.add(status)
            try:
                self.db.commit()
            except Exception:
                self.db.rollback()
            self.set_header('Content-type', 'image/png')
            self.write(response.body)   # return captcha to client
        except Exception as e:
            self.write('error')

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum', default=None)
        password = self.get_argument('password', default=None)
        captcha = self.get_argument('captcha', default=None)
        retjson = {'code': 200, 'content': ''}
        status = None
        if not (cardnum and password and captcha):
            retjson['code'] = 400
            retjson['content'] = 'parameters lack'
        else:
            # 直接从数据库读取cookie，不做try的尝试

# Path: mod/models/library_auth_cache.py
# class LibraryAuthCache(Base):
#     __tablename__ = 'library_auth'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(4096))
#     captcha = Column(String(4096))
#     date = Column(Integer, nullable=False)


# Path: mod/library/library_auth.py
from config import *
from tornado.httpclient import HTTPClient
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from bs4 import BeautifulSoup
from ..models.library_auth_cache import LibraryAuthCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
import re
import json
import urllib
import base64
import tornado.web, tornado.gen

            cookie = response.headers['Set-Cookie'].split(';')[0]
            try:
                status = self.db.query(LibraryAuthCache).filter(LibraryAuthCache.cardnum == cardnum).one()
                status.cookie = cookie
                print status.cookie
            except NoResultFound:
                status = LibraryAuthCache(cardnum=cardnum, cookie=cookie, captcha='*', date=int(time()))
            self.db.add(status)
            try:
                self.db.commit()
            except Exception:
                self.db.rollback()
            self.set_header('Content-type', 'image/png')
            self.write(response.body)   # return captcha to client
        except Exception as e:
            self.write('error')

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum', default=None)
        password = self.get_argument('password', default=None)
        captcha = self.get_argument('captcha', default=None)
        retjson = {'code': 200, 'content': ''}
        status = None
        if not (cardnum and password and captcha):
            retjson['code'] = 400
            retjson['content'] = 'parameters lack'
        else:
            # 直接从数据库读取cookie，不做try的尝试

            status = self.db.query(LibraryAuthCache).filter(LibraryAuthCache.cardnum == cardnum).one()