models.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # models.py
  2. from django.db import models
  3. # Note: JSONField is automatically used for PostgreSQL by Django 3.1+
  4. # If using an older Django, you might need: from django.contrib.postgres.fields import JSONField
  5. class Product(models.Model):
  6. """Product model to store basic product information"""
  7. sku = models.CharField(max_length=100, unique=True)
  8. category = models.CharField(max_length=100)
  9. title = models.TextField()
  10. description = models.TextField(blank=True)
  11. # New fields for completeness (optional, but good practice)
  12. short_description = models.TextField(blank=True)
  13. seo_title = models.CharField(max_length=255, blank=True)
  14. seo_description = models.TextField(blank=True)
  15. attributes = models.JSONField(default=dict)
  16. created_at = models.DateTimeField(auto_now_add=True)
  17. updated_at = models.DateTimeField(auto_now=True)
  18. class Meta:
  19. indexes = [
  20. models.Index(fields=['category']),
  21. models.Index(fields=['sku']),
  22. ]
  23. def __str__(self):
  24. return f"{self.sku} - {self.title}"
  25. # ... AttributeScore model remains the same ...
  26. class AttributeScore(models.Model):
  27. """Store attribute quality scores"""
  28. product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='attribute_scores')
  29. score = models.FloatField()
  30. max_score = models.FloatField(default=100.0)
  31. details = models.JSONField(default=dict)
  32. issues = models.JSONField(default=list)
  33. suggestions = models.JSONField(default=list)
  34. ai_suggestions = models.JSONField(default=dict, blank=True) # Gemini AI suggestions
  35. processing_time = models.FloatField(null=True, blank=True)
  36. created_at = models.DateTimeField(auto_now_add=True)
  37. class Meta:
  38. indexes = [
  39. models.Index(fields=['-created_at']),
  40. ]
  41. def __str__(self):
  42. return f"{self.product.sku} - Score: {self.score}/{self.max_score}"
  43. # ... CategoryAttributeRule model remains the same ...
  44. class CategoryAttributeRule(models.Model):
  45. """Define mandatory attributes per category"""
  46. category = models.CharField(max_length=100)
  47. attribute_name = models.CharField(max_length=100)
  48. is_mandatory = models.BooleanField(default=False)
  49. valid_values = models.JSONField(default=list, blank=True)
  50. data_type = models.CharField(max_length=50, default='string')
  51. validation_regex = models.CharField(max_length=500, blank=True)
  52. min_length = models.IntegerField(null=True, blank=True)
  53. max_length = models.IntegerField(null=True, blank=True)
  54. description = models.TextField(blank=True)
  55. class Meta:
  56. unique_together = ('category', 'attribute_name')
  57. indexes = [
  58. models.Index(fields=['category']),
  59. ]
  60. def __str__(self):
  61. return f"{self.category} - {self.attribute_name}"
  62. # --- NEW MODEL FOR CONTENT RULES ---
  63. class ProductContentRule(models.Model):
  64. """Define rules for general product content fields (title, description, SEO)"""
  65. category = models.CharField(max_length=100, blank=True, null=True, help_text="Category or NULL for a global rule.")
  66. field_name = models.CharField(max_length=100, help_text="e.g., 'title', 'description', 'seo_title'")
  67. is_mandatory = models.BooleanField(default=True)
  68. min_length = models.IntegerField(null=True, blank=True, help_text="Minimum character length.")
  69. max_length = models.IntegerField(null=True, blank=True, help_text="Maximum character length.")
  70. min_word_count = models.IntegerField(null=True, blank=True, help_text="Minimum word count.")
  71. max_word_count = models.IntegerField(null=True, blank=True, help_text="Maximum word count.")
  72. must_contain_keywords = models.JSONField(default=list, blank=True, help_text="List of keywords (case-insensitive) that must be present.")
  73. validation_regex = models.CharField(max_length=500, blank=True, help_text="A regex to validate the field content.")
  74. description = models.TextField(blank=True)
  75. class Meta:
  76. unique_together = ('category', 'field_name')
  77. indexes = [
  78. models.Index(fields=['field_name']),
  79. models.Index(fields=['category', 'field_name']),
  80. ]
  81. def __str__(self):
  82. category_str = self.category if self.category else 'GLOBAL'
  83. return f"{category_str} - Content Rule: {self.field_name}"