コード例 #1
0
def login(request_name, request_password=None):
    print("login -> " + request_name)
    item = find_user(request_name=request_name)
    if item is not None:
        password = item['password']
        token = item['token']
        if password == request_password:
            print("password is correct")
            token_obj = Token(item['id'], request_name)
            get_db().execute('update User set token=? where name=?',
                             (token_obj.get_token(), request_name))
            get_db().commit()
            print('User has login successfully ,and token has been refreshed')
            return find_user(request_name=request_name)
        elif request_password is None or request_password is '':
            print('request_password is None')
            if token is not None or token is not '':
                print("password is empty ,try to check token")
                token_obj = Token(token=token)
                if token_obj.verify_token():
                    print('User has passed token verify!')
                    return find_user(request_name=request_name)
                return 'User Token has expired! (' + request_name + ')'
        return 'Password is not correct && token is null!'
    else:
        return 'User has not registered yet, please register first'
    return 'Unknown error'
コード例 #2
0
def find_user(request_id=None, request_name=None):
    print('find_user')
    try:
        db = get_db()
        cur = None
        if request_id is not None:
            cur = db.execute('select * from User where id=?', (request_id, ))
        elif request_name is not None:
            cur = db.execute('select * from User where name=?',
                             (request_name, ))
    except sqlite3.OperationalError as e:
        print(e)
    else:
        if cur is not None:
            values = cur.fetchall()
            if len(values) == 1:
                print('Ok, find user .')
                json_result = json_utils.db_to_json_item(cur, values[0])
                json_result['server_time'] = int(time.time())
                return json_result
            else:
                print('Can not find User !')
        else:
            print('Can not find User!')
    return None
コード例 #3
0
def request_withdraw(request_user_id, value=0.0):
    print('request_withdraw')
    try:
        db = get_db()
        cur = db.execute('select * from Click where user_id=?',
                         (request_user_id, ))
        items = cur.fetchall()
        if len(items) == 1:
            print('Ok, find withdraw info ...')
            item = items[0]

            earn_amount = item[3]
            with_draw_time_left = item[4]
            print('earn_amount -> ' + str(earn_amount) +
                  ' with_draw_time_left -> ' + str(with_draw_time_left))

            if with_draw_time_left < 1:
                print('You have no with_draw time left.')
                return 'You have no with_draw time left.'

            # just set it to total temp
            if value <= 0 or value > earn_amount:
                value = earn_amount

            if with_draw_time_left > 0:
                print('request value is ' + str(value))
                db.execute(
                    'update Click set request_with_draw_amount=? where user_id=?',
                    (
                        value,
                        request_user_id,
                    ))
                db.execute(
                    'update Click set request_with_draw_time=? where user_id=?',
                    (
                        asctime(),
                        request_user_id,
                    ))
                db.execute(
                    'update Click set with_draw_times_left=? where user_id=?',
                    (
                        (with_draw_time_left - 1),
                        request_user_id,
                    ))
                db.commit()
                return get_earn(request_user_id)
            else:
                print('You have no withdraw time left.')
                return 'You have no withdraw time left.'
        else:
            print('You have not earned anything.')
    except sqlite3.OperationalError as e:
        print(e)
    return 'Error when upload_earn!'
コード例 #4
0
def add_log(request_user_id, operation, ip, location):
    print('add_log user_id -> ' + str(
        request_user_id) + ' operation -> ' + operation + ' ip -> ' + ip + ' location -> ' + location)
    try:
        db = get_db()
        db.execute('insert into Log(user_id,operation,time,ip,location) values (?,?,?,?,?)',
                   (
                       request_user_id, operation, time.asctime(), ip, location))
        db.commit()
        return None
    except sqlite3.OperationalError as e:
        print(e)
    return 'Error when add_log'
