示例#1
0
文件: products.py 项目: Erowlin/Ubid
def add_product():
    resp = helpers.get_response(request)
    allowed_fields = ['title', 'description', 'dateStart', 'dateLength', 'startPrice', 'buyoutPrice', 'reservePrice', 'imgUrl']
    mandatory_fields = ['title', 'description', 'dateStart', 'dateLength', 'startPrice']
    user = helpers.get_by(glob.users, resp['user_id'])
    association_field = [{'association_name': 'user_id', 'association_value': user['id']}]
    product = helpers.new_object(glob.products, resp, products_path, allowed_fields, mandatory_fields, associations=association_field)
    return show(product['id'])
示例#2
0
文件: products.py 项目: Erowlin/Ubid
def destroy(product_id):
    resp = helpers.get_response(request) 
    product = helpers.get_by(glob.products, product_id)
    if product is None:
        return 'No such product', 400
    users.has_right_abort(resp, product['user_id'])
    helpers.delete_object(glob.products, product_id, products_path)
    return 'Delete ok', 200
示例#3
0
文件: products.py 项目: Erowlin/Ubid
def update(product_id):
    resp = helpers.get_response(request) 
    product = helpers.get_by(glob.products, product_id)
    users.has_right_abort(resp, product['user_id'])
    resp = helpers.get_response(request)
    exclude_field = ['reservePrice', 'startPrice', 'id', 'user_id', 'user']
    null_fields = ['title', 'description', 'buyoutPrice', 'dateLength', 'dateStart']
    product = helpers.update_object(glob.products, product_id, resp, glob.products_path, null_fields=null_fields, exclude_fields=exclude_field)
    return show(product_id)
示例#4
0
文件: products.py 项目: Erowlin/Ubid
def show(product_id):
            product = helpers.get_by(glob.products, product_id)
            if product is None :
                return 'No such product', 400
            p = copy.deepcopy(product)
            iduser = p['user_id']
            user = users.get_user_by_id(int(iduser))
            p['user'] = user
            return jsonify( { 'product' : p } )
示例#5
0
文件: users.py 项目: Erowlin/Ubid
def has_right(resp, user_id):
    if  not resp or not 'user_id' in resp or not 'token' in resp:
        return 0
    token = resp['token']
    user = helpers.get_by(glob.users, user_id)
    if int(resp['user_id']) != int(user_id) or resp['token'] != user['token']:
        return False
    else:
        return True
示例#6
0
文件: users.py 项目: Erowlin/Ubid
def get_user_by_id(user_id, token=None):
    user = helpers.get_by(glob.users, user_id)
    if user is None:
        abort(make_response("User not found",400))
    u = copy.deepcopy(user)
    del u['password']
    if  user_id != u['id'] or 'token' is None or u['token'] != token: # Public profil
        del u['address1']
        del u['address2']
        del u['email']
        del u['firstname']
        del u['lastname']
        del u['token']
    return u
示例#7
0
文件: users.py 项目: Erowlin/Ubid
def show(user_id):
    resp = helpers.get_response(request)
    user = helpers.get_by(glob.users, user_id)
    if user is None:
        abort(make_response("User not found",400))
    u = copy.deepcopy(user)        
    del u['password']
    if not 'user_id' in resp or int(resp['user_id']) != user_id or not 'token' in resp or u['token'] != resp['token']: # Public profil
        del u['address1']
        del u['address2']
        del u['email']
        del u['firstname']
        del u['lastname']
        del u['token']
    if (len(u) != 0):
        return jsonify({'user': u})
    return 'User Not found', 401
示例#8
0
文件: users.py 项目: Erowlin/Ubid
def logout():
    resp = helpers.get_response(request)
    has_right_abort(resp, resp['user_id'])
    user = helpers.get_by(glob.users, resp['user_id'])
    destroy_token(user)
    return 'Logout successful', 201
示例#9
0
文件: users.py 项目: Erowlin/Ubid
def generate_token(user):
    user = helpers.get_by(glob.users, user['id'])
    user['token'] = base64.b64encode(str(user['id']) + str(os.urandom(5)) + str(glob.secret_key))
    myjson.save_json(glob.users, users_path)