-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
53 lines (41 loc) · 1.37 KB
/
tasks.py
File metadata and controls
53 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
from celery import shared_task
logger = logging.getLogger(__name__)
@shared_task(
bind=True,
max_retries=3,
default_retry_delay=10,
autoretry_for=(Exception,),
retry_backoff=True,
)
def moderate_listing_task(self, listing_id):
from market.models import Listing
from utils.moderation import moderate_content
try:
listing = Listing.objects.get(id=listing_id)
except Listing.DoesNotExist:
logger.warning("Listing %s not found for moderation, skipping.", listing_id)
return
if listing.status != Listing.Status.PENDING:
logger.info(
"Listing %s status is %s (not PENDING), skipping.",
listing_id,
listing.status,
)
return
text = f"{listing.title} {listing.description or ''}"
image_urls = []
for img in listing.images.all():
if img.image and img.image.url.startswith("http"):
image_urls.append(img.image.url)
logger.info(
"Moderating listing %s: text=%r, image_count=%d",
listing_id,
text[:100],
len(image_urls),
)
is_safe = moderate_content(text, image_urls)
status = Listing.Status.APPROVED if is_safe else Listing.Status.REJECTED
logger.info("Listing %s moderation result: %s", listing_id, status)
listing.status = status
listing.save(update_fields=["status"])