Skip to content
Merged
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
7 changes: 7 additions & 0 deletions books/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@
(RED, 'Red'),
(YELLOW, 'Yellow'),
)

CC_BY_LICENSE_NAME = 'Creative Commons Attribution License'
CC_BY_LICENSE_VERSION = '4.0'
CC_BY_LICENSE_URL = 'https://creativecommons.org/licenses/by/4.0/'
CC_NC_SA_LICENSE_NAME = 'Creative Commons Attribution-NonCommercial-ShareAlike License'
CC_NC_SA_LICENSE_VERSION = '4.0'
CC_NC_SA_LICENSE_URL = 'https://creativecommons.org/licenses/by-nc-sa/4.0/'
18 changes: 18 additions & 0 deletions books/management/commands/add_missing_licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.core.management.base import BaseCommand

from snippets.models import ContentLicense
from books.models import Book

class Command(BaseCommand):
help="add CC license to live books that do not have one"

def handle(self, *args, **options):
nc_sa_books = ['Cálculo volumen 1', 'Cálculo volumen 2', 'Cálculo volumen 3']
books = Book.objects.filter(license_name=None, book_state='live')
for book in books:
if book.book_title in nc_sa_books:
book.license_name = 'Creative Commons Attribution-NonCommercial-ShareAlike License'
else:
book.license_name = 'Creative Commons Attribution License'
book.save()
print('Updated license for ' + str(book.book_title))
18 changes: 18 additions & 0 deletions books/migrations/0134_alter_book_license_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2022-05-09 20:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('books', '0133_bookcategories_bookcategory'),
]

operations = [
migrations.AlterField(
model_name='book',
name='license_name',
field=models.CharField(blank=True, choices=[('Creative Commons Attribution License', 'Creative Commons Attribution License'), ('Creative Commons Attribution-NonCommercial-ShareAlike License', 'Creative Commons Attribution-NonCommercial-ShareAlike License')], help_text='Name of the license.', max_length=255, null=True),
),
]
18 changes: 18 additions & 0 deletions books/migrations/0135_alter_book_license_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2022-05-11 12:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('books', '0134_alter_book_license_name'),
]

operations = [
migrations.AlterField(
model_name='book',
name='license_name',
field=models.CharField(blank=True, choices=[('Creative Commons Attribution License', 'Creative Commons Attribution License'), ('Creative Commons Attribution-NonCommercial-ShareAlike License', 'Creative Commons Attribution-NonCommercial-ShareAlike License')], default='Creative Commons Attribution License', help_text='Name of the license.', max_length=255, null=True),
),
]
21 changes: 19 additions & 2 deletions books/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from wagtail.core.models import Site

from openstax.functions import build_document_url, build_image_url
from books.constants import BOOK_STATES, BOOK_COVER_TEXT_COLOR, COVER_COLORS
from books.constants import BOOK_STATES, BOOK_COVER_TEXT_COLOR, COVER_COLORS, CC_NC_SA_LICENSE_NAME, CC_BY_LICENSE_NAME, \
CC_BY_LICENSE_URL, CC_NC_SA_LICENSE_URL, CC_NC_SA_LICENSE_VERSION, CC_BY_LICENSE_VERSION
import snippets.models as snippets


Expand Down Expand Up @@ -451,6 +452,11 @@ class BookCategories(Orderable, BookCategory):


class Book(Page):
licenses = (
(CC_BY_LICENSE_NAME, CC_BY_LICENSE_NAME),
(CC_NC_SA_LICENSE_NAME, CC_NC_SA_LICENSE_NAME)
)

