06/18 Phase 7
This commit is contained in:
@@ -14,6 +14,7 @@ from app.services import listings as svc
|
||||
from app.services.geo import geocode_zip
|
||||
from app.services.images import process_upload, delete_image_files, ImageError
|
||||
from app.services import reports as rsvc
|
||||
from app.services import reviews as rev_svc
|
||||
from app.blueprints.listings.forms import ListingForm, ImageUploadForm
|
||||
|
||||
listings_bp = Blueprint("listings", __name__)
|
||||
@@ -103,8 +104,18 @@ def detail(listing_id):
|
||||
if not is_owner:
|
||||
listing.view_count = (listing.view_count or 0) + 1
|
||||
db.session.commit()
|
||||
# determine if current user can leave a review
|
||||
can_review = False
|
||||
if (current_user.is_authenticated and not is_owner
|
||||
and listing.status == ListingStatus.sold):
|
||||
from app.models.review import Review
|
||||
already = Review.query.filter_by(
|
||||
listing_id=listing.id, author_id=current_user.id).first()
|
||||
can_review = already is None
|
||||
listing_rating = rev_svc.seller_rating(listing.user_id)
|
||||
return render_template("listings/detail.html", listing=listing,
|
||||
is_owner=is_owner)
|
||||
is_owner=is_owner, can_review=can_review,
|
||||
listing_rating=listing_rating)
|
||||
|
||||
|
||||
# --- create ---
|
||||
@@ -243,6 +254,31 @@ def report(listing_id):
|
||||
return redirect(url_for("listings.detail", listing_id=listing.id))
|
||||
|
||||
|
||||
# --- leave review ---
|
||||
@listings_bp.route("/listings/<int:listing_id>/review", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def leave_review(listing_id):
|
||||
listing = Listing.query.get_or_404(listing_id)
|
||||
if listing.status != ListingStatus.sold:
|
||||
flash(_("You can only review sold listings."), "warning")
|
||||
return redirect(url_for("listings.detail", listing_id=listing.id))
|
||||
if listing.user_id == current_user.id:
|
||||
flash(_("You cannot review your own listing."), "warning")
|
||||
return redirect(url_for("listings.detail", listing_id=listing.id))
|
||||
|
||||
if request.method == "POST":
|
||||
rating = request.form.get("rating", type=int)
|
||||
body = request.form.get("body", "").strip()
|
||||
try:
|
||||
rev_svc.create_review(listing, current_user, rating, body)
|
||||
flash(_("Review submitted. Thanks!"), "success")
|
||||
return redirect(url_for("listings.detail", listing_id=listing.id))
|
||||
except rev_svc.ReviewError as e:
|
||||
flash(str(e), "danger")
|
||||
|
||||
return render_template("listings/review_form.html", listing=listing)
|
||||
|
||||
|
||||
# --- my listings ---
|
||||
@listings_bp.route("/my/listings")
|
||||
@login_required
|
||||
|
||||
Reference in New Issue
Block a user