def login(request):
    if Control.check_session(request):
        return PageResponse.jump_to_index()
    if (request.method == "POST"):
        try:
            assert request.POST.__contains__("username")
            assert request.POST.__contains__("password")
            return PageResponse.login_page(request, request.POST["username"],
                                           request.POST["password"])
        except:
            return PageResponse.login_page(request, "", "")
    return PageResponse.login_page(request, "", "")
def check_login(request):
    if Control.check_session(request):
        return PageResponse.jump_to_index()
    if (request.method == "POST"):
        try:
            from Download9.getmd5 import getmd5
            pwd = getmd5(request.POST["password"])
            x = Database.find_single("account",
                                     {"username": request.POST["username"]})
            assert x[Database.id("account", "password")] == pwd
            request.session["MemberName"] = request.POST["username"]
            request.session.set_expiry(Const.LoginTime)
            return PageResponse.login_success(request)
        except:
            return PageResponse.login_failed(request, request.POST["username"],
                                             request.POST["password"])
    else:
        return PageResponse.jump_to_not_exist()
def account9_login(request):
    if Control.check_session(request):
        return PageResponse.jump_to_index()
    if (request.method == "GET"):
        '''
        printf('')
        printf('a9test_begin')
        '''
        assert request.GET.__contains__("code")
        '''printf('a9test_start_get_info')'''
        ret = getinfo(request.GET["code"])
        '''printf('a9test_end_get_info')'''
        try:
            '''printf('a9test_try_read_old_account')'''
            Database.find_single(
                "account", {
                    "username": ret["user"]["name"],
                    "password": ret["user"]["password"]
                })
            '''printf('a9test_start_setting_session')'''
            request.session["MemberName"] = ret["user"]["name"]
            request.session.set_expiry(Const.LoginTime)
            '''printf('a9test_auth_over')'''
            return PageResponse.login_success(request)
        except:
            '''printf('a9test_try_inser_new_account')'''
            Database.insert(
                "account", {
                    "username": ret["user"]["name"],
                    "password": ret["user"]["password"],
                    "memoryused": 0
                })
            '''printf('a9test_start_setting_session')'''
            request.session["MemberName"] = ret["user"]["name"]
            request.session.set_expiry(Const.LoginTime)
            '''printf('a9test_auth_over')'''
            return PageResponse.login_success(request)
    else:
        '''printf('a9test_end:get_session failed')'''
        return PageResponse.jump_to_not_exist()
def init(request):
    from Download9 import init_database
    return PageResponse.jump_to_index()
def toindex(request):
    return PageResponse.jump_to_index()