mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-27 10:27:04 -05:00
feat: Add blog, media, and support apps, implement ride credits and image API, and remove toplist feature.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
# Generated by Django 5.1.4 on 2025-08-13 21:35
|
||||
# Generated by Django 5.1.6 on 2025-12-26 14:30
|
||||
|
||||
import django.db.models.deletion
|
||||
import apps.media.models
|
||||
import apps.media.storage
|
||||
import pgtrigger.compiler
|
||||
import pgtrigger.migrations
|
||||
from django.conf import settings
|
||||
@@ -15,6 +13,7 @@ class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("contenttypes", "0002_remove_content_type_name"),
|
||||
("django_cloudflareimages_toolkit", "0001_initial"),
|
||||
("pghistory", "0006_delete_aggregateevent"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
@@ -23,88 +22,82 @@ class Migration(migrations.Migration):
|
||||
migrations.CreateModel(
|
||||
name="Photo",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"image",
|
||||
models.ImageField(
|
||||
max_length=255,
|
||||
storage=apps.media.storage.MediaStorage(),
|
||||
upload_to=apps.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)),
|
||||
("is_approved", models.BooleanField(default=False)),
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
("date_taken", models.DateTimeField(blank=True, null=True)),
|
||||
("object_id", models.PositiveIntegerField()),
|
||||
("object_id", models.PositiveIntegerField(help_text="ID of the item")),
|
||||
("caption", models.CharField(blank=True, help_text="Photo caption", max_length=255)),
|
||||
("is_public", models.BooleanField(default=True, help_text="Whether this photo is visible to others")),
|
||||
("source", models.CharField(blank=True, help_text="Source/Credit if applicable", max_length=100)),
|
||||
(
|
||||
"content_type",
|
||||
models.ForeignKey(
|
||||
help_text="Type of item this photo belongs to",
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="contenttypes.contenttype",
|
||||
),
|
||||
),
|
||||
(
|
||||
"uploaded_by",
|
||||
"image",
|
||||
models.ForeignKey(
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="uploaded_photos",
|
||||
help_text="Cloudflare Image reference",
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="photos_usage",
|
||||
to="django_cloudflareimages_toolkit.cloudflareimage",
|
||||
),
|
||||
),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
help_text="User who uploaded this photo",
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="photos",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"ordering": ["-is_primary", "-created_at"],
|
||||
"verbose_name": "Photo",
|
||||
"verbose_name_plural": "Photos",
|
||||
"ordering": ["-created_at"],
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="PhotoEvent",
|
||||
fields=[
|
||||
(
|
||||
"pgh_id",
|
||||
models.AutoField(primary_key=True, serialize=False),
|
||||
),
|
||||
("pgh_id", models.AutoField(primary_key=True, serialize=False)),
|
||||
("pgh_created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("pgh_label", models.TextField(help_text="The event label.")),
|
||||
("id", models.BigIntegerField()),
|
||||
(
|
||||
"image",
|
||||
models.ImageField(
|
||||
max_length=255,
|
||||
storage=apps.media.storage.MediaStorage(),
|
||||
upload_to=apps.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)),
|
||||
("is_approved", models.BooleanField(default=False)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
("date_taken", models.DateTimeField(blank=True, null=True)),
|
||||
("object_id", models.PositiveIntegerField()),
|
||||
("object_id", models.PositiveIntegerField(help_text="ID of the item")),
|
||||
("caption", models.CharField(blank=True, help_text="Photo caption", max_length=255)),
|
||||
("is_public", models.BooleanField(default=True, help_text="Whether this photo is visible to others")),
|
||||
("source", models.CharField(blank=True, help_text="Source/Credit if applicable", max_length=100)),
|
||||
(
|
||||
"content_type",
|
||||
models.ForeignKey(
|
||||
db_constraint=False,
|
||||
help_text="Type of item this photo belongs to",
|
||||
on_delete=django.db.models.deletion.DO_NOTHING,
|
||||
related_name="+",
|
||||
related_query_name="+",
|
||||
to="contenttypes.contenttype",
|
||||
),
|
||||
),
|
||||
(
|
||||
"image",
|
||||
models.ForeignKey(
|
||||
db_constraint=False,
|
||||
help_text="Cloudflare Image reference",
|
||||
on_delete=django.db.models.deletion.DO_NOTHING,
|
||||
related_name="+",
|
||||
related_query_name="+",
|
||||
to="django_cloudflareimages_toolkit.cloudflareimage",
|
||||
),
|
||||
),
|
||||
(
|
||||
"pgh_context",
|
||||
models.ForeignKey(
|
||||
@@ -125,10 +118,10 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
),
|
||||
(
|
||||
"uploaded_by",
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
db_constraint=False,
|
||||
null=True,
|
||||
help_text="User who uploaded this photo",
|
||||
on_delete=django.db.models.deletion.DO_NOTHING,
|
||||
related_name="+",
|
||||
related_query_name="+",
|
||||
@@ -142,18 +135,15 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="photo",
|
||||
index=models.Index(
|
||||
fields=["content_type", "object_id"],
|
||||
name="media_photo_content_0187f5_idx",
|
||||
),
|
||||
index=models.Index(fields=["content_type", "object_id"], name="media_photo_content_0187f5_idx"),
|
||||
),
|
||||
pgtrigger.migrations.AddTrigger(
|
||||
model_name="photo",
|
||||
trigger=pgtrigger.compiler.Trigger(
|
||||
name="insert_insert",
|
||||
sql=pgtrigger.compiler.UpsertTriggerSql(
|
||||
func='INSERT INTO "media_photoevent" ("alt_text", "caption", "content_type_id", "created_at", "date_taken", "id", "image", "is_approved", "is_primary", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "updated_at", "uploaded_by_id") VALUES (NEW."alt_text", NEW."caption", NEW."content_type_id", NEW."created_at", NEW."date_taken", NEW."id", NEW."image", NEW."is_approved", NEW."is_primary", NEW."object_id", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."updated_at", NEW."uploaded_by_id"); RETURN NULL;',
|
||||
hash="[AWS-SECRET-REMOVED]",
|
||||
func='INSERT INTO "media_photoevent" ("caption", "content_type_id", "created_at", "id", "image_id", "is_public", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "source", "updated_at", "user_id") VALUES (NEW."caption", NEW."content_type_id", NEW."created_at", NEW."id", NEW."image_id", NEW."is_public", NEW."object_id", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."source", NEW."updated_at", NEW."user_id"); RETURN NULL;',
|
||||
hash="05c2d557f631f80ebd4b37ffb1ba9a539fa54244",
|
||||
operation="INSERT",
|
||||
pgid="pgtrigger_insert_insert_e1ca0",
|
||||
table="media_photo",
|
||||
@@ -167,8 +157,8 @@ class Migration(migrations.Migration):
|
||||
name="update_update",
|
||||
sql=pgtrigger.compiler.UpsertTriggerSql(
|
||||
condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
|
||||
func='INSERT INTO "media_photoevent" ("alt_text", "caption", "content_type_id", "created_at", "date_taken", "id", "image", "is_approved", "is_primary", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "updated_at", "uploaded_by_id") VALUES (NEW."alt_text", NEW."caption", NEW."content_type_id", NEW."created_at", NEW."date_taken", NEW."id", NEW."image", NEW."is_approved", NEW."is_primary", NEW."object_id", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."updated_at", NEW."uploaded_by_id"); RETURN NULL;',
|
||||
hash="[AWS-SECRET-REMOVED]",
|
||||
func='INSERT INTO "media_photoevent" ("caption", "content_type_id", "created_at", "id", "image_id", "is_public", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "source", "updated_at", "user_id") VALUES (NEW."caption", NEW."content_type_id", NEW."created_at", NEW."id", NEW."image_id", NEW."is_public", NEW."object_id", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."source", NEW."updated_at", NEW."user_id"); RETURN NULL;',
|
||||
hash="9a4caabe540c0fd782b9c148444c364e385327f4",
|
||||
operation="UPDATE",
|
||||
pgid="pgtrigger_update_update_6ff7d",
|
||||
table="media_photo",
|
||||
|
||||
57
backend/apps/media/models.py
Normal file
57
backend/apps/media/models.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from apps.core.history import TrackedModel
|
||||
import pghistory
|
||||
# Using string reference for CloudflareImage to avoid circular imports if possible,
|
||||
# or direct import if safe. django-cloudflare-images-toolkit usually provides a field or model.
|
||||
# Checking installed apps... it's "django_cloudflareimages_toolkit".
|
||||
from django_cloudflareimages_toolkit.models import CloudflareImage
|
||||
|
||||
@pghistory.track()
|
||||
class Photo(TrackedModel):
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="photos",
|
||||
help_text="User who uploaded this photo",
|
||||
)
|
||||
|
||||
# The actual image
|
||||
image = models.ForeignKey(
|
||||
CloudflareImage,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="photos_usage",
|
||||
help_text="Cloudflare Image reference"
|
||||
)
|
||||
|
||||
# Generic relation to target object (Park, Ride, etc.)
|
||||
content_type = models.ForeignKey(
|
||||
ContentType,
|
||||
on_delete=models.CASCADE,
|
||||
help_text="Type of item this photo belongs to",
|
||||
)
|
||||
object_id = models.PositiveIntegerField(help_text="ID of the item")
|
||||
content_object = GenericForeignKey("content_type", "object_id")
|
||||
|
||||
# Metadata
|
||||
caption = models.CharField(max_length=255, blank=True, help_text="Photo caption")
|
||||
is_public = models.BooleanField(
|
||||
default=True,
|
||||
help_text="Whether this photo is visible to others"
|
||||
)
|
||||
|
||||
# We might want credit/source info if not taken by user
|
||||
source = models.CharField(max_length=100, blank=True, help_text="Source/Credit if applicable")
|
||||
|
||||
class Meta(TrackedModel.Meta):
|
||||
verbose_name = "Photo"
|
||||
verbose_name_plural = "Photos"
|
||||
ordering = ["-created_at"]
|
||||
indexes = [
|
||||
models.Index(fields=["content_type", "object_id"]),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"Photo by {self.user.username} for {self.content_object}"
|
||||
62
backend/apps/media/serializers.py
Normal file
62
backend/apps/media/serializers.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Photo
|
||||
from apps.accounts.serializers import UserSerializer
|
||||
from django_cloudflareimages_toolkit.models import CloudflareImage
|
||||
|
||||
# We need a serializer for the CloudflareImage model too if we want to show variants
|
||||
class CloudflareImageSerializer(serializers.ModelSerializer):
|
||||
variants = serializers.JSONField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = CloudflareImage
|
||||
fields = ["id", "cloudflare_id", "variants"]
|
||||
|
||||
class PhotoSerializer(serializers.ModelSerializer):
|
||||
user = UserSerializer(read_only=True)
|
||||
image = CloudflareImageSerializer(read_only=True)
|
||||
cloudflare_image_id = serializers.CharField(write_only=True)
|
||||
|
||||
# Helper for frontend to get URLs easily
|
||||
url = serializers.SerializerMethodField()
|
||||
thumbnail = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Photo
|
||||
fields = [
|
||||
"id",
|
||||
"user",
|
||||
"image",
|
||||
"cloudflare_image_id",
|
||||
"content_type",
|
||||
"object_id",
|
||||
"caption",
|
||||
"source",
|
||||
"is_public",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"url",
|
||||
"thumbnail",
|
||||
]
|
||||
read_only_fields = ["id", "user", "created_at", "updated_at"]
|
||||
|
||||
def create(self, validated_data):
|
||||
cloudflare_id = validated_data.pop("cloudflare_image_id", None)
|
||||
if cloudflare_id:
|
||||
# Get or create the CloudflareImage wrapper
|
||||
# We assume it exists on CF side. We just need the DB record.
|
||||
image, _ = CloudflareImage.objects.get_or_create(cloudflare_id=cloudflare_id)
|
||||
validated_data["image"] = image
|
||||
|
||||
return super().create(validated_data)
|
||||
|
||||
def get_url(self, obj):
|
||||
# Return public variant or default
|
||||
if obj.image:
|
||||
# Check if get_url method exists or we construct strictly
|
||||
return getattr(obj.image, 'get_url', lambda x: None)('public')
|
||||
return None
|
||||
|
||||
def get_thumbnail(self, obj):
|
||||
if obj.image:
|
||||
return getattr(obj.image, 'get_url', lambda x: None)('thumbnail')
|
||||
return None
|
||||
10
backend/apps/media/urls.py
Normal file
10
backend/apps/media/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .views import PhotoViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r"photos", PhotoViewSet, basename="photo")
|
||||
|
||||
urlpatterns = [
|
||||
path("", include(router.urls)),
|
||||
]
|
||||
23
backend/apps/media/views.py
Normal file
23
backend/apps/media/views.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from rest_framework import viewsets, permissions, filters
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from .models import Photo
|
||||
from .serializers import PhotoSerializer
|
||||
from apps.core.permissions import IsOwnerOrReadOnly
|
||||
|
||||
class PhotoViewSet(viewsets.ModelViewSet):
|
||||
queryset = Photo.objects.filter(is_public=True)
|
||||
serializer_class = PhotoSerializer
|
||||
permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
|
||||
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
|
||||
filterset_fields = ["content_type", "object_id", "user"]
|
||||
ordering_fields = ["created_at"]
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def get_queryset(self):
|
||||
qs = Photo.objects.filter(is_public=True)
|
||||
if self.request.user.is_authenticated:
|
||||
qs = qs | Photo.objects.filter(user=self.request.user)
|
||||
return qs.distinct()
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
Reference in New Issue
Block a user