Ejemplo n.º 1
0
def add_feedback(target_username):
    """ add feedbacks """

    if isCurrentUserAuthorized(target_username):

        form = AddFeedbackForm()

        if form.validate_on_submit():
            # grab data from the forms
            title = form.title.data
            content = form.content.data

            # created new feedback instance for targeted user and add to db
            new_feedback = Feedback.create_feedback(title, content,
                                                    target_username)
            db.session.add(new_feedback)
            db.session.commit()

            flash('Feedback was added!')

            return redirect(f'/users/{target_username}')

        else:
            return render_template("add_feedback.html",
                                   form=form,
                                   username=target_username)

    raise Unauthorized()
Ejemplo n.º 2
0
def add_feedback(username):
    """Add feedback."""

    if "user_id" not in session or session["user_id"] != username:
        raise Unauthorized()

    else:
        form = AddFeedbackForm()

        if form.validate_on_submit():
            title = form.title.data
            content = form.content.data

            feedback = Feedback(title=title,
                                content=content,
                                username=username)

            db.session.add(feedback)
            db.session.commit()

        else:
            return render_template("add-feedback.html",
                                   form=form,
                                   username=username)

    return redirect(f"/users/{username}")
Ejemplo n.º 3
0
def add_feedback(username):
    """ On GET: If <username> is in the session, shows add feedback form"""
    
    form = AddFeedbackForm()

    session_user = session.get("username", None)
    
    user = User.validate(session_user, username)
    
    if user:    
        
        if form.validate_on_submit():
            data = {k: v for k, v in form.data.items() if k != "csrf_token"}
            
            data["username"] = username

            new_feedback = Feedback(**data) 

            db.session.add(new_feedback)
            db.session.commit()
        
            return redirect(f"/users/{username}")

        else:
            return render_template("feedback.html",
                                    form=form)

    flash("Invalid user!")
    return redirect(f"/")
Ejemplo n.º 4
0
def add_feedback(username):
    form = AddFeedbackForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        try:
            if session['user_id'] == username:
                fb = Feedback(title=title, content=content, username=username)
                db.session.add(fb)
                db.session.commit()
                return redirect(f'/users/{username}')
            else:
                flash(f"Stop trying to add content under {username}!")
                redirect(f"/users/{session['user_id']}")
        except KeyError:
            flash("Please login or register first ;(")
            return redirect('/register')
    else:
        try:
            if session['user_id'] == username:
                user = User.query.filter_by(username=username).first()
            else:
                flash(f"Stop trying to add content under {username}!")
                redirect(f"/users/{session['user_id']}")
        except KeyError:
            flash("Please login or register first ;(")
            return redirect('/register')
        return render_template('add_feedback.html', user=user, form=form)
Ejemplo n.º 5
0
def update_feedback(feedback_id):

    feedback = Feedback.query.get(feedback_id)

    if "username" not in session or feedback.username != session['username']:
        raise Unauthorized()

    form = AddFeedbackForm(obj=feedback)

    if form.validate_on_submit():
        feedback.title = form.title.data
        feedback.content = form.content.data

        db.session.commit()
        return redirect(f'/users/{feedback.username}')
    return render_template('/feedback/edit.html', form=form, feedback=feedback)
Ejemplo n.º 6
0
def edit_feedback(feedback_id):
    feedback = Feedback.query.get_or_404(feedback_id)
    # pretty much need this whenever you have a user
    if "username" not in session:
        flash("Please login")
        return redirect("/login")
    if feedback.username == session['username']:
        form = AddFeedbackForm(title=feedback.title, content=feedback.content)
        if form.validate_on_submit():
            feedback.title = form.title.data
            feedback.content = form.content.data
            db.session.commit()
            flash("Feedback edited!")
            return redirect(f"/users/{feedback.username}")
        else:
            return render_template('edit-feedback-form.html', form=form)
    else:
        return redirect(f"/user/{feedback.username}")
Ejemplo n.º 7
0
def add_feedback(username):
    if "user" not in session:
        redirect("/")
    if username != session["user"]:
        return redirect(f"/users/{session['user']}")

    form = AddFeedbackForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        feedback = Feedback(title=title,
                            content=content,
                            username=session["user"])
        db.session.add(feedback)
        db.session.commit()
        return redirect(f"/users/{session['user']}")

    return render_template("add-feedback.html", user=username, form=form)
Ejemplo n.º 8
0
def update_feedback(id):
    f = Feedback.query.get(id)
    if "user" not in session:
        redirect("/")
    if f.username != session["user"]:
        return redirect(f"/users/{session['user']}")

    form = AddFeedbackForm()

    if form.validate_on_submit():
        f.title = form.title.data
        f.content = form.content.data
        db.session.commit()
        return redirect(f"/users/{session['user']}")

    form.title.data = f.title
    form.content.data = f.content

    return render_template("update-feedback.html", id=id, form=form)
Ejemplo n.º 9
0
def show_feedback_form(username):
    # user = User.query.get(username)
    if username == session['username']:
        form = AddFeedbackForm()
        if form.validate_on_submit():
            title = form.title.data
            content = form.content.data
            new_feedback = Feedback(title=title,
                                    content=content, username=username)
            db.session.add(new_feedback)
            db.session.commit()
            flash("Feedback added!")
            return redirect(f"/users/{username}")
        else:
            flash("Please login")
            return render_template('add-feedback-form.html', form=form)
    # else if "username" not in session:
        # and is a different user, log into their PF page
    else:
        return redirect("/login")
