feat: Introduce lists and reviews apps, refactor user list functionality from accounts, and add user profile fields.

This commit is contained in:
pacnpal
2025-12-26 09:27:44 -05:00
parent ed04b30469
commit cd8868a591
37 changed files with 5900 additions and 281 deletions

View File

View File

@@ -0,0 +1,90 @@
from django.contrib import admin
from django.db.models import Count
from django.utils.html import format_html
from apps.core.admin import (
BaseModelAdmin,
ExportActionMixin,
QueryOptimizationMixin,
TimestampFieldsMixin,
)
from .models import UserList, ListItem
class ListItemInline(admin.TabularInline):
"""Inline admin for ListItem within UserList admin."""
model = ListItem
extra = 1
fields = ("content_type", "object_id", "rank", "notes")
ordering = ("rank",)
show_change_link = True
@admin.register(UserList)
class UserListAdmin(QueryOptimizationMixin, ExportActionMixin, TimestampFieldsMixin, BaseModelAdmin):
"""Admin interface for UserList."""
list_display = (
"title",
"user_link",
"category",
"is_public",
"item_count",
"created_at",
"updated_at",
)
list_filter = ("category", "is_public", "created_at", "updated_at")
list_select_related = ["user"]
list_prefetch_related = ["items"]
search_fields = ("title", "user__username", "description")
autocomplete_fields = ["user"]
inlines = [ListItemInline]
export_fields = ["id", "title", "user", "category", "is_public", "created_at", "updated_at"]
export_filename_prefix = "user_lists"
fieldsets = (
(
"Basic Information",
{
"fields": ("user", "title", "category", "description", "is_public"),
"description": "List identification and categorization.",
},
),
(
"Timestamps",
{
"fields": ("created_at", "updated_at"),
"classes": ("collapse",),
},
),
)
readonly_fields = ("created_at", "updated_at")
@admin.display(description="User")
def user_link(self, obj):
if obj.user:
from django.urls import reverse
url = reverse("admin:accounts_customuser_change", args=[obj.user.pk])
return format_html('<a href="{}">{}</a>', url, obj.user.username)
return "-"
@admin.display(description="Items")
def item_count(self, obj):
return obj.items.count()
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(_item_count=Count("items", distinct=True))
return qs
@admin.register(ListItem)
class ListItemAdmin(QueryOptimizationMixin, BaseModelAdmin):
"""Admin interface for ListItem."""
list_display = (
"user_list",
"content_type",
"object_id",
"rank",
)
list_filter = ("user_list__category", "content_type", "rank")
list_select_related = ["user_list", "user_list__user", "content_type"]

View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class ListsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.lists"

View File

