Blocked TOS from having all set active=True. Which would break login and lots of other critical things

This commit is contained in:
pydanny 2010-06-18 15:36:51 -05:00
parent 475a66cfd5
commit 07e0b85d5e
2 changed files with 15 additions and 0 deletions

View file

@ -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)

View file

@ -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