Ejemplo n.º 10
0
def feedback_add_view(username):
    if session.get('user') == username:
        form = AddFeedbackForm()
        
        if form.validate_on_submit():
            title = form.data.get('title')
            content = form.data.get('content')
            try:
                new_feedback = Feedback(title=title, content=content, username=username)
                db.session.add(new_feedback)
                db.session.commit()
            except exc.IntegrityError:
                flash('Failed to create feedback!', 'danger')
                return redirect(url_for('feedback_add_view', username=username))
            flash('Feedback created!', 'success')
            return redirect(url_for('user_detail_view', username=username))
        
        return render_template('feedback_add.html', username=username, form=form, submit_button="Create")
    
    else:
        raise Unauthorized('Permission denied. You can only add feedback under your account.')
Ejemplo n.º 11
0
def show_add_feedback(username):
    """Show a form to add feedback. Handle the posting of that feedback."""

    if "username" not in session or username != session['username']:
        flash("You do not have permission to view this content.")
        return redirect("/")
    else:
        form = AddFeedbackForm()

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data

        post = Feedback(title=title, content=content, username=username)
        db.session.add(post)
        db.session.commit()
        flash(f"Feedback Posted!", "success")
        return redirect(f"/users/{username}")

    else:

        return render_template("add_feedback.html", form=form)
Ejemplo n.º 12
0
def edit_feedback(id):
    """Edit the feedback."""

    if "username" not in session or username != session['username']:
        flash("You do not have permission to view this content.")
        return redirect("/")
    else:
        feedback = Feedback.query.get_or_404(id)
        form = AddFeedbackForm(obj=feedback)

    if form.validate_on_submit():
        feedback.title = form.title.data
        feedback.content = form.content.data
        # feedback.title=request.get("title", feedback.title)
        # feedback.content=request.get("content", feedback.content)

        db.session.commit()
        flash(f"Updated Feedback", "success")
        return redirect(f"/users/{feedback.username}")

    else:

        return render_template("edit_feedback.html", form=form)
Ejemplo n.º 13
0
def add_feedback(username):

    if not session['username'] == username or "username" not in session:
        flash('Must be logged in to view user feedback!')
        return redirect('/login')

    form = AddFeedbackForm()
    user = User.query.filter_by(username=username).first()

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data

        new_feedback = Feedback(title=title,
                                content=content,
                                username=username)

        db.session.add(new_feedback)
        db.session.commit()

        return redirect(f'/users/{username}')

    return render_template('feedback.html', user=user, form=form)
Ejemplo n.º 14
0
def feedback_edit_view(feedback_id):
    feedback = Feedback.query.get_or_404(feedback_id)
    username = feedback.username

    if session.get('user') == username:
        form = AddFeedbackForm(obj=feedback)    # populate form
        
        if form.validate_on_submit():
            feedback.title = form.data.get('title')
            feedback.content = form.data.get('content')
            try:
                db.session.add(feedback)
                db.session.commit()
            except exc.IntegrityError:
                flash('Failed to update feedback!', 'danger')
                return redirect(url_for('feedback_edit_view', feedback_id=feedback_id))
            flash('Feedback updated!', 'success')
            return redirect(url_for('user_detail_view', username=username))
        
        return render_template('feedback_edit.html', form=form, submit_button="Update")
    
    else:
        raise Unauthorized('Permission denied. You can only update feedback under your account.')
Ejemplo n.º 15
0
def feedback(username):
    """user submit feedback route"""
    if "username" not in session or username != session['username']:
        raise Unauthorized()

    form = AddFeedbackForm()

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data

        feedback = Feedback(
            title=title,
            content=content,
            username=username,
        )

        db.session.add(feedback)
        db.session.commit()

        return redirect(f"/users/{feedback.username}")

    else:
        return render_template("feedback/new.html", form=form)
Ejemplo n.º 16
0
def add_feedback(username):
    """This view function renders and submits a form to add user feedback"""

    if "username" not in session or username != session["username"]:
        raise Unauthorized()

    form = AddFeedbackForm()
    # user= User.query.get_or_404(username)

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        username = username

        new_feedback = Feedback(title=title,
                                content=content,
                                username=username)

        db.session.add(new_feedback)
        db.session.commit()

        return redirect(f"/users/{username}")

    return render_template("add_feedback.html", form=form)
Ejemplo n.º 17
0
def add_feedback(username):
    """ add feedbacks """

    current_username = session.get('username')

    # current_username has a value
    if current_username:
        # obtain current_username instance
        current_user = User.query.filter_by(username=current_username).first()

        # check if target user is current user or if current user is an admin
        if current_username == username or current_user.is_admin is True:

            form = AddFeedbackForm()

            if form.validate_on_submit():
                # grab data from the forms
                title = form.title.data
                content = form.content.data

                # created new feedback instance for targeted user and add to db
                new_feedback = Feedback.create_feedback(
                    title, content, username)
                db.session.add(new_feedback)
                db.session.commit()

                flash('You added one feedback!')

                return redirect(f'/users/{username}')

            else:
                return render_template("add_feedback.html",
                                       form=form,
                                       username=username)

    raise Unauthorized()
Ejemplo n.º 18
0
def edit_feedback(feedback_id):

    post = Feedback.query.get_or_404(feedback_id)
    username = post.username

    if not session['username'] == username or "username" not in session:
        flash('Must be logged in to edit user feedback!')
        return redirect('/login')

    form = AddFeedbackForm(obj=post)

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data

        post.title = title
        post.content = content

        db.session.add(post)
        db.session.commit()

        return redirect(f'/users/{username}')

    return render_template('editFeedback.html', form=form, post=post)