@@ -0,0 +1,284 @@
# Generated by Django 5.1.6 on 2025-12-26 14:13
import apps.core.choices.fields
import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("pghistory", "0006_delete_aggregateevent"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name="ListItem",
fields=[
("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)),
("object_id", models.PositiveIntegerField(help_text="ID of the item")),
("rank", models.PositiveIntegerField(help_text="Position in the list")),
("notes", models.TextField(blank=True, help_text="User's notes about this item")),
(
"content_type",
models.ForeignKey(
help_text="Type of item (park, ride, etc.)",
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
],
options={
"verbose_name": "List Item",
"verbose_name_plural": "List Items",
"ordering": ["rank"],
"abstract": False,
},
),
migrations.CreateModel(
name="UserList",
fields=[
("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)),
("title", models.CharField(help_text="Title of the list", max_length=100)),
(
"category",
apps.core.choices.fields.RichChoiceField(
allow_deprecated=False,
choice_group="top_list_categories",
choices=[
("RC", "Roller Coaster"),
("DR", "Dark Ride"),
("FR", "Flat Ride"),
("WR", "Water Ride"),
("PK", "Park"),
],
domain="accounts",
help_text="Category of items in this list",
max_length=2,
),
),
("description", models.TextField(blank=True, help_text="Description of the list")),
("is_public", models.BooleanField(default=True, help_text="Whether this list is visible to others")),
(
"user",
models.ForeignKey(
help_text="User who created this list",
on_delete=django.db.models.deletion.CASCADE,
related_name="user_lists",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"verbose_name": "User List",
"verbose_name_plural": "User Lists",
"ordering": ["-updated_at"],
"abstract": False,
},
),
migrations.CreateModel(
name="ListItemEvent",
fields=[
("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()),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("object_id", models.PositiveIntegerField(help_text="ID of the item")),
("rank", models.PositiveIntegerField(help_text="Position in the list")),
("notes", models.TextField(blank=True, help_text="User's notes about this item")),
(
"content_type",
models.ForeignKey(
db_constraint=False,
help_text="Type of item (park, ride, etc.)",
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
related_query_name="+",
to="contenttypes.contenttype",
),
),
(
"pgh_context",
models.ForeignKey(
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="pghistory.context",
),
),
(
"pgh_obj",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="lists.listitem",
),
),
(
"user_list",
models.ForeignKey(
db_constraint=False,
help_text="List this item belongs to",
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
related_query_name="+",
to="lists.userlist",
),
),
],
options={
"abstract": False,
},
),
migrations.AddField(
model_name="listitem",
name="user_list",
field=models.ForeignKey(
help_text="List this item belongs to",
on_delete=django.db.models.deletion.CASCADE,
related_name="items",
to="lists.userlist",
),
),
migrations.CreateModel(
name="UserListEvent",
fields=[
("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()),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("title", models.CharField(help_text="Title of the list", max_length=100)),
(
"category",
apps.core.choices.fields.RichChoiceField(
allow_deprecated=False,
choice_group="top_list_categories",
choices=[
("RC", "Roller Coaster"),
("DR", "Dark Ride"),
("FR", "Flat Ride"),
("WR", "Water Ride"),
("PK", "Park"),
],
domain="accounts",
help_text="Category of items in this list",
max_length=2,
),
),
("description", models.TextField(blank=True, help_text="Description of the list")),
("is_public", models.BooleanField(default=True, help_text="Whether this list is visible to others")),
(
"pgh_context",
models.ForeignKey(
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="pghistory.context",
),
),
(
"pgh_obj",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="lists.userlist",
),
),
(
"user",
models.ForeignKey(
db_constraint=False,
help_text="User who created this list",
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
related_query_name="+",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"abstract": False,
},
),
pgtrigger.migrations.AddTrigger(
model_name="userlist",
trigger=pgtrigger.compiler.Trigger(
name="insert_insert",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "lists_userlistevent" ("category", "created_at", "description", "id", "is_public", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "title", "updated_at", "user_id") VALUES (NEW."category", NEW."created_at", NEW."description", NEW."id", NEW."is_public", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."title", NEW."updated_at", NEW."user_id"); RETURN NULL;',
hash="702082b0a9ed526aa1bffbec0839e9a2d7641f42",
operation="INSERT",
pgid="pgtrigger_insert_insert_7a128",
table="lists_userlist",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="userlist",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
func='INSERT INTO "lists_userlistevent" ("category", "created_at", "description", "id", "is_public", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "title", "updated_at", "user_id") VALUES (NEW."category", NEW."created_at", NEW."description", NEW."id", NEW."is_public", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."title", NEW."updated_at", NEW."user_id"); RETURN NULL;',
hash="843e25a795f48bb1dfbb3c5723598823a71e0da8",
operation="UPDATE",
pgid="pgtrigger_update_update_1d718",
table="lists_userlist",
when="AFTER",
),
),
),
migrations.AlterUniqueTogether(
name="listitem",
unique_together={("user_list", "rank")},
),
pgtrigger.migrations.AddTrigger(
model_name="listitem",
trigger=pgtrigger.compiler.Trigger(
name="insert_insert",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "lists_listitemevent" ("content_type_id", "created_at", "id", "notes", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rank", "updated_at", "user_list_id") VALUES (NEW."content_type_id", NEW."created_at", NEW."id", NEW."notes", NEW."object_id", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."rank", NEW."updated_at", NEW."user_list_id"); RETURN NULL;',
hash="09893103c0995cb295cdf83421583a93266593bb",
operation="INSERT",
pgid="pgtrigger_insert_insert_bb169",
table="lists_listitem",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="listitem",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
func='INSERT INTO "lists_listitemevent" ("content_type_id", "created_at", "id", "notes", "object_id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rank", "updated_at", "user_list_id") VALUES (NEW."content_type_id", NEW."created_at", NEW."id", NEW."notes", NEW."object_id", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."rank", NEW."updated_at", NEW."user_list_id"); RETURN NULL;',
hash="5617f50c7404a18a24f08bd237aecd466b496339",
operation="UPDATE",
pgid="pgtrigger_update_update_2b5a0",
table="lists_listitem",
when="AFTER",
),
),
),
]

View File

@@ -0,0 +1,61 @@
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
from apps.core.choices import RichChoiceField
import pghistory
@pghistory.track()
class UserList(TrackedModel):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="user_lists",
help_text="User who created this list",
)
title = models.CharField(max_length=100, help_text="Title of the list")
category = RichChoiceField(
choice_group="top_list_categories",
domain="accounts",
max_length=2,
help_text="Category of items in this list",
)
description = models.TextField(blank=True, help_text="Description of the list")
is_public = models.BooleanField(default=True, help_text="Whether this list is visible to others")
class Meta(TrackedModel.Meta):
verbose_name = "User List"
verbose_name_plural = "User Lists"
ordering = ["-updated_at"]
def __str__(self):
return f"{self.user.username}'s {self.category} List: {self.title}"
@pghistory.track()
class ListItem(TrackedModel):
user_list = models.ForeignKey(
UserList,
on_delete=models.CASCADE,
related_name="items",
help_text="List this item belongs to",
)
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
help_text="Type of item (park, ride, etc.)",
)
object_id = models.PositiveIntegerField(help_text="ID of the item")
content_object = GenericForeignKey("content_type", "object_id")
rank = models.PositiveIntegerField(help_text="Position in the list")
notes = models.TextField(blank=True, help_text="User's notes about this item")
class Meta(TrackedModel.Meta):
verbose_name = "List Item"
verbose_name_plural = "List Items"
ordering = ["rank"]
unique_together = [["user_list", "rank"]]
def __str__(self):
return f"#{self.rank} in {self.user_list.title}"

View File

@@ -0,0 +1,57 @@
from rest_framework import serializers
from .models import UserList, ListItem
from apps.accounts.serializers import UserSerializer
class ListItemSerializer(serializers.ModelSerializer):
class Meta:
model = ListItem
fields = [
"id",
"user_list",
"content_type",
"object_id",
"rank",
"notes",
"created_at",
"updated_at",
"content_object_data",
]
read_only_fields = ["id", "created_at", "updated_at"]
content_object_data = serializers.SerializerMethodField()
def get_content_object_data(self, obj):
"""
Return serialized data for the content object (Park or Ride).
"""
# Avoid circular imports
from apps.api.v1.parks.serializers import ParkListSerializer
from apps.api.v1.rides.serializers import RideListSerializer
from apps.parks.models import Park
from apps.rides.models import Ride
if isinstance(obj.content_object, Park):
return ParkListSerializer(obj.content_object, context=self.context).data
elif isinstance(obj.content_object, Ride):
return RideListSerializer(obj.content_object, context=self.context).data
return None
class UserListSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
items = ListItemSerializer(many=True, read_only=True)
class Meta:
model = UserList
fields = [
"id",
"user",
"title",
"category",
"description",
"is_public",
"items",
"created_at",
"updated_at",
]
read_only_fields = ["id", "user", "created_at", "updated_at"]

View File

@@ -0,0 +1,11 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import UserListViewSet, ListItemViewSet
router = DefaultRouter()
router.register(r"lists", UserListViewSet, basename="list")
router.register(r"list-items", ListItemViewSet, basename="list-item")
urlpatterns = [
path("", include(router.urls)),
]

View File

@@ -0,0 +1,28 @@
from django.db.models import Q
from rest_framework import viewsets, permissions
from .models import UserList, ListItem
from .serializers import UserListSerializer, ListItemSerializer
from apps.core.permissions import IsOwnerOrReadOnly
class UserListViewSet(viewsets.ModelViewSet):
serializer_class = UserListSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
lookup_field = "id"
def get_queryset(self):
# Users can see their own lists and public lists
if self.request.user.is_authenticated:
return UserList.objects.filter(Q(is_public=True) | Q(user=self.request.user))
return UserList.objects.filter(is_public=True)
def perform_create(self, serializer):
serializer.save(user=self.request.user)
class ListItemViewSet(viewsets.ModelViewSet):
serializer_class = ListItemSerializer
permission_classes = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]
lookup_field = "id"
def get_queryset(self):
return ListItem.objects.filter(user_list__is_public=True) | ListItem.objects.filter(user_list__user=self.request.user)