Esempio n. 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')
Esempio n. 2
0
def profile_create(user):

    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Do I know you?")

    profile_fields = profile_schema.load(request.json)
    is_profile = Profile.query.get(user.id)

    if not is_profile:

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

        user.profile_id.append(
            new_profile
        )  # user = defined above; client_id is linked as relationship to user: client_id = db.relationship("Client", backref=backref("users", uselist=False)) in users table

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

        #return jsonify(client_schema.dump(new_profile))
        return render_template("Profile.html", new_profile=new_profile)

    else:
        return abort(401, description='Profile already exists')
Esempio n. 3
0
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
Esempio n. 4
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
Esempio n. 5
0
def profile_details_update():
    user_id = get_jwt_identity()  # Update a journal entry
    user = User.query.get(user_id)
    if not user:
        return abort(401, description="Invalid profile")

    details = Profile.query.filter_by(id=user.id)
    detail_fields = profile_schema.load(request.json)
    details.update(detail_fields)
    db.session.commit()
    return jsonify(detail_fields)
Esempio n. 6
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]))
Esempio n. 8
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))
Esempio n. 9
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]))
Esempio n. 10
0
def update_profile(id):
    user = load_user(current_user.get_id())
    profile = Profile.query.filter_by(profile_id=id, user_id=user.user_id)

    if profile.count() != 1:
        flash("Can't find profile")
        return redirect(url_for("web_profiles.show_profiles"))

    form = UpdateProfile(obj=profile.first())
    if form.validate_on_submit():
        data = {
            "name": form.name.data,
            "restrictions": form.restriction.data
        }
        fields = profile_schema.load(data, partial=True)
        profile.update(fields)
        db.session.commit()
        flash("Profile updated!")
        return redirect(url_for("web_profiles.show_profiles"))

    return render_template("update_profile.html", form=form, id=id)
Esempio n. 11
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))