mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-22 01:11:09 -05:00
Add operators and property owners functionality
- Implemented OperatorListView and OperatorDetailView for managing operators. - Created corresponding templates for operator listing and detail views. - Added PropertyOwnerListView and PropertyOwnerDetailView for managing property owners. - Developed templates for property owner listing and detail views. - Established relationships between parks and operators, and parks and property owners in the models. - Created migrations to reflect the new relationships and fields in the database. - Added admin interfaces for PropertyOwner management. - Implemented tests for operators and property owners.
This commit is contained in:
0
operators/__init__.py
Normal file
0
operators/__init__.py
Normal file
14
operators/admin.py
Normal file
14
operators/admin.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.contrib import admin
|
||||
from .models import Operator
|
||||
|
||||
|
||||
class OperatorAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'headquarters', 'founded_year', 'parks_count', 'rides_count', 'created_at', 'updated_at')
|
||||
list_filter = ('founded_year',)
|
||||
search_fields = ('name', 'description', 'headquarters')
|
||||
readonly_fields = ('created_at', 'updated_at', 'parks_count', 'rides_count')
|
||||
prepopulated_fields = {'slug': ('name',)}
|
||||
|
||||
|
||||
# Register the model with admin
|
||||
admin.site.register(Operator, OperatorAdmin)
|
||||
6
operators/apps.py
Normal file
6
operators/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class OperatorsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'operators'
|
||||
119
operators/migrations/0001_initial.py
Normal file
119
operators/migrations/0001_initial.py
Normal file
@@ -0,0 +1,119 @@
|
||||
# Generated by Django 5.1.4 on 2025-07-04 14:50
|
||||
|
||||
import django.db.models.deletion
|
||||
import pgtrigger.compiler
|
||||
import pgtrigger.migrations
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("pghistory", "0006_delete_aggregateevent"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Operator",
|
||||
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)),
|
||||
("name", models.CharField(max_length=255)),
|
||||
("slug", models.SlugField(max_length=255, unique=True)),
|
||||
("description", models.TextField(blank=True)),
|
||||
("website", models.URLField(blank=True)),
|
||||
("founded_year", models.PositiveIntegerField(blank=True, null=True)),
|
||||
("headquarters", models.CharField(blank=True, max_length=255)),
|
||||
("parks_count", models.IntegerField(default=0)),
|
||||
("rides_count", models.IntegerField(default=0)),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "Operator",
|
||||
"verbose_name_plural": "Operators",
|
||||
"ordering": ["name"],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="OperatorEvent",
|
||||
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)),
|
||||
("name", models.CharField(max_length=255)),
|
||||
("slug", models.SlugField(db_index=False, max_length=255)),
|
||||
("description", models.TextField(blank=True)),
|
||||
("website", models.URLField(blank=True)),
|
||||
("founded_year", models.PositiveIntegerField(blank=True, null=True)),
|
||||
("headquarters", models.CharField(blank=True, max_length=255)),
|
||||
("parks_count", models.IntegerField(default=0)),
|
||||
("rides_count", models.IntegerField(default=0)),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
pgtrigger.migrations.AddTrigger(
|
||||
model_name="operator",
|
||||
trigger=pgtrigger.compiler.Trigger(
|
||||
name="insert_insert",
|
||||
sql=pgtrigger.compiler.UpsertTriggerSql(
|
||||
func='INSERT INTO "operators_operatorevent" ("created_at", "description", "founded_year", "headquarters", "id", "name", "parks_count", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rides_count", "slug", "updated_at", "website") VALUES (NEW."created_at", NEW."description", NEW."founded_year", NEW."headquarters", NEW."id", NEW."name", NEW."parks_count", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."rides_count", NEW."slug", NEW."updated_at", NEW."website"); RETURN NULL;',
|
||||
hash="[AWS-SECRET-REMOVED]",
|
||||
operation="INSERT",
|
||||
pgid="pgtrigger_insert_insert_504a1",
|
||||
table="operators_operator",
|
||||
when="AFTER",
|
||||
),
|
||||
),
|
||||
),
|
||||
pgtrigger.migrations.AddTrigger(
|
||||
model_name="operator",
|
||||
trigger=pgtrigger.compiler.Trigger(
|
||||
name="update_update",
|
||||
sql=pgtrigger.compiler.UpsertTriggerSql(
|
||||
condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
|
||||
func='INSERT INTO "operators_operatorevent" ("created_at", "description", "founded_year", "headquarters", "id", "name", "parks_count", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "rides_count", "slug", "updated_at", "website") VALUES (NEW."created_at", NEW."description", NEW."founded_year", NEW."headquarters", NEW."id", NEW."name", NEW."parks_count", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."rides_count", NEW."slug", NEW."updated_at", NEW."website"); RETURN NULL;',
|
||||
hash="[AWS-SECRET-REMOVED]",
|
||||
operation="UPDATE",
|
||||
pgid="pgtrigger_update_update_a7fb6",
|
||||
table="operators_operator",
|
||||
when="AFTER",
|
||||
),
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="operatorevent",
|
||||
name="pgh_context",
|
||||
field=models.ForeignKey(
|
||||
db_constraint=False,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.DO_NOTHING,
|
||||
related_name="+",
|
||||
to="pghistory.context",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="operatorevent",
|
||||
name="pgh_obj",
|
||||
field=models.ForeignKey(
|
||||
db_constraint=False,
|
||||
on_delete=django.db.models.deletion.DO_NOTHING,
|
||||
related_name="events",
|
||||
to="operators.operator",
|
||||
),
|
||||
),
|
||||
]
|
||||
0
operators/migrations/__init__.py
Normal file
0
operators/migrations/__init__.py
Normal file
65
operators/models.py
Normal file
65
operators/models.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from django.db import models
|
||||
from django.utils.text import slugify
|
||||
from django.urls import reverse
|
||||
from typing import Tuple, Optional, ClassVar, TYPE_CHECKING
|
||||
import pghistory
|
||||
from history_tracking.models import TrackedModel, HistoricalSlug
|
||||
|
||||
@pghistory.track()
|
||||
class Operator(TrackedModel):
|
||||
"""
|
||||
Companies that operate theme parks (replaces Company.owner)
|
||||
"""
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.SlugField(max_length=255, unique=True)
|
||||
description = models.TextField(blank=True)
|
||||
website = models.URLField(blank=True)
|
||||
founded_year = models.PositiveIntegerField(blank=True, null=True)
|
||||
headquarters = models.CharField(max_length=255, blank=True)
|
||||
parks_count = models.IntegerField(default=0)
|
||||
rides_count = models.IntegerField(default=0)
|
||||
|
||||
objects: ClassVar[models.Manager['Operator']]
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
verbose_name = 'Operator'
|
||||
verbose_name_plural = 'Operators'
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs) -> None:
|
||||
if not self.slug:
|
||||
self.slug = slugify(self.name)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_absolute_url(self) -> str:
|
||||
return reverse('operators:detail', kwargs={'slug': self.slug})
|
||||
|
||||
@classmethod
|
||||
def get_by_slug(cls, slug: str) -> Tuple['Operator', bool]:
|
||||
"""Get operator by slug, checking historical slugs if needed"""
|
||||
try:
|
||||
return cls.objects.get(slug=slug), False
|
||||
except cls.DoesNotExist:
|
||||
# Check pghistory first
|
||||
history_model = cls.get_history_model()
|
||||
history_entry = (
|
||||
history_model.objects.filter(slug=slug)
|
||||
.order_by('-pgh_created_at')
|
||||
.first()
|
||||
)
|
||||
|
||||
if history_entry:
|
||||
return cls.objects.get(id=history_entry.pgh_obj_id), True
|
||||
|
||||
# Check manual slug history as fallback
|
||||
try:
|
||||
historical = HistoricalSlug.objects.get(
|
||||
content_type__model='operator',
|
||||
slug=slug
|
||||
)
|
||||
return cls.objects.get(pk=historical.object_id), True
|
||||
except (HistoricalSlug.DoesNotExist, cls.DoesNotExist):
|
||||
raise cls.DoesNotExist()
|
||||
3
operators/tests.py
Normal file
3
operators/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
10
operators/urls.py
Normal file
10
operators/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
app_name = "operators"
|
||||
|
||||
urlpatterns = [
|
||||
# Operator list and detail views
|
||||
path("", views.OperatorListView.as_view(), name="operator_list"),
|
||||
path("<slug:slug>/", views.OperatorDetailView.as_view(), name="operator_detail"),
|
||||
]
|
||||
43
operators/views.py
Normal file
43
operators/views.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from django.views.generic import ListView, DetailView
|
||||
from django.db.models import QuerySet
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from core.views import SlugRedirectMixin
|
||||
from .models import Operator
|
||||
from typing import Optional, Any, Dict
|
||||
|
||||
|
||||
class OperatorListView(ListView):
|
||||
model = Operator
|
||||
template_name = "operators/operator_list.html"
|
||||
context_object_name = "operators"
|
||||
paginate_by = 20
|
||||
|
||||
def get_queryset(self) -> QuerySet[Operator]:
|
||||
return Operator.objects.all().order_by('name')
|
||||
|
||||
|
||||
class OperatorDetailView(SlugRedirectMixin, DetailView):
|
||||
model = Operator
|
||||
template_name = "operators/operator_detail.html"
|
||||
context_object_name = "operator"
|
||||
|
||||
def get_object(self, queryset: Optional[QuerySet[Operator]] = None) -> Operator:
|
||||
if queryset is None:
|
||||
queryset = self.get_queryset()
|
||||
slug = self.kwargs.get(self.slug_url_kwarg)
|
||||
if slug is None:
|
||||
raise ObjectDoesNotExist("No slug provided")
|
||||
operator, _ = Operator.get_by_slug(slug)
|
||||
return operator
|
||||
|
||||
def get_queryset(self) -> QuerySet[Operator]:
|
||||
return Operator.objects.all()
|
||||
|
||||
def get_context_data(self, **kwargs) -> Dict[str, Any]:
|
||||
context = super().get_context_data(**kwargs)
|
||||
operator = self.get_object()
|
||||
|
||||
# Add related parks to context (using related_name="parks" from Park model)
|
||||
context['parks'] = operator.parks.all().order_by('name')
|
||||
|
||||
return context
|
||||
Reference in New Issue
Block a user