コード例 #5
0
def get_logs(request_user_id):
    print('get_logs -> ', request_user_id)
    try:
        db = get_db()
        cur = db.execute('select * from Log where user_id=? order by time desc', (request_user_id,))
        dict_array = json_utils.db_to_json_items(cur)
        for dict_item in dict_array:
            user = auth.find_user(request_id=request_user_id)
            if isinstance(user, dict):
                dict_item['user'] = user
        return dict_array
    except sqlite3.OperationalError as e:
        print(e)
    return 'Error when get_logs'
コード例 #6
0
def update_click(request_user_id,
                 request_earn_amount=-1,
                 request_with_draw_times_left=-1,
                 request_request_with_draw_amount=-1):
    print('update_click')
    try:
        db = get_db()
        if request_earn_amount >= 0:
            print('earn_amount updated')
            db.execute('update Click set earn_amount=? where user_id=?', (
                request_earn_amount,
                request_user_id,
            ))
        else:
            print('earn_amount unchanged')

        if request_with_draw_times_left >= 0:
            print('with_draw_times_left updated')
            db.execute(
                'update Click set with_draw_times_left=? where user_id=?', (
                    request_with_draw_times_left,
                    request_user_id,
                ))
        else:
            print('with_draw_times_left unchanged')

        if request_request_with_draw_amount >= 0:
            print('request_with_draw_amount updated')
            db.execute(
                'update Click set request_with_draw_amount=? where user_id=?',
                (
                    request_request_with_draw_amount,
                    request_user_id,
                ))
        else:
            print('request_with_draw_amount unchanged')

        db.execute('update Click set manager_deal_time=? where user_id=?', (
            asctime(),
            request_user_id,
        ))
        db.commit()
    except sqlite3.OperationalError as e:
        print(e)
        return 'db error !'
    return get_earn(request_user_id)
コード例 #7
0
def find_users(request_user_id):
    print('find_users user_id -> ' + request_user_id)
    if check_admin(request_user_id=request_user_id):
        try:
            db = get_db()
            cursor = db.execute('select * from User')
            row_headers = [key[0] for key in cursor.description]
            json_data = []
            items = cursor.fetchall()
            for item in items:
                item_dict = dict(zip(row_headers, item))
                item_dict['server_time'] = int(time.time())
                json_data.append(item_dict)
            return json_data
        except sqlite3.OperationalError as e:
            print(e)
    else:
        return 'Permission denied !'
    return None
コード例 #8
0
def register(request_name, request_password, request_im_qq, request_alipay,
             request_alipay_name):
    if find_user(request_name=request_name) is not None:
        return 'User name has been registered!'
    db = get_db()
    is_admin = 0
    if request_name == 'super_admin':
        is_admin = 1
    db.execute(
        'insert into User(name,password,time,im_qq,alipay,alipay_name,is_admin) values (?,?,?,?,?,?,?)',
        (request_name, request_password, time.asctime(), request_im_qq,
         request_alipay, request_alipay_name, is_admin))

    # init the click table
    db.execute('insert into Click(time,user_id) values (?,?)',
               (time.asctime(), find_user_id(request_name=request_name)))
    print('User ? has been inserted into db successfully', request_name)
    db.commit()
    return find_user(request_name=request_name)
コード例 #9
0
def get_earn(request_user_id, to_dict=True):
    print('get_earn -> ', request_user_id)
    try:
        db = get_db()
        cur = None
        if request_user_id is not None:
            cur = db.execute('select * from Click where user_id=?',
                             (request_user_id, ))
    except sqlite3.OperationalError as e:
        print(e)
    else:
        if cur is not None:
            values = cur.fetchall()
            if len(values) == 1:
                if to_dict:
                    return json_utils.db_to_json_item(cur, values[0])
                else:
                    return values[0]
            else:
                print('values is not 1 ??')
    return None
