Beispiel #1
0
def profile_create(user=None):

    user_id = get_jwt_identity()

    profile_fields = profile_schema.load(request.json)

    profile = Profile.query.get(user_id)

    if not profile:

        new_profile = Profile()
        new_profile.username = profile_fields["username"]
        new_profile.fname = profile_fields["fname"]
        new_profile.lname = profile_fields["lname"]
        new_profile.account_active = profile_fields["account_active"]
        new_profile.admin = profile_fields["admin"]

        user.profile.append(new_profile)

        db.session.add(new_profile)
        db.session.commit()

        return jsonify(profile_schema.dump(new_profile))

    else:
        return abort(401, description='User Profile already exists')
def profile_create(user):  # This function will run when the route is matched

    if user.profile != []:  # If the user already has a profile
        return abort(400, description="User already has profile"
                     )  # Return the error "Email already in use"

    profile_fields = profile_schema.load(
        request.json)  # Retrieving the fields from the request
    profile = Profile.query.filter_by(
        username=profile_fields["username"]).first(
        )  # Query the user table with the email and return the first user

    if profile:  # If a user is returned
        return abort(400, description="username already in use"
                     )  # Return the error "Email already in use"

    new_profile = Profile(
    )  # Create a new profile object from the Profile model
    new_profile.username = profile_fields[
        "username"]  # Add username to the new_profile
    new_profile.firstname = profile_fields[
        "firstname"]  # Add username to the new_profile
    new_profile.lastname = profile_fields[
        "lastname"]  # Add username to the new_profile
    new_profile.user_id = user.id  # Add username to the new_profile

    user.profile.append(new_profile)  # Add profile to the user
    db.session.commit()  # Commit the DB session

    return jsonify(
        profile_schema.dump(new_profile))  # Return the newly created profile
Beispiel #3
0
def delete_profile(id):
    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Invalid user")

    profile = Profile.query.filter_by(profile_id=id,
                                      user_id=user.user_id).first()

    if not profile:
        return abort(404, description="Profile not found")

    while len(profile.unrecommend) > 0:
        for item in profile.unrecommend:
            profile.unrecommend.remove(item)
        db.session.commit()

    groups = GroupMembers.query.filter_by(profile_id=profile.profile_id)
    for group in groups:
        db.session.delete(group)
        db.session.commit()

    db.session.delete(profile)
    db.session.commit()

    return jsonify(profile_schema.dump(profile))
Beispiel #4
0
def profile_delete(user, id):
    profile = Profile.query.filter_by(id=id, user_id=user.id).first()  # Query the user table with the id and the user id then return the first user
    # print(profile[0].__dict__)
    # return("bills")
    if not profile:                                                    # If there is any number other than 1
        return abort(400, description="Unauthorized to update this profile") # Return this error
    
    db.session.delete(profile)
    db.session.commit()                                                # Commit the session to the db
    return jsonify(profile_schema.dump(profile))                       # Return the deleted profile
Beispiel #5
0
def profile_update(user, id):                             
    
    profile_fields = profile_schema.load(request.json)                 # Retrieving the fields from the request
    profile = Profile.query.filter_by(id=id, user_id=user.id)          # Query the user table with the id and the user id then return the first user
    if not profile:                                                    # If there is no profile found
        return abort(401, description="Unauthorized to update this profile")  # Return this error

    profile.update(profile_fields)                                     # Update the fields with the data from the request
    db.session.commit()                                                # Commit the session to the db
    return jsonify(profile_schema.dump(profile[0]))                    # Return the recently committed profile
Beispiel #6
0
def profile_delete(user, id):

    profile = Profile.query.filter_by(profileid=id, user_id=user.id).first()

    if not profile:
        return abort(400, description="Unauthorised to delete user")
    db.session.delete(profile)
    db.session.commit()

    return jsonify(profile_schema.dump(profile))
Beispiel #7
0
def profile_update(user, id):

    profile_fields = profile_schema.load(request.json)
    profile = Profile.query.filter_by(id=id, user_id=user.id)
    if not profile:
        return abort(401, description="Unauthorized to update this profile")

    print(profile.__dict__)
    profile.update(profile_fields)
    db.session.commit()
    return jsonify(profile_schema.dump(profile[0]))
def profiles_update(id):
    #Update a user
    profile = Profiles.query.filter_by(profileid=id, user_id=current_user.id)
    profile_fields = profile_schema.load(request.json)
    print(profile)

    if not profile:
        return abort(401, description="Unauthorised to update")
    profile.update(profile_fields)

    db.session.commit()

    return jsonify(profile_schema.dump(profile[0]))
Beispiel #9
0
def get_profile_by_id(id):
    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Invalid user")

    profile = Profile.query.filter_by(profile_id=id, user_id=user.user_id)

    if profile.count() != 1:
        return abort(404, description="Profile not found")

    return jsonify(profile_schema.dump(profile[0]))
Beispiel #10
0
def create_profile():
    profile_fields = profile_schema.load(request.json)
    user_id = get_jwt_identity()

    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Invalid user")

    new_profile = Profile()
    new_profile.name = profile_fields["name"]
    new_profile.restrictions = profile_fields["restrictions"]

    user.profiles.append(new_profile)
    db.session.commit()

    return jsonify(profile_schema.dump(new_profile))
Beispiel #11
0
def update_profile(id):
    profile_fields = profile_schema.load(request.json, partial=True)
    user_id = get_jwt_identity()

    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Invalid user")

    profile = Profile.query.filter_by(profile_id=id, user_id=user.user_id)

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

    profile.update(profile_fields)
    db.session.commit()

    return jsonify(profile_schema.dump(profile[0]))
Beispiel #12
0
def profile_create(user):

    if user.profile != []:
        return abort(400, description="User already has profile")

    profile_fields = profile_schema.load(request.json)
    profile = Profile.query.filter_by(
        username=profile_fields["username"]).first()

    if profile:
        return abort(400, description="username already in use")

    new_profile = Profile()
    new_profile.username = profile_fields["username"]
    new_profile.firstname = profile_fields["firstname"]
    new_profile.lastname = profile_fields["lastname"]
    new_profile.user_id = user.id

    user.profile.append(new_profile)
    db.session.commit()

    return jsonify(profile_schema.dump(new_profile))
Beispiel #13
0
def profile_show(id):                                                  # Auth service to make sure the correct user owns this profile
    profile = Profile.query.get(id)                                    # Query the user table with the id then return that user
    return jsonify(profile_schema.dump(profile))                       # Returb the profile in JSON
Beispiel #14
0
def profile_show(username):
    #Return a single user
    profile = Profile.query.filter_by(username=username).first()
    return jsonify(profile_schema.dump(profile))
Beispiel #15
0
def profile_show(id):
    profile = Profile.query.get(id)
    return jsonify(profile_schema.dump(profile))