Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2m products topics #6050

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kitsune/products/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
class ProductsConfig(AppConfig):
name = "kitsune.products"
default_auto_field = "django.db.models.AutoField"

def ready(self):
from kitsune.products import signals # noqa
17 changes: 17 additions & 0 deletions kitsune/products/migrations/0007_remove_product_sprite_height.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.11 on 2024-06-06 06:31

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("products", "0006_alter_product_and_topic_images"),
]

operations = [
migrations.RemoveField(
model_name="product",
name="sprite_height",
),
]
18 changes: 18 additions & 0 deletions kitsune/products/migrations/0008_topic_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-06-06 06:31

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("products", "0007_remove_product_sprite_height"),
]

operations = [
migrations.AddField(
model_name="topic",
name="products",
field=models.ManyToManyField(related_name="m2m_topics", to="products.product"),
),
]
24 changes: 24 additions & 0 deletions kitsune/products/migrations/0009_auto_20240606_0632.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.11 on 2024-06-06 06:32

import copy

from django.db import migrations


def copy_product_to_products(apps, schema_editor):
Topic = apps.get_model("products", "Topic")
for topic in Topic.objects.all():
if topic.product:
topic.products.add(topic.product)


def m2m_backwards(apps, schema_editor): ...


class Migration(migrations.Migration):

dependencies = [
("products", "0008_topic_products"),
]

operations = [migrations.RunPython(copy_product_to_products, m2m_backwards)]
38 changes: 38 additions & 0 deletions kitsune/products/migrations/0010_topicslughistory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.11 on 2024-06-06 07:07

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("products", "0009_auto_20240606_0632"),
]

operations = [
migrations.CreateModel(
name="TopicSlugHistory",
fields=[
(
"id",
models.AutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("slug", models.CharField(max_length=255)),
("created", models.DateTimeField(auto_now_add=True)),
(
"topic",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="slug_history",
to="products.topic",
),
),
],
options={
"ordering": ["-created"],
},
),
]
11 changes: 10 additions & 1 deletion kitsune/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Product(ModelBase):
)
image_offset = models.IntegerField(default=None, null=True, editable=False)
image_cachebuster = models.CharField(max_length=32, default=None, null=True, editable=False)
sprite_height = models.IntegerField(default=None, null=True, editable=False)

# Dictates the order in which products are displayed in product
# lists.
Expand Down Expand Up @@ -96,6 +95,7 @@ class Topic(ModelBase):

# Topics are product-specific
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="topics")
products = models.ManyToManyField(Product, related_name="m2m_topics")

# Topics can optionally have a parent.
parent = models.ForeignKey(
Expand Down Expand Up @@ -169,6 +169,15 @@ def get_absolute_url(self):
)


class TopicSlugHistory(ModelBase):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name="slug_history")
slug = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)

class Meta(object):
ordering = ["-created"]


class Version(ModelBase):
name = models.CharField(max_length=255)
# We don't use a SlugField here because we want to allow dots.
Expand Down
12 changes: 12 additions & 0 deletions kitsune/products/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db.models.signals import pre_save
from django.dispatch import receiver

from kitsune.products.models import Topic


@receiver(pre_save, sender=Topic)
def update_topic_slug_history(sender, instance, **kwargs):
if instance.pk:
old_instance = sender.objects.get(pk=instance.pk)
if old_instance.slug != instance.slug:
TopicSlugHistory.objects.create(topic=instance, slug=old_instance.slug)