# Path: mod/models/user_detail.py
# class UserDetail(Base):
#     __tablename__ = 'user_detail'
#     cardnum = Column(String(10), primary_key=True)
#     schoolnum = Column(String(10), nullable=True)
#     name = Column(String(50), nullable=False)
#     sex = Column(String(10), nullable=True)
#     nation = Column(String(50), nullable=True)
#     room = Column(String(50), nullable=True)
#     bed = Column(String(50), nullable=True)
#     last_update = Column(BIGINT, nullable=True)
# 
# Path: mod/models/room_cache.py
# class RoomCache(Base):
#     __tablename__ = 'room'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/handler.py
# def authApi(username,password):
#     data = {
#             'username':username,
#             'password':password
#         }
#     result = {'code':200,'content':''}
#     try:
#         client = HTTPClient()
#         request = HTTPRequest(
#             CHECK_URL,
#             method='POST',
#             body=urllib.urlencode(data),
#             validate_cert=False,
#             request_timeout=TIME_OUT)
#         response = client.fetch(request)
#         header = response.headers
#         if 'Ssocookie' in header.keys():
#             headertemp = json.loads(header['Ssocookie'])
#             cookie = headertemp[0]['cookieName']+"="+headertemp[0]['cookieValue']
#             cookie += ";"+header['Set-Cookie'].split(";")[0]
#             result['content'] = cookie
#         else:
#             result['code'] = 400
#     except HTTPError as e:
#         if e.code == 401:
#           result['code'] = 401
#         else:
#           result['code'] = 500
#     except Exception,e:
#         result['code'] = 500
#     return result


from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from sqlalchemy.orm.exc import NoResultFound
from ..models.user_detail import UserDetail
from ..models.room_cache import RoomCache
from time import time
from bs4 import BeautifulSoup
from time import time, localtime, strftime
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64
import urllib
import datetime
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-08-24 12:46:36
# @Author  : LiangJ


class RoomHandler(tornado.web.RequestHandler):

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

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

    def on_finish(self):
        self.db.close()
    
    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        number = self.get_argument('number',default=None)
        retjson = {'code':200, 'content':''}
        data = {
            'Login.Token1':number,
            'Login.Token2':self.get_argument('password'),
        }

        # read from cache
        try:

# Path: mod/models/user_detail.py
# class UserDetail(Base):
#     __tablename__ = 'user_detail'
#     cardnum = Column(String(10), primary_key=True)
#     schoolnum = Column(String(10), nullable=True)
#     name = Column(String(50), nullable=False)
#     sex = Column(String(10), nullable=True)
#     nation = Column(String(50), nullable=True)
#     room = Column(String(50), nullable=True)
#     bed = Column(String(50), nullable=True)
#     last_update = Column(BIGINT, nullable=True)
# 
# Path: mod/models/room_cache.py
# class RoomCache(Base):
#     __tablename__ = 'room'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/handler.py
# def authApi(username,password):
#     data = {
#             'username':username,
#             'password':password
#         }
#     result = {'code':200,'content':''}
#     try:
#         client = HTTPClient()
#         request = HTTPRequest(
#             CHECK_URL,
#             method='POST',
#             body=urllib.urlencode(data),
#             validate_cert=False,
#             request_timeout=TIME_OUT)
#         response = client.fetch(request)
#         header = response.headers
#         if 'Ssocookie' in header.keys():
#             headertemp = json.loads(header['Ssocookie'])
#             cookie = headertemp[0]['cookieName']+"="+headertemp[0]['cookieValue']
#             cookie += ";"+header['Set-Cookie'].split(";")[0]
#             result['content'] = cookie
#         else:
#             result['code'] = 400
#     except HTTPError as e:
#         if e.code == 401:
#           result['code'] = 401
#         else:
#           result['code'] = 500
#     except Exception,e:
#         result['code'] = 500
#     return result


# Path: mod/bedRoom/handler.py
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from sqlalchemy.orm.exc import NoResultFound
from ..models.user_detail import UserDetail
from ..models.room_cache import RoomCache
from time import time
from bs4 import BeautifulSoup
from time import time, localtime, strftime
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64
import urllib
import datetime

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-08-24 12:46:36
# @Author  : LiangJ


class RoomHandler(tornado.web.RequestHandler):

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

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

    def on_finish(self):
        self.db.close()
    
    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        number = self.get_argument('number',default=None)
        retjson = {'code':200, 'content':''}
        data = {
            'Login.Token1':number,
            'Login.Token2':self.get_argument('password'),
        }

        # read from cache
        try:

            status = self.db.query(RoomCache).filter(RoomCache.cardnum == number).one()