コード例 #10
0
def check_youmeng(request_id, baidu, google, sougou, taobao):
    print('check_youmeng')
    if baidu is not None and baidu == 'baidu':
        pass
    else:
        return 'Baidu not passed.'
    if google is not None and google == 'google':
        pass
    else:
        return 'Google not passed.'
    if sougou is not None and sougou == 'sougou':
        pass
    else:
        return 'Sougou not passed.'
    if taobao is not None and taobao == 'taobao':
        pass
    else:
        return 'Taobao not passed.'
    db = get_db()
    db.execute('update User set youmeng_checked=? where id=?', (1, request_id))
    db.commit()
    return find_user(request_id=request_id)
コード例 #11
0
def upload_earn(request_user_id, request_value):
    print('upload_earn -> ' + request_user_id + ' request_value -> ' +
          str(request_value))
    try:
        db = get_db()

        cur = db.execute('select * from Click where user_id=?',
                         (request_user_id, ))
        value = cur.fetchall()
        if len(value) == 1:
            current_earn_amount = value[0][3]
            print('Current earn_amount is ' + str(current_earn_amount))
            db.execute('update Click set earn_amount=? where user_id=?', (
                float(request_value + current_earn_amount),
                request_user_id,
            ))
            db.commit()
        else:
            print('Can not find user !')
            return None
    except sqlite3.OperationalError as e:
        print(e)
        return None
    return get_earn(request_user_id)
コード例 #12
0
def logout(request_id):
    print('logout')
    get_db().execute('update User set token=? where id=?', (None, request_id))
    get_db().commit()
    print('User ? has logout successfully!', request_id)
    return find_user(request_id=request_id)
コード例 #13
0
def update_profile(request_id,
                   request_password=None,
                   request_qq=None,
                   request_phone=None,
                   request_alipay=None,
                   request_alipay_name=None,
                   request_prime_level=-1,
                   request_prime_period=-1,
                   request_youmeng_checked=-1):
    print('update_profile')
    db = get_db()
    if request_password is not None and request_password != '':
        print('request_password updated')
        db.execute('update User set password=? where id=?',
                   (request_password, request_id))
    else:
        print('request_password unchanged.')
    if request_qq is not None and request_qq != '':
        print('request_qq updated')
        db.execute('update User set im_qq=? where id=?',
                   (request_qq, request_id))
    else:
        print('request_qq unchanged.')
    if request_phone is not None and request_phone != '':
        print('request_phone updated')
        db.execute('update User set telephone=? where id=?',
                   (request_phone, request_id))
    else:
        print('request_phone unchanged.')

    if request_alipay is not None and request_alipay != '':
        print('request_alipay updated')
        db.execute('update User set alipay=? where id=?',
                   (request_alipay, request_id))
    else:
        print('request_phone unchanged.')
    if request_alipay_name is not None and request_alipay_name != '':
        print('request_alipay_name updated')
        db.execute('update User set alipay_name=? where id=?',
                   (request_alipay_name, request_id))
    else:
        print('request_alipay_name unchanged.')
    if request_prime_level >= 0:
        print('request_prime_level updated')
        db.execute('update User set prime_level=? where id=?',
                   (request_prime_level, request_id))
        db.execute('update User set prime_open_time=? where id=?',
                   (int(time.time()), request_id))  # seconds
        print('prime_update_time is -> ' + str(int(time.time())))
        if request_prime_level == 1:
            if request_prime_period >= 0:
                request_prime_time_end = int(
                    (time.time() + request_prime_period))
                print('prime_time_end is -> ' + str(request_prime_time_end))
                db.execute('update User set prime_end_time=? where id=?',
                           (request_prime_time_end, request_id))  # seconds
            else:
                print('User should setup trial time for PrimeTrial!')
    else:
        print('request_prime_level unchanged.')

    if request_youmeng_checked >= 0:
        print('request_youmeng_checked updated')
        db.execute('update User set youmeng_checked=? where id=?',
                   (request_youmeng_checked, request_id))
    else:
        print('request_youmeng_checked unchanged.')

    db.commit()
    return find_user(request_id=request_id)