-
-
Notifications
You must be signed in to change notification settings - Fork 225
feat(TOS): allows admins to force TOS reaccpetance DEV-2178 #7235
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d87ae76
feat(TOS): allows admins to force TOS reaccpetance DEV-2178
rgraber e5b90ae
fixup!: fixes
rgraber bd8b2f6
fixup!: sigh
rgraber 5363fcc
fixup!: unit test better
rgraber eabbfcb
fixup!: everything is strftime
rgraber d5b5362
fixup!: um
rgraber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,52 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from constance import config | ||
| from django.contrib import admin | ||
| from django.utils import timezone | ||
|
|
||
| from kobo.apps.markdownx_uploader.admin import MarkdownxModelAdminBase | ||
| from ..models import SitewideMessage | ||
|
|
||
|
|
||
| class SitewideMessageAdmin(MarkdownxModelAdminBase): | ||
|
|
||
| model = SitewideMessage | ||
| actions = ['require_terms_of_service_reacceptance'] | ||
|
|
||
| def changelist_view(self, request, extra_context=None): | ||
| actions = self.get_actions(request) | ||
| # cheating: since the require_terms_of_service_reacceptance doesn't apply to | ||
| # individual SitewideMessages, always act as if all of them have been selected | ||
| # to avoid 'Items must be selected in order to perform actions on them.' error | ||
| if ( | ||
| actions | ||
| and request.method == 'POST' | ||
| and 'index' in request.POST | ||
| and request.POST['action'] == 'require_terms_of_service_reacceptance' | ||
| ): | ||
| data = request.POST.copy() | ||
| data['select_across'] = '1' | ||
| request.POST = data | ||
| response = self.response_action( | ||
| request, queryset=self.get_queryset(request) | ||
| ) | ||
| if response: | ||
| return response | ||
| return super().changelist_view(request, extra_context) | ||
|
|
||
| @admin.action(description='Require all users to reaccept the Terms of Service') | ||
| def require_terms_of_service_reacceptance(self, request, *args, **kwargs): | ||
| tos_exists = SitewideMessage.objects.filter(slug='terms_of_service').exists() | ||
| if tos_exists: | ||
| setattr( | ||
| config, 'LAST_TOS_UPDATE', timezone.now().strftime('%Y-%m-%dT%H:%M:%SZ') | ||
| ) | ||
| self.message_user( | ||
| request, 'All users will be prompted to re-accept the Terms of Service' | ||
| ) | ||
| else: | ||
| self.message_user( | ||
| request, | ||
| 'Add a SitewideMessage with the slug terms_of_service' | ||
| ' before requiring reacceptance', | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 5.2.13 on 2026-07-10 13:24 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('hub', '0022_delete_v1usertracker'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='sitewidemessage', | ||
| name='slug', | ||
| field=models.CharField(max_length=50, unique=True), | ||
| ), | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from constance import config | ||
| from constance.test import override_config | ||
| from django.contrib.admin import site | ||
| from django.contrib.messages import get_messages | ||
| from django.test import TestCase | ||
| from django.utils import timezone | ||
| from freezegun import freeze_time | ||
|
|
||
| from hub.admin import SitewideMessageAdmin | ||
| from hub.models import SitewideMessage | ||
|
|
||
|
|
||
| class SitewideMessageAdminTest(TestCase): | ||
| @override_config(LAST_TOS_UPDATE='') | ||
| def test_require_reacceptance_with_no_tos_message(self): | ||
| admin_instance = SitewideMessageAdmin(SitewideMessage, site) | ||
| request = self.client.request().wsgi_request | ||
| admin_instance.require_terms_of_service_reacceptance( | ||
| request, SitewideMessage.objects.none() | ||
| ) | ||
| messages_list = [m.message for m in get_messages(request)] | ||
| expected_message = ( | ||
| 'Add a SitewideMessage with the slug terms_of_service ' | ||
| 'before requiring reacceptance' | ||
| ) | ||
| assert expected_message in messages_list | ||
| assert config.LAST_TOS_UPDATE == '' | ||
|
|
||
| def test_require_reacceptance_with_tos_message(self): | ||
| SitewideMessage.objects.create(slug='terms_of_service') | ||
| admin_instance = SitewideMessageAdmin(SitewideMessage, site) | ||
| request = self.client.request().wsgi_request | ||
| now = timezone.now() | ||
| with freeze_time(now): | ||
| admin_instance.require_terms_of_service_reacceptance( | ||
| request, SitewideMessage.objects.none() | ||
| ) | ||
| messages_list = [m.message for m in get_messages(request)] | ||
| expected_message = ( | ||
| 'All users will be prompted to re-accept' ' the Terms of Service' | ||
| ) | ||
| assert expected_message in messages_list | ||
| assert config.LAST_TOS_UPDATE == now.strftime('%Y-%m-%dT%H:%M:%SZ') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.