diff --git a/tos/models.py b/tos/models.py index b40d052..3bf895b 100644 --- a/tos/models.py +++ b/tos/models.py @@ -1,6 +1,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models + from django.utils.translation import ugettext_lazy as _ class BaseModel(models.Model): @@ -40,6 +41,10 @@ class TermsOfService(BaseModel): if self.active: TermsOfService.objects.exclude(id=self.id).update(active=False) + + else: + if not TermsOfService.objects.exclude(id=self.id).filter(active=True): + raise ValidationError('One of the terms of service must be marked active') super(TermsOfService,self).save(*args, **kwargs) diff --git a/tos/tests/test_models.py b/tos/tests/test_models.py index 22cc8be..a2e685b 100644 --- a/tos/tests/test_models.py +++ b/tos/tests/test_models.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.test import TestCase from tos.models import TermsOfService, UserAgreement, has_user_agreed_latest_tos @@ -33,10 +34,19 @@ class TestModels(TestCase): first = TermsOfService.objects.get(id=self.tos1.id) self.assertFalse(first.active) + # latest is active though + self.assertTrue(latest.active) + def test_terms_of_service_manager(self): self.assertEquals(TermsOfService.objects.get_current_tos(), self.tos1) + def test_validation_error_all_set_false(self): + """ If you try and set all to false the model will throw a ValidationError """ + + self.tos1.active=False + self.assertRaises(ValidationError, self.tos1.save) + def test_user_agreement(self): # simple agreement