created = models.DateTimeField(auto_now_add=True)
book_state = models.CharField(max_length=255, choices=BOOK_STATES, default='live', help_text='The state of the book.')
cnx_id = models.CharField(
Expand Down Expand Up @@ -506,7 +512,7 @@ def get_title_image_url(self):
license_text = models.TextField(
blank=True, null=True, help_text="Overrides default license text.")
license_name = models.CharField(
max_length=255, blank=True, null=True, editable=False, help_text="Name of the license.")
max_length=255, blank=True, null=True, choices=licenses,default=CC_BY_LICENSE_NAME, help_text="Name of the license.")
license_version = models.CharField(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the other license fields not need similar choices?

max_length=255, blank=True, null=True, editable=False, help_text="Version of the license.")
license_url = models.CharField(
Expand Down Expand Up @@ -660,6 +666,7 @@ def get_community_resource_feature_link_url(self):
FieldPanel('ibook_volume_2_isbn_10'),
FieldPanel('ibook_volume_2_isbn_13'),
FieldPanel('license_text'),
FieldPanel('license_name'),
FieldPanel('webview_rex_link'),
FieldPanel('rex_callout_title'),
FieldPanel('rex_callout_blurb'),
Expand Down Expand Up @@ -881,6 +888,16 @@ def save(self, *args, **kwargs):
if self.support_statement:
Book.objects.filter(locale=self.locale).update(support_statement=self.support_statement)

# populate license
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see what you are doing - might want to set the other license fields to uneditable then editable=False

if self.license_name:
if self.license_name == CC_BY_LICENSE_NAME:
self.license_url = CC_BY_LICENSE_URL
self.license_version = CC_BY_LICENSE_VERSION
else:
self.license_url = CC_NC_SA_LICENSE_URL
self.license_version = CC_NC_SA_LICENSE_VERSION


# if book is new, clear out isbn 10 fields
if self._state.adding:
self.print_isbn_10 = None
Expand Down
20 changes: 20 additions & 0 deletions books/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from wagtail.tests.utils import WagtailPageTests
from wagtail.core.models import Page

import snippets.models
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import?

from pages.models import HomePage
from books.models import BookIndex, Book
from shared.test_utilities import assertPathDoesNotRedirectToTrailingSlash
Expand Down Expand Up @@ -117,3 +119,21 @@ def test_cannot_create_book_under_homepage(self):
def test_slashless_apis_are_good(self):
assertPathDoesNotRedirectToTrailingSlash(self, '/apps/cms/api/books')
assertPathDoesNotRedirectToTrailingSlash(self, '/apps/cms/api/books/slug')

def test_can_create_book_with_cc_license(self):
book_index = BookIndex.objects.all()[0]
root_page = Page.objects.get(title="Root")
book = Book(title="University Physics",
slug="university-physics",
cnx_id='031da8d3-b525-429c-80cf-6c8ed997733a',
salesforce_abbreviation='University Phys (Calc)',
salesforce_name='University Physics',
description="Test Book",
cover=self.test_doc,
title_image=self.test_doc,
publish_date=datetime.date.today(),
locale=root_page.locale,
license_name='Creative Commons Attribution License',
)
book_index.add_child(instance=book)
self.assertEqual(book.license_url, 'https://creativecommons.org/licenses/by/4.0/')
32 changes: 32 additions & 0 deletions snippets/migrations/0020_contentlicense.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.5 on 2022-05-09 15:39

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


class Migration(migrations.Migration):

dependencies = [
('wagtailcore', '0066_collection_management_permissions'),
('snippets', '0019_givebanner'),
]

operations = [
migrations.CreateModel(
name='ContentLicense',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('translation_key', models.UUIDField(default=uuid.uuid4, editable=False)),
('license_code', models.CharField(blank=True, max_length=255, null=True)),
('version', models.CharField(blank=True, max_length=255, null=True)),
('license_name', models.CharField(blank=True, max_length=255, null=True)),
('license_url', models.URLField(blank=True, null=True)),
('locale', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='wagtailcore.locale')),
],
options={
'abstract': False,
'unique_together': {('translation_key', 'locale')},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 3.2.5 on 2022-05-10 15:37

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('snippets', '0020_contentlicense'),
('snippets', '0021_blogcollection'),
]

operations = [
]
16 changes: 16 additions & 0 deletions snippets/migrations/0023_delete_contentlicense.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 3.2.5 on 2022-05-10 21:12

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('snippets', '0022_merge_0020_contentlicense_0021_blogcollection'),
]

operations = [
migrations.DeleteModel(
name='ContentLicense',
),
]
3 changes: 2 additions & 1 deletion snippets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,5 @@ def __str__(self):
return self.name


register_snippet(BlogCollection)
register_snippet(BlogCollection)

1 change: 1 addition & 0 deletions snippets/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ class Meta:
fields = ('name',
'description',
'collection_image')

3 changes: 2 additions & 1 deletion snippets/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from rest_framework import viewsets

from .models import Role, Subject, ErrataContent, SubjectCategory, GiveBanner, BlogContentType, BlogCollection
from .serializers import RoleSerializer, SubjectSerializer, ErrataContentSerializer, SubjectCategorySerializer, GiveBannerSerializer, BlogContentTypeSerializer, BlogCollectionSerializer
from .serializers import RoleSerializer, SubjectSerializer, ErrataContentSerializer, SubjectCategorySerializer, \
GiveBannerSerializer, BlogContentTypeSerializer, BlogCollectionSerializer

from rest_framework import generics, viewsets
from django_filters.rest_framework import DjangoFilterBackend
Expand Down