# 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
# -*- coding: utf-8 -*-
# modified from old renewhandler.py 
# by yml_bright@163.com


class LibRenewHandler(tornado.web.RequestHandler):

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

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

    def get(self):
        self.write("herald webservice")

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum')
        # captcha = self.get_argument('captcha')
        barcode = self.get_argument('barcode')
        retjson = {'code': 200, 'content': u''}
        if (not cardnum or not barcode):
            retjson['code'] = 400
            retjson['content'] = u'parameter lack'
        else:
            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/renewhandler.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

# -*- coding: utf-8 -*-
# modified from old renewhandler.py 
# by yml_bright@163.com


class LibRenewHandler(tornado.web.RequestHandler):

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

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

    def get(self):
        self.write("herald webservice")

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        cardnum = self.get_argument('cardnum')
        # captcha = self.get_argument('captcha')
        barcode = self.get_argument('barcode')
        retjson = {'code': 200, 'content': u''}
        if (not cardnum or not barcode):
            retjson['code'] = 400
            retjson['content'] = u'parameter lack'
        else:
            try:

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