Example #1
0
def create_post():
    try:
        data = request.get_json()
    except:
        return {"error": "Bad Request"}, 400

    new_post = Post()

    # Validate data
    if 'tags' not in data:
        return {"error": "Tags are required"}, 400
    if 'post_url' not in data:
        return {"error": "URL is required"}, 400

    for tag in data['tags']:
        if tag not in Tag.valid_tags():
            return {"error": "Invalid Tag"}, 400

    if not re.match(regex, data['post_url']):
        return {"error": "URL is not valid"}, 400

    new_post.from_dict(data)

    user = User.query.filter_by(token=token_auth.current_user().token).first()
    new_post.user_id = user.id

    db.session.add(new_post)
    db.session.commit()
    increment_posts(user)

    return jsonify(data), 201
Example #2
0
def update_inventory(id):
    inventory = Inventory.query.get_or_404(id)
    data = request.get_json() or {}

    if data:
        inventory.from_dict(data, update_by=token_auth.current_user().email)
        db.session.add(inventory)
        db.session.commit()

    return inventory.to_dict()
Example #3
0
def get_all_inventory():
    page = request.args.get('page', 1, type=int)
    per_page = min(
        request.args.get('per_page',
                         current_app.config['PAGINATION_MIN'],
                         type=int), current_app.config['PAGINATION_MAX'])
    curr_user = token_auth.current_user()
    data = Inventory.to_collection_dict(Inventory.query, page, per_page,
                                        'inventory.get_all_inventory')

    return data
Example #4
0
def update_customer(id):
    customer = User.query.filter_by(id=id).first().customer
    if not customer:
        error_response(404, 'Customer does not exist')

    data = request.get_json() or {}
    if data:
        customer.from_dict(data, update_by=token_auth.current_user().email)
        db.session.commit()

    return customer.to_dict()
Example #5
0
def like_post():
    data = request.get_json() or {}
    try:
        post = int(data['post_id'])
    except:
        return {"error": "invalid data"}, 400

    user = User.query.filter_by(token=token_auth.current_user().token).first()
    liked_post = user.add_remove_like(post)

    db.session.commit()

    return jsonify(liked_post)
Example #6
0
def create_inventory():
    data = request.get_json() or {}

    #mand_fields = ('name')
    #if not all(field in data for field in mand_fields):
    if 'name' not in data:
        return bad_request('Please provide all mandatory fields')

    inventory = Inventory()
    inventory.from_dict(data, update_by=token_auth.current_user().email)
    db.session.add(inventory)
    db.session.commit()

    return inventory.to_dict()
Example #7
0
def delete_post():
    data = request.get_json() or {}
    try:
        post = Post.query.get(int(data['post_id']))
    except:
        return {"error": "invalid data"}, 400

    user = User.query.filter_by(token=token_auth.current_user().token).first()
    if not post:
        return {"error": "could not find post"}, 400
    if post.user_id != user.id:
        return {"error": "Invalid credentials"}, 401

    decrement_posts(user)
    db.session.delete(post)
    db.session.commit()

    return {"msg": "post deleted"}, 202
Example #8
0
def revoke_token():
    token_auth.current_user().revoke_token()
    db.session.commit()
    # 204 - successful and no body
    return '', 204