Ejemplo n.º 1
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
Ejemplo n.º 2
0
def new_review():
    """
    This function handles the creation of new reviews,
    if the review submitted is valid then we create
    a record in the database linking it to a Resource
    and a User.

    When something goes wrong in the validation the User
    is redirected to the home page. We should better
    discuss form UI stuff.

    If all is OK the user is redirected to the provider
    been reviewed.
    """
    form = ReviewForm()

    if form.validate_on_submit():

        new_r = Review(form.rating.data,
                       form.description.data,
                       Resource.query.get(form.provider.data),
                       user=current_user)

        db.session.add(new_r)

        # Flush the session so we get an ID
        db.session.flush()

        # See if we have other existing reviews
        existing_reviews = Review.query. \
            filter(Review.id != new_r.id). \
            filter(Review.resource_id == new_r.resource_id). \
            filter(Review.user_id == new_r.user_id). \
            all()

        # If we do, mark all those old reviews 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()

        flash('Review submitted!')

        return resource_redirect(new_r.resource_id)
    else:
        flash_errors(form)

        # Try to see if we can get the resource ID and at least
        # go back to the form
        try:
            resource_id = int(form.provider.data)
            return resource_redirect(resource_id)
        except:
            return redirect('/')
Ejemplo n.º 3
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)
Ejemplo n.º 5
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)