Switch PROTECT to CASCADE on module-related models

Relates to MS-830
This commit is contained in:
Ramon Wenger 2024-04-09 15:29:13 +02:00
parent 86d48f5456
commit 496ff5bc16
3 changed files with 32 additions and 38 deletions

View File

@ -23,7 +23,7 @@ class Assignment(index.Indexed, TimeStampedModel, GraphqlNodeMixin):
get_user_model(), on_delete=models.PROTECT, null=True, blank=True get_user_model(), on_delete=models.PROTECT, null=True, blank=True
) # probably don't want to delete all assignments if a user gets deleted ) # probably don't want to delete all assignments if a user gets deleted
module = models.ForeignKey( module = models.ForeignKey(
"books.Module", related_name="assignments", on_delete=models.PROTECT "books.Module", related_name="assignments", on_delete=models.CASCADE
) )
user_created = models.BooleanField(default=False) user_created = models.BooleanField(default=False)
taskbase_id = models.CharField(max_length=255, null=True, blank=True) taskbase_id = models.CharField(max_length=255, null=True, blank=True)

View File

@ -15,15 +15,12 @@ class ChapterSnapshot(models.Model):
Captures the state of a chapter at the time when the snapshot was taken, for the school class that was selected Captures the state of a chapter at the time when the snapshot was taken, for the school class that was selected
for the user creating the snapshot for the user creating the snapshot
""" """
chapter = models.ForeignKey( chapter = models.ForeignKey(
'books.Chapter', "books.Chapter", related_name="chapter_snapshots", on_delete=models.CASCADE
related_name='chapter_snapshots',
on_delete=models.PROTECT
) )
snapshot = models.ForeignKey( snapshot = models.ForeignKey(
'books.Snapshot', "books.Snapshot", related_name="chapter_snapshots", on_delete=models.CASCADE
related_name='chapter_snapshots',
on_delete=models.CASCADE
) )
title_hidden = models.BooleanField(default=False) title_hidden = models.BooleanField(default=False)
description_hidden = models.BooleanField(default=False) description_hidden = models.BooleanField(default=False)
@ -31,13 +28,12 @@ class ChapterSnapshot(models.Model):
class ObjectiveGroupSnapshot(models.Model): class ObjectiveGroupSnapshot(models.Model):
objective_group = models.ForeignKey( objective_group = models.ForeignKey(
'objectives.ObjectiveGroup', "objectives.ObjectiveGroup", on_delete=models.CASCADE
on_delete=models.CASCADE
) )
snapshot = models.ForeignKey( snapshot = models.ForeignKey(
'books.Snapshot', "books.Snapshot",
related_name='objective_group_snapshots', related_name="objective_group_snapshots",
on_delete=models.CASCADE on_delete=models.CASCADE,
) )
hidden = models.BooleanField(default=False) hidden = models.BooleanField(default=False)
@ -50,10 +46,16 @@ class SnapshotManager(models.Manager):
ChapterSnapshot.objects.create( ChapterSnapshot.objects.create(
chapter=chapter, chapter=chapter,
snapshot=snapshot, snapshot=snapshot,
title_hidden=chapter.title_hidden_for.filter(id=school_class.id).exists(), title_hidden=chapter.title_hidden_for.filter(
description_hidden=chapter.description_hidden_for.filter(id=school_class.id).exists() id=school_class.id
).exists(),
description_hidden=chapter.description_hidden_for.filter(
id=school_class.id
).exists(),
)
base_qs = ContentBlock.get_by_parent(chapter).filter(
contentblocksnapshot__isnull=True
) )
base_qs = ContentBlock.get_by_parent(chapter).filter(contentblocksnapshot__isnull=True)
# Verlagsinhalte # Verlagsinhalte
for content_block in base_qs.filter(user_created=False): for content_block in base_qs.filter(user_created=False):
if content_block.hidden_for.filter(id=school_class.id).exists(): if content_block.hidden_for.filter(id=school_class.id).exists():
@ -66,9 +68,9 @@ class SnapshotManager(models.Manager):
contents=content_block.contents, contents=content_block.contents,
type=content_block.type, type=content_block.type,
title=content_block.title, title=content_block.title,
original_creator=content_block.owner original_creator=content_block.owner,
) )
content_block.add_sibling(instance=new_content_block, pos='right') content_block.add_sibling(instance=new_content_block, pos="right")
revision = new_content_block.save_revision() revision = new_content_block.save_revision()
revision.publish() revision.publish()
new_content_block.save() new_content_block.save()
@ -91,7 +93,7 @@ class SnapshotManager(models.Manager):
snapshot=snapshot, snapshot=snapshot,
text=objective.text, text=objective.text,
group=objective_group, group=objective_group,
order=objective.order order=objective.order,
) )
return snapshot return snapshot
@ -99,36 +101,27 @@ class SnapshotManager(models.Manager):
class Snapshot(models.Model): class Snapshot(models.Model):
module = models.ForeignKey( module = models.ForeignKey(
'books.Module', "books.Module", on_delete=models.CASCADE, related_name="snapshots"
on_delete=models.PROTECT,
related_name='snapshots'
)
chapters = models.ManyToManyField(
'books.Chapter',
through=ChapterSnapshot
) )
chapters = models.ManyToManyField("books.Chapter", through=ChapterSnapshot)
hidden_content_blocks = models.ManyToManyField( hidden_content_blocks = models.ManyToManyField(
'books.ContentBlock', "books.ContentBlock", related_name="hidden_for_snapshots"
related_name='hidden_for_snapshots'
) )
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True) creator = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True)
shared = models.BooleanField(default=False) shared = models.BooleanField(default=False)
objective_groups = models.ManyToManyField( objective_groups = models.ManyToManyField(
'objectives.ObjectiveGroup', "objectives.ObjectiveGroup", through=ObjectiveGroupSnapshot, related_name="+"
through=ObjectiveGroupSnapshot,
related_name='+'
) )
hidden_objectives = models.ManyToManyField( hidden_objectives = models.ManyToManyField(
'objectives.Objective', "objectives.Objective", related_name="hidden_for_snapshots"
related_name='hidden_for_snapshots'
) )
title = models.CharField(max_length=255, blank=True, null=True) title = models.CharField(max_length=255, blank=True, null=True)
objects = SnapshotManager() objects = SnapshotManager()
def __str__(self): def __str__(self):
return f'Snapshot {self.id}' return f"Snapshot {self.id}"
def reset(self, user: User, selected_class: SchoolClass): def reset(self, user: User, selected_class: SchoolClass):
for chapter in Chapter.get_by_parent(self.module): for chapter in Chapter.get_by_parent(self.module):
@ -136,7 +129,7 @@ class Snapshot(models.Model):
default_content = Q(user_created=False) default_content = Q(user_created=False)
no_snapshot = Q(contentblocksnapshot__isnull=True) no_snapshot = Q(contentblocksnapshot__isnull=True)
owner_user = Q(owner=user) owner_user = Q(owner=user)
logger.info(f'resetting hidden properties for chapter {chapter.id}') logger.info(f"resetting hidden properties for chapter {chapter.id}")
chapter.title_hidden_for.remove(selected_class) chapter.title_hidden_for.remove(selected_class)
chapter.description_hidden_for.remove(selected_class) chapter.description_hidden_for.remove(selected_class)
@ -149,7 +142,9 @@ class Snapshot(models.Model):
for content_block in self.hidden_content_blocks.all(): for content_block in self.hidden_content_blocks.all():
content_block.hidden_for.add(selected_class) content_block.hidden_for.add(selected_class)
for custom_content_block in self.custom_content_blocks.all(): for custom_content_block in self.custom_content_blocks.all():
custom_content_block.to_regular_content_block(owner=user, school_class=selected_class) custom_content_block.to_regular_content_block(
owner=user, school_class=selected_class
)
for chapter_snapshot in ChapterSnapshot.objects.filter(snapshot=self): for chapter_snapshot in ChapterSnapshot.objects.filter(snapshot=self):
chapter = chapter_snapshot.chapter chapter = chapter_snapshot.chapter
if chapter_snapshot.title_hidden: if chapter_snapshot.title_hidden:

