06/16 Phase 6
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
"""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.commit()
|
||||
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
|
||||
Reference in New Issue
Block a user