Exemple #1
0
def users_create(account=None):

    account_id = get_jwt_identity()

    account = Accounts.query.get(account_id)

    if not account:
        return abort(401, description="Account not found")

    users_fields = user_schema.load(request.json)

    user = Users.query.get(account_id)

    if not user:

        new_user = Users()
        new_user.username = users_fields["username"]
        new_user.fname = users_fields["fname"]
        new_user.lname = users_fields["lname"]
        new_user.account_active = users_fields["account_active"]

        account.users.append(new_user)

        db.session.add(new_user)
        db.session.commit()

        return jsonify(user_schema.dump(new_user))

    else:
        return abort(401, description='User Profile already exists')
Exemple #2
0
def auth_register():
    user_fields = user_schema.load(request.json)

    user = Users.query.filter_by(email=user_fields["email"]).first()

    if user:
        return abort(400, description="User already")

    user = Users()
    user.email = user_fields["email"]
    user.password = bcrypt.generate_password_hash(
        user_fields["password"]).decode("utf-8")

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

    return jsonify(user_schema.dump(user))
Exemple #3
0
def user_delete(username):

    account_id = get_jwt_identity()

    account = Accounts.query.get(account_id)

    if not account:
        return abort(401, description="Account not found")

    #Delete a User
    user = Users.query.filter_by(username=username,
                                 account_id=account.id).first()

    # users_fields = user_schema.load(request.json)

    if not user:
        return abort(400)
    db.session.delete(user)
    db.session.commit()

    return jsonify(user_schema.dump(user))
Exemple #4
0
def user_update(username):

    account_id = get_jwt_identity()

    account = Accounts.query.get(account_id)

    if not account:
        return abort(401, description="Account not found")
    #Update a user

    user = Users.query.filter_by(username=username, account_id=account.id)

    users_fields = user_schema.load(request.json)

    if user.count() != 1:
        return abort(401, description="Unauthorised to update this user")
    user.update(users_fields)

    db.session.commit()

    return jsonify(user_schema.dump(user[0]))
Exemple #5
0
def users_show(username):
    #Return a single user
    user = Users.query.filter_by(username=username).first()
    return jsonify(user_schema.dump(user))