View File

@ -3,7 +3,6 @@ from django.db import models
from django.db.models import JSONField from django.db.models import JSONField
from wagtail.snippets.models import register_snippet from wagtail.snippets.models import register_snippet
from wagtail.search import index from wagtail.search import index
from modelcluster.fields import ParentalKey
from core.mixins import GraphqlNodeMixin from core.mixins import GraphqlNodeMixin
@ -14,7 +13,7 @@ class Survey(models.Model, index.Indexed, GraphqlNodeMixin):
module = models.ForeignKey( module = models.ForeignKey(
"books.Module", "books.Module",
related_name="surveys", related_name="surveys",
on_delete=models.PROTECT, on_delete=models.CASCADE,
null=True, null=True,
blank=True, blank=True,
) )
@ -41,7 +40,7 @@ class Answer(models.Model):
get_user_model(), on_delete=models.CASCADE, related_name="answers" get_user_model(), on_delete=models.CASCADE, related_name="answers"
) )
data = JSONField() data = JSONField()
survey = models.ForeignKey(Survey, on_delete=models.PROTECT, related_name="answers") survey = models.ForeignKey(Survey, on_delete=models.CASCADE, related_name="answers")
def __str__(self): def __str__(self):
return "{} - {}".format(self.owner.username, self.survey.title) return "{} - {}".format(self.owner.username, self.survey.title)