add the ability to disable the web display of some apps

This commit is contained in:
Brian S. Stephan 2023-03-27 15:56:15 -05:00
parent a214d8acfd
commit 420a7b1472
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
7 changed files with 76 additions and 15 deletions

View File

@ -1,5 +1,5 @@
"""Site processors to add additional template tags and whatnot."""
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.utils.functional import SimpleLazyObject
@ -11,4 +11,5 @@ def site(request):
return {
'site': site,
'site_root': SimpleLazyObject(lambda: "{0}://{1}".format(protocol, site.domain)),
'WEB_ENABLED_APPS': settings.WEB_ENABLED_APPS,
}

View File

@ -145,6 +145,16 @@ BOOTSTRAP3 = {
'javascript_in_head': True,
}
###############
# web options #
###############
# choose which apps to display in the web UI, for those that support this config
WEB_ENABLED_APPS = [
'karma',
'races',
]
# IRC module stuff

View File

@ -30,9 +30,9 @@
{% block navbar_menu %}
<ul class="nav navbar-nav">
<li><a href="{% url 'facts_index' %}">Item Sets</a></li>
<li><a href="{% url 'karma_index' %}">Karma</a></li>
{% if "karma" in WEB_ENABLED_APPS %}<li><a href="{% url 'karma_index' %}">Karma</a></li>{% endif %}
<li><a href="{% url 'markov_index' %}">Markov</a></li>
<li><a href="{% url 'races_index' %}">Races</a></li>
{% if "races" in WEB_ENABLED_APPS %}<li><a href="{% url 'races_index' %}">Races</a></li>{% endif %}
</ul>
{% endblock %}
<div class="navbar-right">

View File

@ -1,27 +1,32 @@
"""Present karma data."""
import logging
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, render
from rest_framework import viewsets
from karma.models import KarmaKey
from karma.serializers import KarmaKeySerializer
log = logging.getLogger('karma.views')
log = logging.getLogger(__name__)
def index(request):
"""Display all karma keys."""
entries = KarmaKey.objects.all().order_by('key')
if 'karma' not in settings.WEB_ENABLED_APPS:
raise PermissionDenied()
entries = KarmaKey.objects.all().order_by('key')
return render(request, 'karma/index.html', {'entries': entries})
def key_detail(request, karma_key):
"""Display the requested karma key."""
entry = get_object_or_404(KarmaKey, key=karma_key.lower())
if 'karma' not in settings.WEB_ENABLED_APPS:
raise PermissionDenied()
entry = get_object_or_404(KarmaKey, key=karma_key.lower())
return render(request, 'karma/karma_key.html', {'entry': entry, 'entry_history': entry.history(mode='date')})

View File

@ -1,26 +1,28 @@
"""Display race statuses and whatnot."""
import logging
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, render
from races.models import Race, Racer, RaceUpdate
log = logging.getLogger('races.views')
log = logging.getLogger(__name__)
def index(request):
"""Display a list of races."""
if 'races' not in settings.WEB_ENABLED_APPS:
raise PermissionDenied()
races = Race.objects.all()
return render(request, 'races/index.html', {'races': races})
def race_detail(request, race_id):
"""Display a race detail."""
if 'races' not in settings.WEB_ENABLED_APPS:
raise PermissionDenied()
race = get_object_or_404(Race, pk=race_id)
return render(request, 'races/race_detail.html', {'race': race})

View File

@ -1,8 +1,5 @@
"""Test the race views."""
from unittest import mock
from django.test import TestCase
from django.utils.timezone import now
from races.models import Race, Racer, RaceUpdate

View File

@ -0,0 +1,46 @@
"""Test views and templates' adherence to WEB_ENABLED_APPS."""
from django.conf import settings
from django.test import TestCase
class WebEnabledAppsAPITest(TestCase):
"""Test that certain display elements and views can be enabled/disabled via settings."""
def setUp(self):
"""Store the old setting so that we can restore it later."""
self.old_sites_list = settings.WEB_ENABLED_APPS
print("butt")
def tearDown(self):
"""Restore the old setting stored earlier."""
settings.WEB_ENABLED_APPS = self.old_sites_list
print("butt")
def test_default_enabled(self):
"""Test that the expected sites can be reached and displayed by default."""
resp = self.client.get('/karma/')
self.assertEqual(resp.status_code, 200)
self.assertIn(b'<a href="/karma/">', resp.content)
resp = self.client.get('/races/')
self.assertEqual(resp.status_code, 200)
self.assertIn(b'<a href="/races/">', resp.content)
def test_one_disabled(self):
"""Test that we can disable one site but not all sites using this setting."""
settings.WEB_ENABLED_APPS = ['karma']
resp = self.client.get('/karma/')
self.assertEqual(resp.status_code, 200)
self.assertIn(b'<a href="/karma/">', resp.content)
resp = self.client.get('/races/')
self.assertEqual(resp.status_code, 403)
self.assertNotIn(b'<a href="/races/">', resp.content)
def test_all_disabled(self):
"""Test that we can disable all sites using this setting."""
settings.WEB_ENABLED_APPS = []
resp = self.client.get('/karma/')
self.assertEqual(resp.status_code, 403)
self.assertNotIn(b'<a href="/karma/">', resp.content)
resp = self.client.get('/races/')
self.assertEqual(resp.status_code, 403)
self.assertNotIn(b'<a href="/races/">', resp.content)