# 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
                    'name': _name
                    }
            retjson['code'] =200
            user_detail = UserDetail(schoolnum = _schoolnum,  cardnum= _cardnum, name = _name, last_update = int(time.time()))
            self.db.add(user_detail)
            self.db.commit();
        else:
            pass
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()
        return

    def get_schoolnum_name(self, number):
        try:
            CURR_URL = 'http://xk.urp.seu.edu.cn/jw_service/service/stuCurriculum.action'
            term = "16-17-1"
            params = urllib.urlencode({
                'queryStudentId': number,
                'queryAcademicYear': term})
            client = HTTPClient()
            request = HTTPRequest(
                    CURR_URL,
                    method='POST',
                    body=params,
                    request_timeout=TIME_OUT)
            response = client.fetch(request)
            body = response.body
            if not body:
                return "-1"
            else:

# 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

                    'name': _name
                    }
            retjson['code'] =200
            user_detail = UserDetail(schoolnum = _schoolnum,  cardnum= _cardnum, name = _name, last_update = int(time.time()))
            self.db.add(user_detail)
            self.db.commit();
        else:
            pass
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()
        return

    def get_schoolnum_name(self, number):
        try:
            CURR_URL = 'http://xk.urp.seu.edu.cn/jw_service/service/stuCurriculum.action'
            term = "16-17-1"
            params = urllib.urlencode({
                'queryStudentId': number,
                'queryAcademicYear': term})
            client = HTTPClient()
            request = HTTPRequest(
                    CURR_URL,
                    method='POST',
                    body=params,
                    request_timeout=TIME_OUT)
            response = client.fetch(request)
            body = response.body
            if not body:
                return "-1"
            else:

                soup = BeautifulSoup(body)