# Path: mod/_config.py

# 
# 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/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 userCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPClient
from ..models.user_detail import UserDetail
from bs4 import BeautifulSoup
from sqlalchemy.orm.exc import NoResultFound
from ..auth.handler import authApi
from sqlalchemy import func, or_, not_
import tornado.web
import tornado.gen
import urllib, json
import time
            else:
                # 兼容老版本不存在last_update字段
                # 更新缓存
                user.name, user.schoolnum, user.cardnum = self.get_schoolnum_name(_number)
                user.last_update = int(time.time())
                retjson['code'] = 200
                retjson['content'] = {
                        'cardnum': user.cardnum,
                        'schoolnum': user.schoolnum,
                        'name': user.name,
                        'sex': user.sex,
                        }
                self.db.add(user)
                self.db.commit()
            self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
            self.finish()
            return
        except NoResultFound as e:
            pass


        _user = self.get_schoolnum_name(_number)
        if (_user != "-1"):
            _name, _schoolnum, _cardnum = _user
            retjson['content']= {
                    'schoolnum': _schoolnum,
                    'cardnum': _cardnum,
                    'name': _name
                    }
            retjson['code'] =200

# Path: mod/_config.py

# 
# 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/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/user/handler.py
from .._config import userCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPClient
from ..models.user_detail import UserDetail
from bs4 import BeautifulSoup
from sqlalchemy.orm.exc import NoResultFound
from ..auth.handler import authApi
from sqlalchemy import func, or_, not_
import tornado.web
import tornado.gen
import urllib, json
import time

            else:
                # 兼容老版本不存在last_update字段
                # 更新缓存
                user.name, user.schoolnum, user.cardnum = self.get_schoolnum_name(_number)
                user.last_update = int(time.time())
                retjson['code'] = 200
                retjson['content'] = {
                        'cardnum': user.cardnum,
                        'schoolnum': user.schoolnum,
                        'name': user.name,
                        'sex': user.sex,
                        }
                self.db.add(user)
                self.db.commit()
            self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
            self.finish()
            return
        except NoResultFound as e:
            pass


        _user = self.get_schoolnum_name(_number)
        if (_user != "-1"):
            _name, _schoolnum, _cardnum = _user
            retjson['content']= {
                    'schoolnum': _schoolnum,
                    'cardnum': _cardnum,
                    'name': _name
                    }
            retjson['code'] =200

            user_detail = UserDetail(schoolnum = _schoolnum,  cardnum= _cardnum, name = _name, last_update = int(time.time()))