From 2b3efcfcb043e389c73f72054433a8c377b022c6 Mon Sep 17 00:00:00 2001 From: Krzysztof Dorosz Date: Sun, 14 Oct 2012 23:41:35 +0200 Subject: [PATCH 1/2] This commit fixes bug: 1) in urls a view "tos" has context object evaluated only in a runtime. This means that even if tos was changed, the "tos" view shows always the same old tos object. 2) check_tos view uses now messages framework instead of {{note}} 3) Support for translation in "you cannot login..." message added The following code was tested with django 1.4 and it is reported to work well. --- tos/urls.py | 24 ++++-------------------- tos/views.py | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/tos/urls.py b/tos/urls.py index 73924f8..27cbbf4 100644 --- a/tos/urls.py +++ b/tos/urls.py @@ -1,26 +1,10 @@ -from django.conf.urls.defaults import * -from django.views.generic.simple import direct_to_template - -from tos.models import TermsOfService -from tos.views import check_tos +from django.conf.urls import url, patterns +from tos.views import check_tos, TosView urlpatterns = patterns('', # Terms of Service conform - url( - regex = '^confirm/$', - view = check_tos, - name = 'tos_check_tos', - ), + url(r'^confirm/$', check_tos, name='tos_check_tos'), # Terms of service simple display - url( - regex = '^$', - view = direct_to_template, - kwargs = {'template': 'tos/tos.html', - 'extra_context':{ - 'tos':TermsOfService.objects.get_current_tos() - }, - }, - name = 'tos', - ), + url(r'^$', TosView.as_view(), name='tos'), ) \ No newline at end of file diff --git a/tos/views.py b/tos/views.py index 89e847a..a85032c 100644 --- a/tos/views.py +++ b/tos/views.py @@ -1,17 +1,29 @@ +from django.views.generic import TemplateView +import re from django.conf import settings +from django.contrib import messages from django.contrib.auth import login as auth_login from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.forms import AuthenticationForm -from django.contrib.auth.models import User from django.contrib.sites.models import Site, RequestSite from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.template import RequestContext from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_protect +from django.utils.translation import ugettext_lazy as _ from tos.models import has_user_agreed_latest_tos, TermsOfService, UserAgreement +class TosView(TemplateView): + template_name = "tos/tos.html" + + def get_context_data(self, **kwargs): + context = super(TosView, self).get_context_data(**kwargs) + context['tos'] = TermsOfService.objects.get_current_tos() + return context + + def _redirect_to(redirect_to): """ Moved redirect_to logic here to avoid duplication in views""" @@ -31,13 +43,9 @@ def _redirect_to(redirect_to): @never_cache def check_tos(request, template_name='tos/tos_check.html', redirect_field_name=REDIRECT_FIELD_NAME,): - + redirect_to = _redirect_to(request.REQUEST.get(redirect_field_name, '')) - - note="" - tos = TermsOfService.objects.get_current_tos() - if request.method=="POST": if request.POST.get("accept", "") == "accept": user = request.session['tos_user'] @@ -53,12 +61,12 @@ def check_tos(request, template_name='tos/tos_check.html', return HttpResponseRedirect(redirect_to) else: - note="You cannot login without agreeing to the terms of this site." + messages.error(request, _(u"You cannot login without agreeing to the terms of this site.")) + - return render_to_response(template_name, { 'tos':tos, - 'note':note, + redirect_field_name: redirect_to, }, context_instance=RequestContext(request)) From 705356cb53eb092916f515b8d2cbd2e342e7d0ae Mon Sep 17 00:00:00 2001 From: Krzysztof Dorosz Date: Sun, 14 Oct 2012 23:45:49 +0200 Subject: [PATCH 2/2] - Bumping version to 1.1 - Adding me to AUTHORS :) - Correcting login url name to auth_login to keep up with django standards --- AUTHORS.txt | 1 + README.rst | 4 ++-- tos/__init__.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index 0de79ad..9f2e772 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -4,3 +4,4 @@ The following is a list of much appreciated contributors: Daniel Greenfeld Frank Wiles +Krzysztof Dorosz diff --git a/README.rst b/README.rst index 263666f..1c93ea0 100644 --- a/README.rst +++ b/README.rst @@ -22,6 +22,6 @@ Installation # terms of service links urlpatterns += patterns('', - (r'^login/$', 'tos.views.login', {}, 'login',), - (r'^terms-of-service/', include('tos.urls')), + url(r'^login/$', 'tos.views.login', {}, 'auth_login',), + url(r'^terms-of-service/', include('tos.urls')), ) \ No newline at end of file diff --git a/tos/__init__.py b/tos/__init__.py index 93c4c7c..e8e61c0 100644 --- a/tos/__init__.py +++ b/tos/__init__.py @@ -1 +1 @@ -VERSION = (0, 1, 0) \ No newline at end of file +VERSION = (0, 1, 1) \ No newline at end of file