Skip to content

Commit

Permalink
Merge pull request #84 from blag/middleware-warn-on-debug
Browse files Browse the repository at this point in the history
Middleware: Only warn when settings.DEBUG is True
  • Loading branch information
nicholasserra authored Jun 25, 2024
2 parents ad03867 + 2d57b69 commit d9e10ad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
20 changes: 14 additions & 6 deletions tos/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
Expand All @@ -21,9 +23,12 @@ def get_current_tos(self):
try:
return self.get(active=True)
except self.model.DoesNotExist:
raise NoActiveTermsOfService(
'Please create an active Terms-of-Service'
)
if settings.DEBUG:
warnings.warn("There is no active Terms-of-Service")
else:
raise NoActiveTermsOfService(
'Please create an active Terms-of-Service'
)


class TermsOfService(BaseModel):
Expand Down Expand Up @@ -60,9 +65,12 @@ def save(self, *args, **kwargs):
.exclude(id=self.id)\
.filter(active=True)\
.exists():
raise NoActiveTermsOfService(
'One of the terms of service must be marked active'
)
if settings.DEBUG:
warnings.warn("There is no active Terms-of-Service")
else:
raise NoActiveTermsOfService(
'One of the terms of service must be marked active'
)

super().save(*args, **kwargs)

Expand Down
38 changes: 37 additions & 1 deletion tos/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test import TestCase, override_settings

from tos.models import (
NoActiveTermsOfService,
Expand Down Expand Up @@ -104,3 +104,39 @@ def test_terms_of_service_manager(self):
def test_terms_of_service_manager_raises_error(self):

self.assertRaises(NoActiveTermsOfService, TermsOfService.objects.get_current_tos)


class TestNoActiveTOS(TestCase):
@classmethod
def setUpClass(cls):
# Use bulk_create to avoid calling the model's save() method
TermsOfService.objects.bulk_create([
TermsOfService(
content="The only TOS",
active=False,
)
])

@classmethod
def tearDownClass(cls):
TermsOfService.objects.all().delete()

@override_settings(DEBUG=True)
def test_model_save_raises_warning(self):
with self.assertWarns(Warning):
TermsOfService.objects.first().save()

@override_settings(DEBUG=True)
def test_get_current_tos_raises_warning(self):
with self.assertWarns(Warning):
TermsOfService.objects.get_current_tos()

@override_settings(DEBUG=False)
def test_model_save_raises_exception(self):
with self.assertRaises(NoActiveTermsOfService):
TermsOfService.objects.first().save()

@override_settings(DEBUG=False)
def test_get_current_tos_raises_exception(self):
with self.assertRaises(NoActiveTermsOfService):
TermsOfService.objects.get_current_tos()

0 comments on commit d9e10ad

Please sign in to comment.