Browse Source

feat(ads): initialize ads app with Ad, AdEvaluation, and NotificationLog models and API views

master
PouyaKhajavi 2 days ago
parent
commit
5aa180350a
  1. 0
      backend/ads/__init__.py
  2. 3
      backend/ads/admin.py
  3. 5
      backend/ads/apps.py
  4. 0
      backend/ads/migrations/__init__.py
  5. 59
      backend/ads/models.py
  6. 3
      backend/ads/tests.py
  7. 6
      backend/ads/urls.py
  8. 10
      backend/ads/views.py
  9. 1
      backend/config/settings.py
  10. 1
      backend/config/urls.py

0
backend/ads/__init__.py

3
backend/ads/admin.py

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
backend/ads/apps.py

@ -0,0 +1,5 @@
from django.apps import AppConfig
class AdsConfig(AppConfig):
name = 'ads'

0
backend/ads/migrations/__init__.py

59
backend/ads/models.py

@ -0,0 +1,59 @@
from django.db import models
from core.models import BaseModel
from crawler.models import CrawlTask
class Ad(BaseModel):
divar_token = models.CharField(max_length=50, unique=True)
title = models.CharField(max_length=255)
description = models.TextField()
price = models.CharField(max_length=100, null=True, blank=True)
category = models.CharField(max_length=100, null=True, blank=True)
images = models.JSONField(default=list)
url = models.URLField()
created_at = models.DateTimeField(auto_now_add=True)
published_at = models.DateTimeField(null=True, blank=True)
class Meta:
indexes = [
models.Index(fields=['created_at']),
]
def __str__(self):
return f"{self.title} (Token: {self.divar_token})"
class AdEvaluation(BaseModel):
crawl_task = models.ForeignKey(CrawlTask, on_delete=models.PROTECT, related_name='evaluations')
ad = models.ForeignKey(Ad, on_delete=models.PROTECT, related_name='evaluations')
is_flagged = models.BooleanField(default=False)
reason = models.TextField(null=True, blank=True)
confidence = models.FloatField(null=True, blank=True)
extracted_fields = models.JSONField(default=dict)
evaluated_at = models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=['crawl_task', 'ad'], name='unique_crawl_task_ad')
]
indexes = [
models.Index(fields=['is_flagged']),
models.Index(fields=['evaluated_at']),
]
def __str__(self):
return f"Evaluation of Ad {self.ad.id} under Task {self.crawl_task.title} (Flagged: {self.is_flagged})"
class NotificationLog(BaseModel):
STATUS_CHOICES = [
('SENT', 'Sent'),
('FAILED', 'Failed'),
]
evaluation = models.ForeignKey(AdEvaluation, on_delete=models.CASCADE, related_name='notifications')
channel_id = models.CharField(max_length=100)
sent_at = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=20, choices=STATUS_CHOICES)
error_message = models.TextField(null=True, blank=True)
def __str__(self):
return f"Notification to {self.channel_id} (Status: {self.status})"

3
backend/ads/tests.py

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

6
backend/ads/urls.py

@ -0,0 +1,6 @@
from django.urls import path
from .views import AdsView
urlpatterns = [
path('', AdsView.as_view()),
]

10
backend/ads/views.py

@ -0,0 +1,10 @@
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class AdsView(APIView):
def get(self, request):
return Response({'message': 'Hello from Ads'})

1
backend/config/settings.py

@ -49,6 +49,7 @@ INSTALLED_APPS = [
# Local apps
'core',
'crawler',
'ads',
]
MIDDLEWARE = [

1
backend/config/urls.py

@ -20,4 +20,5 @@ from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/crawlers/', include('crawler.urls')),
path('api/ads/', include('ads.urls')),
]
Loading…
Cancel
Save