# Path: util/login.py
# def login(userid):
#     # 1. sha1(salt+ip+userid+now)
#     now=str(datetime.now())
#     userid=str(userid)
#     hash=sha1(salt+userid+now)
#     # 2. 储存hash id,userid,hash
#     db.insert("login",userid=userid,hash=hash,time=now,discard=0)
#     # 3. write cookie ua:sha1
#     web.setcookie('cu', hash, 3600*24*30)
#     return hash
# 
# Path: util/oauth.py
# class weibo(oauthbase):
# class douban(oauthbase):
#     def get_current_user_info(self,access_token):
#     def post(self,content):
#     def get_current_user_info(self,access_token):
#     def post(self,content):
# def createClientWithName(name,user=None):
# 
# Path: model/user.py
# def check(name,info):
# def get_by_id(id):
# def fav_pages(user_id=None,show_private=False,per=5):
# def favs_of_page(page=1,per=5,user_id=0,show_private=False):
# def parse_data(name,info):
# def new_oauth_user(name,info):
# def update_oauth_userid(name,id,oauth_id):
# def update_access_token(name,id,token):
# def exist_oauth_user(name,info):
# def login_oauth_user(name,info):
# 
# Path: config/setting.py



import web
from util import login,oauth
from model import user
from config.setting import blankrender
# /auth/(douban|weibo)

class auth:
    def GET(self,name):
        input = web.input()
        new_user = None
        cur_user = login.logged()
        try:
            client = oauth.createClientWithName(name)
            access_token = client.get_access_token(input["code"])
            user_info = client.get_current_user_info(access_token)

            user_info["access_token"] = access_token
            


            if cur_user:
                user.update_oauth_userid(name,cur_user["id"],user_info["id"])
                user.update_access_token(name,user_info["id"],access_token)
            if not cur_user:
                oauth_user = user.exist_oauth_user(name,user_info)
                if not oauth_user:

# Path: util/login.py
# def login(userid):
#     # 1. sha1(salt+ip+userid+now)
#     now=str(datetime.now())
#     userid=str(userid)
#     hash=sha1(salt+userid+now)
#     # 2. 储存hash id,userid,hash
#     db.insert("login",userid=userid,hash=hash,time=now,discard=0)
#     # 3. write cookie ua:sha1
#     web.setcookie('cu', hash, 3600*24*30)
#     return hash
# 
# Path: util/oauth.py
# class weibo(oauthbase):
# class douban(oauthbase):
#     def get_current_user_info(self,access_token):
#     def post(self,content):
#     def get_current_user_info(self,access_token):
#     def post(self,content):
# def createClientWithName(name,user=None):
# 
# Path: model/user.py
# def check(name,info):
# def get_by_id(id):
# def fav_pages(user_id=None,show_private=False,per=5):
# def favs_of_page(page=1,per=5,user_id=0,show_private=False):
# def parse_data(name,info):
# def new_oauth_user(name,info):
# def update_oauth_userid(name,id,oauth_id):
# def update_access_token(name,id,token):
# def exist_oauth_user(name,info):
# def login_oauth_user(name,info):
# 
# Path: config/setting.py



# Path: controller/page/auth.py
import web
from util import login,oauth
from model import user
from config.setting import blankrender

# /auth/(douban|weibo)

class auth:
    def GET(self,name):
        input = web.input()
        new_user = None
        cur_user = login.logged()
        try:
            client = oauth.createClientWithName(name)
            access_token = client.get_access_token(input["code"])
            user_info = client.get_current_user_info(access_token)

            user_info["access_token"] = access_token
            


            if cur_user:
                user.update_oauth_userid(name,cur_user["id"],user_info["id"])
                user.update_access_token(name,user_info["id"],access_token)
            if not cur_user:
                oauth_user = user.exist_oauth_user(name,user_info)
                if not oauth_user:

                    new_user = user.new_oauth_user(name,user_info)