From 475a66cfd58aa8d580fb1b682a51c15f64d24591 Mon Sep 17 00:00:00 2001 From: pydanny Date: Fri, 18 Jun 2010 14:57:24 -0500 Subject: [PATCH] Nuked the crazy flatpages since that was a miscommunication. Added in a handed TOS manager and a agreement checker. Updated tests to match --- README.rst | 19 ++++----------- tos/admin.py | 2 -- tos/models.py | 22 ++++++++++++++---- tos/tests/test_models.py | 50 +++++++++++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/README.rst b/README.rst index e1bc2ac..2afc1aa 100644 --- a/README.rst +++ b/README.rst @@ -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 it’s not already in there. - 2. Also make sure you’ve 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 you’re 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. \ No newline at end of file + 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. \ No newline at end of file diff --git a/tos/admin.py b/tos/admin.py index 3ff4268..369eb93 100644 --- a/tos/admin.py +++ b/tos/admin.py @@ -1,5 +1,3 @@ -# TODO - enhance with FlatPage inline - from django.contrib import admin from tos.models import TermsOfService, UserAgreement diff --git a/tos/models.py b/tos/models.py index 20ec9e6..b40d052 100644 --- a/tos/models.py +++ b/tos/models.py @@ -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__()) \ No newline at end of file + 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 \ No newline at end of file diff --git a/tos/tests/test_models.py b/tos/tests/test_models.py index 1ad1985..22cc8be 100644 --- a/tos/tests/test_models.py +++ b/tos/tests/test_models.py @@ -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() \ No newline at end of file + # 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)) + \ No newline at end of file