Nuked the crazy flatpages since that was a miscommunication. Added in a handed TOS manager and a agreement checker. Updated tests to match

This commit is contained in:
pydanny 2010-06-18 14:57:24 -05:00
parent 2fd6920fac
commit 475a66cfd5
4 changed files with 59 additions and 34 deletions

View file

@ -7,26 +7,17 @@ This project gives the admin the ability to reset terms of agreement with the en
Summary
=======
- based on flatpages
- keeps track of when TOS is changed
- Users need to be informed and agree/re-agree when they login (custom login is provided)
- Just two models (TOS and user agreement)
Installation
============
django-tos relies on django-flatpages so you have to follow those rules of installation:
1. Install the sites framework by adding `django.contrib.sites` to your INSTALLED_APPS setting, if its not already in there.
2. Also make sure youve correctly set `SITE_ID` to the ID of the site the settings file represents. This will usually be 1 (i.e. `SITE_ID = 1`, but if youre using the sites framework to manage multiple sites, it could be the ID of a different site.
3. Add `django.contrib.flatpages` to your INSTALLED_APPS setting.
4. Add `tos` to your INSTALLED_APPS setting.
1. Add `tos` to your INSTALLED_APPS setting.
5. Add `django.contrib.flatpages.middleware.FlatpageFallbackMiddleware` to your MIDDLEWARE_CLASSES setting.
6. Run the command `manage.py syncdb`.
2. Run the command `manage.py syncdb`.
7. In your root urlconf file `urls.py` add `(r'^login/$', 'tos.views.login', {}, 'login',),` to your url patterns.
3. In your root urlconf file `urls.py` add `(r'^login/$', 'tos.views.login', {}, 'login',),` to your url patterns.
4. In your root urlconf file `urls.py` add `(r'^terms-of-service/$', 'tos.views.tos', {}, 'tos',),` to your url patterns.

View file

@ -1,5 +1,3 @@
# TODO - enhance with FlatPage inline
from django.contrib import admin
from tos.models import TermsOfService, UserAgreement

View file

@ -1,5 +1,4 @@
from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
@ -11,11 +10,18 @@ class BaseModel(models.Model):
class Meta:
abstract = True
class TermsOfServiceManager(models.Manager):
def get_current_tos(self):
return super(TermsOfServiceManager, self).get_query_set().get(active=True)
class TermsOfService(BaseModel):
flat_page = models.ForeignKey(FlatPage, related_name='flatpage')
active = models.BooleanField(_('active'), _('Only one terms of service is allowed to be active'))
active = models.BooleanField(_('active'), _('Only one terms of service is allowed to be active'))
content = models.TextField(_('content'), blank=True)
objects = TermsOfServiceManager()
class Meta:
get_latest_by = 'created'
@ -43,4 +49,10 @@ class UserAgreement(BaseModel):
user = models.ForeignKey(User, related_name='user_agreement')
def __unicode__(self):
return '%s agreed to TOS: %s ' % (self.user.username, self.terms_of_service.__unicode__())
return '%s agreed to TOS: %s ' % (self.user.username, self.terms_of_service.__unicode__())
def has_user_agreed_latest_tos(user):
if UserAgreement.objects.filter(terms_of_service=TermsOfService.objects.get_current_tos(),user=user):
return True
return False

View file

@ -1,29 +1,21 @@
from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage
from django.test import TestCase
from tos.models import TermsOfService, UserAgreement
from tos.models import TermsOfService, UserAgreement, has_user_agreed_latest_tos
class TestBasics(TestCase):
class TestModels(TestCase):
def setUp(self):
self.user1 = User.objects.create_user('user1', 'user1@example.com', 'user1pass')
self.user2 = User.objects.create_user('user2', 'user2@example.com', 'user2pass')
self.user3 = User.objects.create_user('user3', 'user3@example.com', 'user3pass')
self.flatpage1 = FlatPage.objects.create(
url = '/terms-of-service/',
title = 'Terms of Service',
content = 'lorem ipsum and stuff',
enable_comments = 0,
registration_required = False
)
self.tos1 = TermsOfService.objects.create(
flat_page = self.flatpage1,
content = "first edition of the terms of service",
active = True
)
self.tos2 = TermsOfService.objects.create(
flat_page = self.flatpage1,
content = "second edition of the terms of service",
active = False
)
@ -41,6 +33,38 @@ class TestBasics(TestCase):
first = TermsOfService.objects.get(id=self.tos1.id)
self.assertFalse(first.active)
def test_terms_of_service_manager(self):
self.assertEquals(TermsOfService.objects.get_current_tos(), self.tos1)
def test_user_agreement(self):
#agreement = UserAgreement.objects.create()
# simple agreement
UserAgreement.objects.create(
terms_of_service = self.tos1,
user = self.user1
)
self.assertTrue(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertFalse(has_user_agreed_latest_tos(self.user3))
# Now set self.tos2.active to True and see what happens
self.tos2.active=True
self.tos2.save()
self.assertFalse(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertFalse(has_user_agreed_latest_tos(self.user3))
# add in a couple agreements and try again
UserAgreement.objects.create(
terms_of_service = self.tos2,
user = self.user1
)
UserAgreement.objects.create(
terms_of_service = self.tos2,
user = self.user3
)
self.assertTrue(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertTrue(has_user_agreed_latest_tos(self.user3))