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
) # probably don't want to delete all assignments if a user gets deleted
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)
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
for the user creating the snapshot
"""
chapter = models.ForeignKey(
'books.Chapter',
related_name='chapter_snapshots',
on_delete=models.PROTECT
"books.Chapter", related_name="chapter_snapshots", on_delete=models.CASCADE
)
snapshot = models.ForeignKey(
'books.Snapshot',
related_name='chapter_snapshots',
on_delete=models.CASCADE
"books.Snapshot", related_name="chapter_snapshots", on_delete=models.CASCADE
)
title_hidden = models.BooleanField(default=False)
description_hidden = models.BooleanField(default=False)
@ -31,13 +28,12 @@ class ChapterSnapshot(models.Model):
class ObjectiveGroupSnapshot(models.Model):
objective_group = models.ForeignKey(
'objectives.ObjectiveGroup',
on_delete=models.CASCADE
"objectives.ObjectiveGroup", on_delete=models.CASCADE
)
snapshot = models.ForeignKey(
'books.Snapshot',
related_name='objective_group_snapshots',
on_delete=models.CASCADE
"books.Snapshot",
related_name="objective_group_snapshots",
on_delete=models.CASCADE,
)
hidden = models.BooleanField(default=False)
@ -50,10 +46,16 @@ class SnapshotManager(models.Manager):
ChapterSnapshot.objects.create(
chapter=chapter,
snapshot=snapshot,
title_hidden=chapter.title_hidden_for.filter(id=school_class.id).exists(),
description_hidden=chapter.description_hidden_for.filter(id=school_class.id).exists()
title_hidden=chapter.title_hidden_for.filter(
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
for content_block in base_qs.filter(user_created=False):
if content_block.hidden_for.filter(id=school_class.id).exists():
@ -66,9 +68,9 @@ class SnapshotManager(models.Manager):
contents=content_block.contents,
type=content_block.type,
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.publish()
new_content_block.save()
@ -91,7 +93,7 @@ class SnapshotManager(models.Manager):
snapshot=snapshot,
text=objective.text,
group=objective_group,
order=objective.order
order=objective.order,
)
return snapshot
@ -99,36 +101,27 @@ class SnapshotManager(models.Manager):
class Snapshot(models.Model):
module = models.ForeignKey(
'books.Module',
on_delete=models.PROTECT,
related_name='snapshots'
)
chapters = models.ManyToManyField(
'books.Chapter',
through=ChapterSnapshot
"books.Module", on_delete=models.CASCADE, related_name="snapshots"
)
chapters = models.ManyToManyField("books.Chapter", through=ChapterSnapshot)
hidden_content_blocks = models.ManyToManyField(
'books.ContentBlock',
related_name='hidden_for_snapshots'
"books.ContentBlock", related_name="hidden_for_snapshots"
)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True)
shared = models.BooleanField(default=False)
objective_groups = models.ManyToManyField(
'objectives.ObjectiveGroup',
through=ObjectiveGroupSnapshot,
related_name='+'
"objectives.ObjectiveGroup", through=ObjectiveGroupSnapshot, related_name="+"
)
hidden_objectives = models.ManyToManyField(
'objectives.Objective',
related_name='hidden_for_snapshots'
"objectives.Objective", related_name="hidden_for_snapshots"
)
title = models.CharField(max_length=255, blank=True, null=True)
objects = SnapshotManager()
def __str__(self):
return f'Snapshot {self.id}'
return f"Snapshot {self.id}"
def reset(self, user: User, selected_class: SchoolClass):
for chapter in Chapter.get_by_parent(self.module):
@ -136,7 +129,7 @@ class Snapshot(models.Model):
default_content = Q(user_created=False)
no_snapshot = Q(contentblocksnapshot__isnull=True)
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.description_hidden_for.remove(selected_class)
@ -149,7 +142,9 @@ class Snapshot(models.Model):
for content_block in self.hidden_content_blocks.all():
content_block.hidden_for.add(selected_class)
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):
chapter = chapter_snapshot.chapter
if chapter_snapshot.title_hidden:

View File

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