Skip to content

Commit 410b416

Browse files
authored
Merge pull request #1564 from openstax/content-warning
add content warning field to book
2 parents d5e51c7 + d8ae3c3 commit 410b416

File tree

7 files changed

+109
-11
lines changed

7 files changed

+109
-11
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 5.0.6 on 2024-06-26 16:58
2+
3+
import django.db.models.deletion
4+
from django.db import migrations, models
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("books", "0155_alter_book_book_cover_text_color_and_more"),
10+
("snippets", "0036_contentwarning"),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name="book",
16+
name="content_warning",
17+
field=models.ForeignKey(
18+
blank=True,
19+
help_text="Message shown in the content warning modal.",
20+
null=True,
21+
on_delete=django.db.models.deletion.SET_NULL,
22+
related_name="content_warnings_content_warning",
23+
to="snippets.contentwarning",
24+
),
25+
),
26+
]

books/models.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,14 @@ class Book(Page):
551551
description = RichTextField(
552552
blank=True, help_text="Description shown on Book Detail page.")
553553

554+
content_warning = models.ForeignKey(
555+
snippets.ContentWarning,
556+
on_delete=models.SET_NULL,
557+
null=True,
558+
blank=True,
559+
related_name='content_warnings_content_warning',
560+
help_text="Message shown in the content warning modal.")
561+
554562
cover = models.ForeignKey(
555563
'wagtaildocs.Document',
556564
null=True, blank=True,
@@ -731,7 +739,7 @@ def get_community_resource_feature_link_url(self):
731739
support_statement = models.TextField(blank=True, null=True,
732740
default="With philanthropic support, this book is used in <span id='adoption_number'></span> classrooms, saving students <span id='savings'></span> dollars this school year. <a href='/impact'>Learn more about our impact</a> and how you can help.",
733741
help_text="Updating this statement updates it for all book pages.")
734-
742+
735743
promote_snippet = StreamField(PromoteSnippetBlock(), null=True, blank=True, use_json_field=True)
736744

737745
videos = StreamField([
@@ -773,6 +781,7 @@ def get_community_resource_feature_link_url(self):
773781
InlinePanel('k12book_subjects', label='K12 Subjects'),
774782
FieldPanel('is_ap'),
775783
FieldPanel('description', classname="full"),
784+
FieldPanel('content_warning'),
776785
FieldPanel('cover'),
777786
FieldPanel('title_image'),
778787
FieldPanel('cover_color'),
@@ -870,6 +879,7 @@ def get_community_resource_feature_link_url(self):
870879
APIField('k12book_subjects'),
871880
APIField('is_ap'),
872881
APIField('description'),
882+
APIField('content_warning_text'),
873883
APIField('cover_url'),
874884
APIField('title_image_url'),
875885
APIField('cover_color'),
@@ -978,6 +988,10 @@ def errata_content(self):
978988
return snippets.ErrataContent.objects.filter(locale=self.locale).first().content
979989
return snippets.ErrataContent.objects.filter(book_state=self.book_state, locale=self.locale).first().content
980990

991+
@property
992+
def content_warning_text(self):
993+
return self.content_warning.content_warning if self.content_warning else None
994+
981995
def get_slug(self):
982996
return 'books/{}'.format(self.slug)
983997

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Generated by Django 5.0.6 on 2024-06-25 18:22
2+
3+
import django.db.models.deletion
4+
import uuid
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
("snippets", "0035_alter_k12subject_subject_color_and_more"),
12+
("wagtailcore", "0089_log_entry_data_json_null_to_object"),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name="ContentWarning",
18+
fields=[
19+
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
20+
("translation_key", models.UUIDField(default=uuid.uuid4, editable=False)),
21+
("content_warning", models.TextField()),
22+
(
23+
"locale",
24+
models.ForeignKey(
25+
editable=False,
26+
on_delete=django.db.models.deletion.PROTECT,
27+
related_name="+",
28+
to="wagtailcore.locale",
29+
),
30+
),
31+
],
32+
options={
33+
"abstract": False,
34+
"unique_together": {("translation_key", "locale")},
35+
},
36+
),
37+
]

snippets/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,19 @@ def __str__(self):
485485

486486

487487
register_snippet(AmazonBookBlurb)
488+
489+
class ContentWarning(TranslatableMixin, models.Model):
490+
content_warning = models.TextField()
491+
492+
api_fields = ('content_warning')
493+
494+
panels = [
495+
FieldPanel('content_warning')
496+
]
497+
498+
def __str__(self):
499+
return (self.content_warning[:100] + '...') if len(self.content_warning) > 100 else self.content_warning
500+
501+
502+
503+
register_snippet(ContentWarning)

snippets/serializers.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class K12SubjectSerializer(serializers.ModelSerializer):
3939
class Meta:
4040
model = K12Subject
4141
fields = ('id',
42-
'name',
43-
'intro_text',
44-
'subject_image',
45-
'subject_category' ,
46-
'subject_color',
42+
'name',
43+
'intro_text',
44+
'subject_image',
45+
'subject_category' ,
46+
'subject_color',
4747
'subject_link'
4848
)
4949
read_only_fields = ('id',
@@ -132,5 +132,3 @@ class Meta:
132132
model = AmazonBookBlurb
133133
fields = ('amazon_book_blurb',)
134134
read_only_fields = ('amazon_book_blurb',)
135-
136-

snippets/signals.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.dispatch import receiver
33

44
from global_settings.functions import invalidate_cloudfront_caches
5-
from snippets.models import Subject, Role, ErrataContent, SubjectCategory, GiveBanner, BlogContentType, BlogCollection, \
5+
from snippets.models import ContentWarning, Subject, Role, ErrataContent, SubjectCategory, GiveBanner, BlogContentType, BlogCollection, \
66
WebinarCollection, AmazonBookBlurb, PromoteSnippet
77

88

@@ -53,3 +53,6 @@ def clear_cloudfront_on_assignable_available_save(sender, **kwargs):
5353
def clear_cloudfront_on_amazon_book_blurb_save(sender, **kwargs):
5454
invalidate_cloudfront_caches('snippets/amazonbookblurb')
5555

56+
@receiver(post_save, sender=ContentWarning)
57+
def clear_cloudfront_on_content_warning_save(sender, **kwargs):
58+
invalidate_cloudfront_caches('snippets/contentwarning')

snippets/tests.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.conf import settings
77
from django.urls import reverse
88

9-
from snippets.models import Subject, ErrataContent, GiveBanner, BlogContentType, NoWebinarMessage, K12Subject, \
9+
from snippets.models import ContentWarning, Subject, ErrataContent, GiveBanner, BlogContentType, NoWebinarMessage, K12Subject, \
1010
FacultyResource, StudentResource, Role, SharedContent, NewsSource, SubjectCategory, BlogCollection, \
1111
AmazonBookBlurb, PromoteSnippet
1212

@@ -72,6 +72,11 @@ def setUp(self):
7272
amazon_book_blurb="Amazon Book Blurb. Amazon Book Blurb. Amazon Book Blurb.")
7373
self.amazon_book_blurb.save()
7474

75+
self.content_warning = ContentWarning(
76+
content_warning = "Content Warning"
77+
)
78+
self.content_warning.save()
79+
7580

7681
def test_can_create_subject(self):
7782
subject = Subject(name="Science", page_content="Science page content.", seo_title="Science SEO Title",
@@ -151,4 +156,3 @@ def test_can_create_promote_snippet(self):
151156
promote_snippet.save()
152157

153158
self.assertEqual(promote_snippet.name, "Assignable")
154-

0 commit comments

Comments
 (0)