def get_new_review(form, resource):
    """
    Gets a new review based on the submitted form and specified resource.
    Assumes submission from the current user.

    Args:
        form: The WTForms Form instance to use.
            Should incorporate the ReviewFieldsMixin mixin.
        resource: The associated resource.

    Returns:
        An instantiated/inflated Review instance.
    """
    # Set up the new review
    new_r = Review(
        int(form.rating.data),
        form.review_comments.data,
        resource,
        user=current_user)

    # Set the IP
    new_r.ip = get_ip()

    # Add optional intake/staff ratings
    if int(form.intake_rating.data) > 0:
        new_r.intake_rating = int(form.intake_rating.data)
    else:
        new_r.intake_rating = None

    if int(form.staff_rating.data) > 0:
        new_r.staff_rating = int(form.staff_rating.data)
    else:
        new_r.staff_rating = None

    return new_r
Example #2
0
def get_new_review(form, resource):
    """
    Gets a new review based on the submitted form and specified resource.
    Assumes submission from the current user.

    Args:
        form: The WTForms Form instance to use.
            Should incorporate the ReviewFieldsMixin mixin.
        resource: The associated resource.

    Returns:
        An instantiated/inflated Review instance.
    """
    # Set up the new review
    new_r = Review(int(form.rating.data),
                   form.review_comments.data,
                   resource,
                   user=current_user)

    # Set the IP
    new_r.ip = get_ip()

    # Add optional intake/staff ratings
    if int(form.intake_rating.data) > 0:
        new_r.intake_rating = int(form.intake_rating.data)
    else:
        new_r.intake_rating = None

    if int(form.staff_rating.data) > 0:
        new_r.staff_rating = int(form.staff_rating.data)
    else:
        new_r.staff_rating = None

    return new_r
def new_review(resource_id):
    """
    Allows users to submit a new review.

    Args:
        resource_id: The ID of the resource to review.

    Returns:
        When accessed via GET, a form for submitting reviews (via add-review.html).
        This template is provided with the following variables:
            provider: The specific provider being reviewd.
            has_existing_review: A boolean indicating if the
                current user has already left a review for
                this resource.
            form: A ReviewForm instance for submitting a
                new review.
        When accessed via POST, a redirection action to the associated resource
        after the review has been successfully submitted.
    """
    # Get the form - prefill the resource_id with what's
    # been provided via query string.
    form = ReviewForm(request.form)
    form.provider.data = resource_id

    # Get the associated resource
    resource = resource_with_id(resource_id)

    # See if we have other existing reviews left by this user
    existing_reviews = resource.reviews. \
        filter(Review.user_id == current_user.id). \
        all()

    if len(existing_reviews) > 0:
        has_existing_review = True
    else:
        has_existing_review = False

    # Only bother trying to handle the form if we have a submission
    if request.method == 'POST':
        # See if the form's valid
        if form.validate_on_submit():

            # Set up the new review
            new_r = Review(int(form.rating.data), 
                form.comments.data,
                resource, 
                user=current_user)

            # Set the IP
            new_r.ip = get_ip()

            # Add optional intake/staff ratings
            if int(form.intake_rating.data) > 0:
                new_r.intake_rating = int(form.intake_rating.data)
            else:
                new_r.intake_rating = None

            if int(form.staff_rating.data) > 0:
                new_r.staff_rating = int(form.staff_rating.data)
            else:
                new_r.staff_rating = None

            # Add the review and flush the DB to get the new review ID
            db.session.add(new_r)
            db.session.flush()

            # If we have other existing reviews, mark them as old
            if len(existing_reviews) > 0:
                for old_review in existing_reviews:
                    old_review.is_old_review = True
                    old_review.new_review_id = new_r.id

            db.session.commit()

            # Redirect the user to the resource
            flash('Review submitted!')

            return resource_redirect(new_r.resource_id)
        else:
            # Not valid - flash errors
            flash_errors(form)

    # We'll hit this if the form is invalid or we're
    # doing a simple GET.
    return render_template('add-review.html', 
        provider=resource,
        has_existing_review=has_existing_review,
        form=form)
Example #4
0
def new_review(resource_id):
    """
    Allows users to submit a new review.

    Args:
        resource_id: The ID of the resource to review.

    Returns:
        When accessed via GET, a form for submitting reviews (via add-review.html).
        This template is provided with the following variables:
            provider: The specific provider being reviewd.
            has_existing_review: A boolean indicating if the
                current user has already left a review for
                this resource.
            form: A ReviewForm instance for submitting a
                new review.
        When accessed via POST, a redirection action to the associated resource
        after the review has been successfully submitted.
    """
    # Get the form - prefill the resource_id with what's
    # been provided via query string.
    form = ReviewForm(request.form)
    form.provider.data = resource_id

    # Get the associated resource
    resource = resource_with_id(resource_id)

    # See if we have other existing reviews left by this user
    existing_reviews = resource.reviews. \
        filter(Review.user_id == current_user.id). \
        all()

    if len(existing_reviews) > 0:
        has_existing_review = True
    else:
        has_existing_review = False

    # Only bother trying to handle the form if we have a submission
    if request.method == 'POST':
        # See if the form's valid
        if form.validate_on_submit():

            # Set up the new review
            new_r = Review(int(form.rating.data),
                           form.comments.data,
                           resource,
                           user=current_user)

            # Set the IP
            new_r.ip = get_ip()

            # Add optional intake/staff ratings
            if int(form.intake_rating.data) > 0:
                new_r.intake_rating = int(form.intake_rating.data)
            else:
                new_r.intake_rating = None

            if int(form.staff_rating.data) > 0:
                new_r.staff_rating = int(form.staff_rating.data)
            else:
                new_r.staff_rating = None

            # Add the review and flush the DB to get the new review ID
            db.session.add(new_r)
            db.session.flush()

            # If we have other existing reviews, mark them as old
            if len(existing_reviews) > 0:
                for old_review in existing_reviews:
                    old_review.is_old_review = True
                    old_review.new_review_id = new_r.id

            db.session.commit()

            # Redirect the user to the resource
            flash('Review submitted!')

            return resource_redirect(new_r.resource_id)
        else:
            # Not valid - flash errors
            flash_errors(form)

    # We'll hit this if the form is invalid or we're
    # doing a simple GET.
    return render_template('add-review.html',
                           provider=resource,
                           has_existing_review=has_existing_review,
                           form=form)