first commit

This commit is contained in:
pacnpal
2024-10-28 17:09:57 -04:00
commit 1339baec59
9993 changed files with 1182741 additions and 0 deletions

0
media/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

19
media/admin.py Normal file
View File

@@ -0,0 +1,19 @@
from django.contrib import admin
from django.utils.html import format_html
from .models import Photo
@admin.register(Photo)
class PhotoAdmin(admin.ModelAdmin):
list_display = ('thumbnail_preview', 'content_type', 'content_object', 'caption', 'is_primary', 'created_at')
list_filter = ('content_type', 'is_primary', 'created_at')
search_fields = ('caption', 'alt_text')
readonly_fields = ('thumbnail_preview',)
def thumbnail_preview(self, obj):
if obj.image:
return format_html(
'<img src="{}" style="max-height: 50px; max-width: 100px;" />',
obj.image.url
)
return "No image"
thumbnail_preview.short_description = 'Thumbnail'

6
media/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class MediaConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'media'
verbose_name = 'Media'

View File

@@ -0,0 +1,35 @@
# Generated by Django 5.1.2 on 2024-10-28 20:17
import django.db.models.deletion
import media.models
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
]
operations = [
migrations.CreateModel(
name='Photo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(upload_to=media.models.photo_upload_path)),
('caption', models.CharField(blank=True, max_length=255)),
('alt_text', models.CharField(blank=True, max_length=255)),
('is_primary', models.BooleanField(default=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('object_id', models.PositiveIntegerField()),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
],
options={
'ordering': ['-is_primary', '-created_at'],
'indexes': [models.Index(fields=['content_type', 'object_id'], name='media_photo_content_0187f5_idx')],
},
),
]

View File

Binary file not shown.

52
media/models.py Normal file
View File

@@ -0,0 +1,52 @@
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
import os
def photo_upload_path(instance, filename):
"""Generate upload path for photos"""
# Get the content type and object
content_type = instance.content_type.model
obj = instance.content_object
# Get object identifier (slug or id)
identifier = getattr(obj, 'slug', obj.id)
# Create path: content_type/identifier/filename
base, ext = os.path.splitext(filename)
new_filename = f"{slugify(base)}{ext}"
return f"{content_type}/{identifier}/{new_filename}"
class Photo(models.Model):
"""Generic photo model that can be attached to any model"""
image = models.ImageField(upload_to=photo_upload_path)
caption = models.CharField(max_length=255, blank=True)
alt_text = models.CharField(max_length=255, blank=True)
is_primary = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Generic foreign key fields
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
ordering = ['-is_primary', '-created_at']
indexes = [
models.Index(fields=['content_type', 'object_id']),
]
def __str__(self):
return f"{self.content_type} - {self.content_object} - {self.caption or 'No caption'}"
def save(self, *args, **kwargs):
# If this is marked as primary, unmark other primary photos
if self.is_primary:
Photo.objects.filter(
content_type=self.content_type,
object_id=self.object_id,
is_primary=True
).exclude(id=self.id).update(is_primary=False)
super().save(*args, **kwargs)