Files
2026-06-17 17:06:43 -04:00

33 lines
1.2 KiB
Python

"""User-submitted listing reports + flag-count threshold auto-flag."""
from sqlalchemy.exc import IntegrityError
from app.extensions import db
from app.models.report import Report
from app.models.enums import ListingStatus, ReportReason
from app.services.settings import get_setting
class ReportError(ValueError):
pass
def create_report(listing, reporter, reason: ReportReason, note=None):
"""Record a report, bump flag_count, auto-flag at threshold. Commits."""
if listing.user_id == reporter.id:
raise ReportError("cannot report your own listing")
report = Report(listing_id=listing.id, reporter_id=reporter.id,
reason=reason, note=(note or "").strip() or None)
db.session.add(report)
try:
db.session.flush()
except IntegrityError:
db.session.rollback()
raise ReportError("you have already reported this listing")
listing.flag_count = (listing.flag_count or 0) + 1
threshold = get_setting("flag_threshold", 5)
if listing.flag_count >= threshold and listing.status == ListingStatus.active:
listing.status = ListingStatus.flagged
db.session.commit()
return report