Update unique_slug

This commit is contained in:
Nyymix 2025-01-09 15:07:32 +02:00
parent 732834e598
commit ea92c2c788

View file

@ -65,14 +65,11 @@ class Photo(models.Model):
def _get_upload_path(instance, filename): def _get_upload_path(instance, filename):
return os.path.join('albums', str(instance.album.slug), filename) return os.path.join('albums', str(instance.album.slug), filename)
def _generate_unique_slug(self, datetime_taken=None): def _generate_unique_slug(self, datetime_taken=datetime.now()):
datetime_taken = datetime_taken or datetime.now() slug = int(datetime_taken.strftime('%y%m%d%H%M%S'))
slug = datetime_taken.strftime('%y%m%d%H%M%S')
counter = 0
while Photo.objects.filter(album=self.album, slug=slug).exists(): while Photo.objects.filter(album=self.album, slug=slug).exists():
counter += 1 slug += 1
slug = (datetime_taken + timedelta(seconds=counter)).strftime('%y%m%d%H%M%S') return str(slug)
return slug
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name="Album") album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name="Album")
slug = models.CharField(max_length=15, editable=False, verbose_name="Photo Slug") slug = models.CharField(max_length=15, editable=False, verbose_name="Photo Slug")