parent
3b3cf56896
commit
7c68762178
@ -0,0 +1,33 @@ |
|||||||
|
# Generated by Django 5.1 on 2025-05-07 07:58 |
||||||
|
|
||||||
|
import django.db.models.deletion |
||||||
|
import django.utils.timezone |
||||||
|
import tournaments.models.image |
||||||
|
import uuid |
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('tournaments', '0119_alter_tournament_animation_type'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.CreateModel( |
||||||
|
name='Image', |
||||||
|
fields=[ |
||||||
|
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), |
||||||
|
('title', models.CharField(blank=True, max_length=255)), |
||||||
|
('description', models.TextField(blank=True)), |
||||||
|
('image', models.ImageField(upload_to=tournaments.models.image.image_upload_path)), |
||||||
|
('uploaded_at', models.DateTimeField(default=django.utils.timezone.now)), |
||||||
|
('image_type', models.CharField(choices=[('sponsor', 'Sponsor'), ('club', 'Club')], default='sponsor', max_length=20)), |
||||||
|
('is_primary', models.BooleanField(default=False)), |
||||||
|
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='tournaments.event')), |
||||||
|
], |
||||||
|
options={ |
||||||
|
'ordering': ['-uploaded_at'], |
||||||
|
}, |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
# Generated by Django 5.1 on 2025-05-07 11:42 |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('tournaments', '0120_image'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AlterModelOptions( |
||||||
|
name='image', |
||||||
|
options={'ordering': ['order']}, |
||||||
|
), |
||||||
|
migrations.RemoveField( |
||||||
|
model_name='image', |
||||||
|
name='is_primary', |
||||||
|
), |
||||||
|
migrations.AddField( |
||||||
|
model_name='image', |
||||||
|
name='order', |
||||||
|
field=models.IntegerField(default=0), |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
from django.db import models |
||||||
|
import uuid |
||||||
|
import os |
||||||
|
from django.utils.timezone import now |
||||||
|
from .event import Event |
||||||
|
|
||||||
|
def image_upload_path(instance, filename): |
||||||
|
"""Generate a unique file path for the uploaded image.""" |
||||||
|
# Get the file extension from the original filename |
||||||
|
ext = filename.split('.')[-1] |
||||||
|
# Create a unique filename using UUID |
||||||
|
unique_filename = f"{uuid.uuid4().hex}.{ext}" |
||||||
|
|
||||||
|
# Determine the folder based on the event |
||||||
|
folder = f"event_{instance.event.id}" |
||||||
|
|
||||||
|
# Return the complete upload path |
||||||
|
return os.path.join('images', folder, unique_filename) |
||||||
|
|
||||||
|
class Image(models.Model): |
||||||
|
"""Model for storing uploaded images for events.""" |
||||||
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) |
||||||
|
title = models.CharField(max_length=255, blank=True) |
||||||
|
description = models.TextField(blank=True) |
||||||
|
image = models.ImageField(upload_to=image_upload_path) |
||||||
|
uploaded_at = models.DateTimeField(default=now) |
||||||
|
|
||||||
|
# Relation to event model |
||||||
|
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='images') |
||||||
|
|
||||||
|
# Image type for filtering |
||||||
|
IMAGE_TYPES = ( |
||||||
|
('sponsor', 'Sponsor'), |
||||||
|
('club', 'Club'), |
||||||
|
) |
||||||
|
image_type = models.CharField(max_length=20, choices=IMAGE_TYPES, default='sponsor') |
||||||
|
|
||||||
|
order = models.IntegerField(default=0) |
||||||
|
|
||||||
|
class Meta: |
||||||
|
ordering = ['order'] |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
return self.title or f"Event Image {self.id}" |
||||||
Loading…
Reference in new issue