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

deprecating CC licenses in the CML #703

Merged
merged 6 commits into from
Apr 19, 2024
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
3 changes: 1 addition & 2 deletions django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ COPY ${REQUIREMENTS_FILE} requirements.txt /tmp/

# FIXME: change to container user
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
pip3 install -r /tmp/${REQUIREMENTS_FILE} \
&& touch /code/override.env
pip3 install -r /tmp/${REQUIREMENTS_FILE}

COPY ./deploy/cron.daily/* /etc/cron.daily/
COPY ./deploy/cron.hourly/* /etc/cron.hourly/
Expand Down
7 changes: 3 additions & 4 deletions django/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def create_markdown_email(
body: str = None,
from_email: str = settings.DEFAULT_FROM_EMAIL,
email_subject_prefix: str = settings.EMAIL_SUBJECT_PREFIX,
bcc=None,
**kwargs,
):
if all([template_name, context]):
Expand All @@ -77,15 +78,13 @@ def create_markdown_email(
if all(required_fields):
subject = f"{email_subject_prefix} {subject}"
email = EmailMultiAlternatives(
subject=subject, body=body, to=to, from_email=from_email, **kwargs
subject=subject, body=body, to=to, from_email=from_email, bcc=bcc, **kwargs
)
email.attach_alternative(markdown(body), "text/html")
return email
else:
raise ValueError(
"Ignoring request to create a markdown email with missing required content {}".format(
required_fields
)
f"Not creating markdown email due to missing required fields: {required_fields}"
)


Expand Down
105 changes: 105 additions & 0 deletions django/curator/management/commands/send_emails_cc_license_authors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse
from pathlib import Path

from core.utils import send_markdown_email
from library.models import CodebaseRelease
import logging

logger = logging.getLogger(__name__)

# temp file to track which user ids have been sent emails
# in case of failure, we can resume from where we left off
SENT_EMAILS_FILE_PATH = Path(settings.SHARE_DIR, "sent_cc_emails.txt")

CC_LICENSE_CHANGE_URL = settings.BASE_URL + reverse("library:cc-license-change")


class Command(BaseCommand):
help = """
Sends an email to all users who have submitted codebase releases
that are currently licensed under a Creative Commons license
"""

def add_arguments(self, parser):
parser.add_argument(
"--test-user-id",
type=int,
required=False,
help="If specified, only email the specified user about their releases",
)

def handle(self, *args, **options):
self.send_emails(test_user_id=options["test_user_id"])

def send_emails(self, test_user_id=None):
sent_user_ids = self._read_sent_user_ids()

# get all unique user ids that have submitted codebases with CC licenses
releases_with_cc = CodebaseRelease.objects.with_cc_license()
unique_submitter_ids = set(
releases_with_cc.values_list("submitter", flat=True).distinct()
if not test_user_id
else [test_user_id]
)
users = User.objects.filter(id__in=unique_submitter_ids)
user_emails = [user.email for user in users if user.id not in sent_user_ids]
try:
# send email, if successful, append user id to temp file to mark as sent
if user_emails:
send_markdown_email(
to=[settings.EDITOR_EMAIL],
subject="CoMSES Net Codebase License Change",
body=self._get_email_body(),
bcc=[user_emails],
)
with SENT_EMAILS_FILE_PATH.open("a") as f:
f.write("\n".join(map(str, unique_submitter_ids)))
else:
logger.info("No emails needed to be sent: %s", user_emails)
except Exception as e:
logger.error("Failed to send email", e)

# check if all submitters have been sent an email
sent_user_ids = self._read_sent_user_ids()
unset_ids = unique_submitter_ids - sent_user_ids
if unset_ids:
logger.warning(
f"Failed to send emails to the following user ids: {unset_ids}"
)
else:
logger.info(
f"""

====================================================================
All emails sent successfully, {SENT_EMAILS_FILE_PATH} can be removed
====================================================================

"""
)

def _read_sent_user_ids(self):
# read temp file that tracks which user ids have been sent emails
try:
with SENT_EMAILS_FILE_PATH.open("r") as f:
return {int(line.strip()) for line in f}
except FileNotFoundError:
return set()

def _get_email_body(self):
return f"""
Dear CoMSES Member,

We are planning to phase out Creative Commons licenses in the CoMSES Model Library due to their [unsuitability for software](https://creativecommons.org/faq/#can-i-apply-a-creative-commons-license-to-software) and have determined that one or more of your submissions to the CoMSES Computational Model Library currently use a Creative Commons license. In order to ensure that your published computational models are properly licensed for reuse, we strongly urge to consider selecting an alternate OSI-approved open source license for your computational models.

We have set up a straightforward process to transition all of your submissions at once to an appropriate software license. You can access this service at the following link (you will need to sign in to comses.net first):

[{CC_LICENSE_CHANGE_URL}]({CC_LICENSE_CHANGE_URL})

Thank you for helping us make model software more accessible and reusable, and do not hesitate to contact us if you have any questions or require further assistance.

Sincerely,
The CoMSES Net Team
"""
77 changes: 77 additions & 0 deletions django/library/jinja2/library/cc_license_change.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{% extends "base.jinja" %}

{% block title %}Model License Migration{% endblock %}

{% block introduction %}<h1>Model License Migration</h1>{% endblock %}

{% block content %}
<div class="container mt-4">
{% if user_releases %}
<p>
We are phasing out Creative Commons licenses for computational models
published in the CoMSES Model Library. Creative Commons offers excellent licenses for data and publications but
<a href="https://creativecommons.org/faq/#can-i-apply-a-creative-commons-license-to-software">
explicitly state that they <em>do not recommend</em> their usage for software.
</a>
In order to ensure that software in our model library is properly licensed for reuse, we are providing an easy
way to relicense your models all at once to an appropriate software license. If you prefer a different
license to the default alternatives listed below, please edit your specific codebase release's metadata and
update the license there.
</p>
<ul>
<li>
Most <a href='https://creativecommons.org/licenses/by/4.0/'>CC-BY</a>, CC-NC, and CC-ND licenses will be
replaced with the <a href="https://opensource.org/license/MIT" target="_blank">MIT license</a>, a commonly
used permissive license that preserves the attribution requirement but promotes maximum reuse.
</li>
<li>
All CC ShareAlike licenses will be changed to the
<a href="https://www.gnu.org/licenses/gpl-3.0.en.html" target="_blank">
GNU General Public License v3.0 (GPL-3.0-or-later)
</a>, a widely used copyleft license that is closest in spirit to the
<a href='https://creativecommons.org/licenses/by-sa/4.0/'>ShareAlike</a> restriction. There are no commonly
used software licenses that offer the same restrictions as the NonCommercial or NoDerivatives clauses in the
Creative Commons family of licenses.
</li>
</ul>
<hr />
<p><strong>
Please review the following changes to the licensing of your models stored in the model library. By pressing
confirm, you will be relicensing your software which affects how others are legally allowed to use your code.
</strong></p>
<form method="post">
{{ csrf_input }}
<table class="table">
<thead>
<tr>
<th>Model release</th>
<th>Current license</th>
<th>Change to new license</th>
</tr>
</thead>
<tbody>
{% for release in user_releases %}
<tr>
<td><a href="{{ release.get_absolute_url() }}">{{ release.title }}</a></td>
<td class="table-danger">
{{ release.license.name }}
<a href="{{ release.license.url }}" target="_blank"><i
class="small fas fa-external-link-alt"></i></a>
</td>
<td class="table-success">
{{ release.candidate_license.name }} <a href="{{ release.candidate_license.url }}"
target="_blank"><i class="small fas fa-external-link-alt"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="btn btn-primary mb-5 mt-3">Confirm License Change</button>
</form>
{% else %}
<div class="alert alert-info" role="alert">
You do not have any releases requiring license updates.
</div>
{% endif %}
</div>
{% endblock %}
20 changes: 20 additions & 0 deletions django/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,25 @@ class ContributorAffiliation(TaggedItemBase):
)


class LicenseQuerySet(models.QuerySet):
def software(self, **kwargs):
return self.no_cc(**kwargs)

def no_cc(self, **kwargs):
return (
self.exclude(name="None")
.exclude(name__istartswith="CC", **kwargs)
.order_by("name")
)

def creative_commons(self, **kwargs):
return self.filter(name__istartswith="CC", **kwargs)


@register_snippet
class License(models.Model):
objects = LicenseQuerySet.as_manager()

name = models.CharField(
max_length=200, help_text=_("SPDX license code from https://spdx.org/licenses/")
)
Expand Down Expand Up @@ -1073,6 +1090,9 @@ def latest_for_feed(self, number=10, include_all=False):
return qs
return qs[:number]

def with_cc_license(self, **kwargs):
return self.filter(license__in=License.objects.creative_commons(), **kwargs)


@add_to_comses_permission_whitelist
class CodebaseRelease(index.Indexed, ClusterableModel):
Expand Down
20 changes: 10 additions & 10 deletions django/library/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,13 @@


class LicenseSerializer(serializers.ModelSerializer):
def create(self, validated_data):
license = License.objects.filter(name=validated_data["name"]).first()
if license is not None:
if validated_data["url"] != license.url:
license.url = validated_data["url"]
license.save()
else:
license = super().create(validated_data)
def validate_name(self, value):
if not License.objects.filter(name=value).exists():
raise ValidationError(_(f"Invalid license: {value}"))
return value

return license
def create(self, validated_data):
return License.objects.filter(name=validated_data["name"]).first()

class Meta:
model = License
Expand Down Expand Up @@ -573,7 +570,10 @@ class CodebaseReleaseEditSerializer(CodebaseReleaseSerializer):

def get_possible_licenses(self, instance):
serialized = LicenseSerializer(
License.objects.order_by("name").all(), many=True
# FXIME: directly exclude CC licenses, this will be unnecessary if/when we manage
# to fully migrate all codebases to proper OSI-approved licenses
License.objects.software(),
many=True,
)
return serialized.data

Expand Down
5 changes: 5 additions & 0 deletions django/library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@
views.CodebaseReleaseFormCreateView.as_view(),
name="codebaserelease-add",
),
path(
"cc-license/",
views.CCLicenseChangeView.as_view(),
name="cc-license-change",
),
] + router.urls
66 changes: 61 additions & 5 deletions django/library/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import logging
import pathlib

from django.forms import Form
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import resolve
from django.urls import resolve, reverse
from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic import FormView
from django.views.generic.base import RedirectView
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView
Expand Down Expand Up @@ -59,6 +58,7 @@
CodebaseRelease,
Contributor,
CodebaseImage,
License,
PeerReview,
PeerReviewerFeedback,
PeerReviewInvitation,
Expand All @@ -78,6 +78,8 @@
PeerReviewEventLogSerializer,
)

import logging
import pathlib

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1083,3 +1085,57 @@ class ContributorList(generics.ListAPIView):
serializer_class = ContributorSerializer
pagination_class = SmallResultSetPagination
filter_backends = (ContributorFilter,)


class CCLicenseChangeView(LoginRequiredMixin, FormView):
template_name = "library/cc_license_change.jinja"
form_class = Form # just a confirmation form, we don't need any fields
success_message = "Licenses updated successfully."

# maps CC license SPDX names to a default alternative
LICENSE_MAPPING = {
"CC-BY-4.0": "MIT",
"CC-BY-ND-4.0": "MIT",
"CC-BY-NC-4.0": "MIT",
"CC-BY-NC-ND-4.0": "MIT",
"CC-BY-SA-4.0": "GPL-3.0",
"CC-BY-NC-SA-4.0": "GPL-3.0",
}

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user_releases = CodebaseRelease.objects.with_cc_license(
submitter=self.request.user,
)
for release in user_releases:
candidate_license_name = self.LICENSE_MAPPING.get(release.license.name)
release.candidate_license = License.objects.get(name=candidate_license_name)

context["user_releases"] = user_releases
return context

def form_valid(self, form):
# update all codebase releases with invalid licenses with their mapped alternative
try:
releases_with_cc = CodebaseRelease.objects.with_cc_license(
submitter=self.request.user
)
for release in releases_with_cc:
candidate_license_name = self.LICENSE_MAPPING.get(release.license.name)
release.license = License.objects.get(name=candidate_license_name)
release.save()
messages.success(
self.request,
"Licenses successfully updated. Thanks for making your models more reusable on CoMSES.Net!",
)
return super().form_valid(form)
except Exception as e:
messages.error(
self.request,
"An error occurred while attempting to update your licenses. Please contact us if the problem persists.",
)
logger.error("Error updating licenses: %s", e)
return super().form_invalid(form)

def get_success_url(self):
return reverse("core:profile-detail", kwargs={"pk": self.request.user.pk})
Loading