Пример #1
0
def delete_user(username):
    user_to_be_deleted = User.query.filter_by(username=username).first()
    ts_of_that_user = Timesheet.query.filter_by(user_id=user_to_be_deleted.id).all()

    if not user_to_be_deleted:
        return jsonify({'message': 'user does not exist'})

    if len(ts_of_that_user) > 0:
        db.session.delete(ts_of_that_user)

    db.session.delete(user_to_be_deleted)
    db.session.commit()
    return user_schema.jsonify(user_to_be_deleted)
Пример #2
0
def create_user():
    username = request.json['username']
    password = request.json['password']
    first_name = request.json['first_name']
    last_name = request.json['last_name']
    email = request.json['email']
    is_approved = request.json['is_approved']
    is_admin = request.json['is_admin']
    confirmed = request.json['confirmed']
    timesheets = request.json['timesheets']

    hashed_password = hash_password(password)

    new_user = User(username=username, timesheets=timesheets, first_name=first_name, last_name=last_name, email=email,
                    is_admin=is_admin, is_approved=is_approved, password=hashed_password, confirmed=confirmed)
    db.session.add(new_user)
    db.session.commit()
    return user_schema.jsonify(new_user)
Пример #3
0
def patch_anything(username):
    user = User.query.filter_by(username=username).first()
    if user:
        for key in request.json:
            if key == 'is_admin':
                user.is_admin = request.json[key]
            elif key == 'is_approved':
                user.is_approved = request.json[key]
            elif key == 'confirmed':
                user.confirmed = request.json[key]
            elif key == 'username':
                user.username = request.json[key]
            elif key == 'first_name':
                user.first_name = request.json[key]
            elif key == 'last_name':
                user.last_name = request.json[key]
            elif key == 'email':
                user.email = request.json[key]
            elif key == 'timesheets':
                user.timesheets = request.json[key]
        db.session.commit()
        return user_schema.jsonify(user)
    else:
        return jsonify("custom_message_error", "user doesn't exist with that given username")
Пример #4
0
def get_user_by_id(uid):
    user = User.query.filter_by(id=uid).first()
    if not user:
        return jsonify({'custom_error': 'user does not exist'})
    return user_schema.jsonify(user)