Add multi-image compositions, docker setup, tests, and docs
- Add CompositionImage model with multi-image upload and main image flag - Add composition endpoints: add-images, set-main-image, delete image, by-created-at filter - Make contact-us by_category public - Add 24 API tests covering all endpoints - Add Dockerfile, docker-compose, entrypoint and docker docs - Add Persian API docs and update README and Postman collection Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -49,6 +49,8 @@ class Composition(models.Model):
|
||||
"""
|
||||
name = models.CharField(max_length=200, verbose_name='نام')
|
||||
description = models.TextField(verbose_name='توضیحات')
|
||||
# Legacy single-image field, kept for backward compatibility.
|
||||
# New uploads use the related CompositionImage model below.
|
||||
image = models.ImageField(
|
||||
upload_to='compositions/',
|
||||
verbose_name='تصویر',
|
||||
@@ -68,6 +70,56 @@ class Composition(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def main_image(self):
|
||||
"""Return the image flagged as main, falling back to the first image."""
|
||||
return self.images.filter(is_main=True).first() or self.images.first()
|
||||
|
||||
|
||||
class CompositionImage(models.Model):
|
||||
"""
|
||||
Image belonging to a Composition. A composition can have many images,
|
||||
and at most one of them can be flagged as the main image.
|
||||
"""
|
||||
composition = models.ForeignKey(
|
||||
Composition,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='images',
|
||||
verbose_name='ترکیب'
|
||||
)
|
||||
image = models.ImageField(
|
||||
upload_to='compositions/',
|
||||
verbose_name='تصویر'
|
||||
)
|
||||
is_main = models.BooleanField(default=False, verbose_name='تصویر اصلی')
|
||||
created_at = models.DateTimeField(auto_now_add=True, verbose_name='تاریخ ایجاد')
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'تصویر ترکیب'
|
||||
verbose_name_plural = 'تصاویر ترکیب'
|
||||
# Main image first, then newest.
|
||||
ordering = ['-is_main', '-created_at']
|
||||
constraints = [
|
||||
# At most one main image per composition.
|
||||
models.UniqueConstraint(
|
||||
fields=['composition'],
|
||||
condition=models.Q(is_main=True),
|
||||
name='unique_main_image_per_composition'
|
||||
)
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.composition.name} - {'main' if self.is_main else 'image'} #{self.pk}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# Ensure only one main image per composition: if this one is being
|
||||
# set as main, unset the flag on the others.
|
||||
if self.is_main:
|
||||
CompositionImage.objects.filter(
|
||||
composition=self.composition, is_main=True
|
||||
).exclude(pk=self.pk).update(is_main=False)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class Campaign(models.Model):
|
||||
|
||||
Reference in New Issue
Block a user