From a4d919900779c9664c3058b1bf8c86b79869c8f1 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Tue, 13 Dec 2011 19:58:33 +0000 Subject: [PATCH] Name Clarifications Updated the views to clarify the names and make it clearer what they do. Which included re-naming the templates. Also performed a number of PEP8 fixes along the way. --- consent/models.py | 23 ++++++++-------- ...nsent_edit_view.html => consent_edit.html} | 0 consent/templates/consent/consent_list.html | 9 +++++++ consent/templates/consent/privilege_list.html | 9 ------- consent/views.py | 27 +++++++++++-------- tests/test_consent/fixture_gen.py | 13 ++++++--- tests/test_consent/tests.py | 10 ++++--- tests/test_consent/urls.py | 4 +-- 8 files changed, 54 insertions(+), 41 deletions(-) rename consent/templates/consent/{consent_edit_view.html => consent_edit.html} (100%) create mode 100644 consent/templates/consent/consent_list.html delete mode 100644 consent/templates/consent/privilege_list.html diff --git a/consent/models.py b/consent/models.py index 9641a21..b59627c 100644 --- a/consent/models.py +++ b/consent/models.py @@ -1,8 +1,8 @@ """ -There are two key models in the Consent app. These are Privilege and Consent. A -privilage is added to the website normally in the Django admin and then a user -has the option of granting the consent to to the website. After Consent has -been granted, the user is able to revoke the consent. +There are two key models in the Consent app. These are Privilege and Consent. +A privilage is added to the website normally in the Django admin and then a +user has the option of granting the consent to to the website. After Consent +has been granted, the user is able to revoke the consent. """ from datetime import datetime @@ -12,9 +12,9 @@ class Privilege(models.Model): """ - A privilage is a permission that the website asks from the user. This could - be the permission to email them, share the users details or to use their - (already authorised) social netorking sites. + A privilage is a permission that the website asks from the user. This + could be the permission to email them, share the users details or to use + their (already authorised) social netorking sites. """ name = models.CharField(max_length=64) description = models.TextField() @@ -70,8 +70,8 @@ def granted(self, user=None): def revoked(self, user=None): """ - Return all of the revoked consents either for all the users or the given - user. + Return all of the revoked consents either for all the users or the + given user. """ revoked_consents = self.filter(revoked=True) if user: @@ -81,8 +81,8 @@ def revoked(self, user=None): class Consent(models.Model): """ - Consent is the agreement from a user to grant a specific privilege. This can - then be revoked by the user at a later date. + Consent is the agreement from a user to grant a specific privilege. This + can then be revoked by the user at a later date. """ user = models.ForeignKey(User) privilege = models.ForeignKey(Privilege) @@ -94,6 +94,7 @@ class Consent(models.Model): class Meta: unique_together = ('user', 'privilege',) + ordering = ['privilege__name', ] def revoke(self): """ diff --git a/consent/templates/consent/consent_edit_view.html b/consent/templates/consent/consent_edit.html similarity index 100% rename from consent/templates/consent/consent_edit_view.html rename to consent/templates/consent/consent_edit.html diff --git a/consent/templates/consent/consent_list.html b/consent/templates/consent/consent_list.html new file mode 100644 index 0000000..7fdf89b --- /dev/null +++ b/consent/templates/consent/consent_list.html @@ -0,0 +1,9 @@ + +{% for consent in consent_list %} + +

{{ consent.privilege.name }}

+ {{ consent.privilege.description }}
+
+ ({{ consent.privilege.users.count }} User{{ consent.privilege.users.count|pluralize }}) + +{% endfor %} diff --git a/consent/templates/consent/privilege_list.html b/consent/templates/consent/privilege_list.html deleted file mode 100644 index 975a85c..0000000 --- a/consent/templates/consent/privilege_list.html +++ /dev/null @@ -1,9 +0,0 @@ - -{% for privilege in privilege_list %} - -

{{ privilege.name }}

- {{ privilege.description }}
-
- ({{ privilege.users.count }} User{{ privilege.users.count|pluralize }}) - -{% endfor %} diff --git a/consent/views.py b/consent/views.py index c60dca1..d102dca 100644 --- a/consent/views.py +++ b/consent/views.py @@ -1,27 +1,31 @@ from django.http import HttpResponseRedirect from django.views.generic import ListView, FormView -from consent.models import Privilege, Consent +from consent.models import Consent from consent.forms import ConsentForm -class PrivilegeListView(ListView): +class ConsentListView(ListView): """ - The PrivilegeListView inherits from ``django.views.generic.ListView`` and + The ConsentListView inherits from ``django.views.generic.ListView`` and sets a number of defaults to make it easy to integrate into your app. """ - #: The template variable name for the QuerySet of ``consent.models.Privilege`` - context_object_name = 'privilege_list' + #: The template variable name for the QuerySet of + #: ``consent.models.Privilege`` + context_object_name = 'consent_list' #: The default template name for showing the list of privileges - template_name = 'consent/privilege_list_view.html' - model = Privilege + template_name = 'consent/consent_list.html' + + def get_queryset(self): + return Consent.objects.for_user(self.request.user) class ConsentEditView(FormView): - #: The default template name for editing instances of ``consent.model.Consent`` - template_name = 'consent/consent_edit_view.html' + #: The default template name for editing instances of + #: ``consent.model.Consent`` + template_name = 'consent/consent_edit.html' form_class = ConsentForm success_url = '.' @@ -48,17 +52,18 @@ def form_valid(self, form): current_consents = self.get_privileges_with_consent() consents = form.cleaned_data['consents'] + user = self.request.user if consents: consent_ids = consents.values_list('id', flat=True) else: consent_ids = [] revoked_privileges = current_consents.exclude(pk__in=consent_ids) - Consent.objects.revoke_consent(self.request.user, revoked_privileges) + Consent.objects.revoke_consent(user, revoked_privileges) if consents: current_consent_ids = current_consents.values_list('id', flat=True) consented_privileges = consents.exclude(pk__in=current_consent_ids) - Consent.objects.grant_consent(self.request.user, consented_privileges) + Consent.objects.grant_consent(user, consented_privileges) return HttpResponseRedirect(self.get_success_url()) diff --git a/tests/test_consent/fixture_gen.py b/tests/test_consent/fixture_gen.py index c4fb8f9..6ded3be 100644 --- a/tests/test_consent/fixture_gen.py +++ b/tests/test_consent/fixture_gen.py @@ -8,8 +8,10 @@ @fixture_generator(User) def test_users(): - User.objects.create_user(username="john", email="john@test.com", password="password") - User.objects.create_user(username="smith", email="smith@test.com", password="password") + User.objects.create_user(username="john", email="john@test.com", + password="password") + User.objects.create_user(username="smith", email="smith@test.com", + password="password") @fixture_generator(Privilege) @@ -28,10 +30,13 @@ def test_privileges(): """) -@fixture_generator(Consent, requires=['test_consent.test_privileges', 'test_consent.test_users']) +@fixture_generator(Consent, requires=['test_consent.test_privileges', + 'test_consent.test_users']) def test_consents(): - newsletter, marketing, facebook, twitter = Privilege.objects.order_by('name') + privileges = Privilege.objects.order_by('name') + + newsletter, marketing, facebook, twitter = privileges smith, john = User.objects.order_by('username') diff --git a/tests/test_consent/tests.py b/tests/test_consent/tests.py index cecda6c..e446820 100755 --- a/tests/test_consent/tests.py +++ b/tests/test_consent/tests.py @@ -16,13 +16,15 @@ def test_consent(self): from consent.models import Consent - consents = Consent.objects.for_user(self.john).order_by('privilege__name') + consents = Consent.objects.for_user(self.john) # the three that John has previously encoutered in the fixtures newsletter, marketing, facebook = consents - self.assertEqual(str(newsletter), "john permits the 'Email Newsletter' privilege") - self.assertEqual(str(marketing), "john revoked the 'Marketing Emails' privilege") + msg = "john permits the 'Email Newsletter' privilege" + self.assertEqual(str(newsletter), msg) + msg = "john revoked the 'Marketing Emails' privilege" + self.assertEqual(str(marketing), msg) self.assertNotIn(newsletter, Consent.objects.revoked(self.john)) newsletter.revoke() @@ -57,7 +59,7 @@ def test_list_privilges(self): self.assertEqual(r.status_code, 200, "Response returned a %s when 200 was expected" % r.status_code) - for privilege in Privilege.objects.all(): + for privilege in Privilege.objects.filter(consent__user=self.john): self.assertIn(privilege.name, r.content) self.assertIn(privilege.description, r.content) diff --git a/tests/test_consent/urls.py b/tests/test_consent/urls.py index 592f105..9671ae6 100644 --- a/tests/test_consent/urls.py +++ b/tests/test_consent/urls.py @@ -1,6 +1,6 @@ from django.conf.urls.defaults import patterns, url, include -from consent.views import PrivilegeListView, ConsentEditView +from consent.views import ConsentListView, ConsentEditView from django.contrib import admin @@ -8,7 +8,7 @@ urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)), - url(r'^$', PrivilegeListView.as_view(), name="privileges"), + url(r'^$', ConsentListView.as_view(), name="privileges"), url(r'^edit/$', ConsentEditView.as_view(), name="edit_privileges"), )