Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Name Clarifications
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d0ugal committed Dec 13, 2011
1 parent 37226aa commit a4d9199
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 41 deletions.
23 changes: 12 additions & 11 deletions 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

Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -94,6 +94,7 @@ class Consent(models.Model):

class Meta:
unique_together = ('user', 'privilege',)
ordering = ['privilege__name', ]

def revoke(self):
"""
Expand Down
9 changes: 9 additions & 0 deletions consent/templates/consent/consent_list.html
@@ -0,0 +1,9 @@

{% for consent in consent_list %}

<h4>{{ consent.privilege.name }}</h4>
{{ consent.privilege.description }}<br/>
<br/>
({{ consent.privilege.users.count }} User{{ consent.privilege.users.count|pluralize }})

{% endfor %}
9 changes: 0 additions & 9 deletions consent/templates/consent/privilege_list.html

This file was deleted.

27 changes: 16 additions & 11 deletions 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 = '.'

Expand All @@ -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())
13 changes: 9 additions & 4 deletions tests/test_consent/fixture_gen.py
Expand Up @@ -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)
Expand All @@ -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')

Expand Down
10 changes: 6 additions & 4 deletions tests/test_consent/tests.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_consent/urls.py
@@ -1,14 +1,14 @@
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

admin.autodiscover()

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"),

)

0 comments on commit a4d9199

Please sign in to comment.