This commit is contained in:
Frank Wiles 2016-03-02 11:15:01 -06:00
parent b90231a1f2
commit e40fedf279
5 changed files with 41 additions and 28 deletions

View file

@ -2,6 +2,8 @@ language: python
python:
- "2.6"
- "2.7"
- "3.4"
- "3.5"
install:
- pip install .

View file

@ -1,14 +1,15 @@
from django.contrib import admin
from django.contrib import admin
from tos.models import TermsOfService, UserAgreement
class TermsOfServiceAdmin(admin.ModelAdmin):
model = TermsOfService
class TermsOfServiceAdmin(admin.ModelAdmin):
model = TermsOfService
admin.site.register(TermsOfService, TermsOfServiceAdmin)
class UserAgreementAdmin(admin.ModelAdmin):
class UserAgreementAdmin(admin.ModelAdmin):
model = UserAgreement
admin.site.register(UserAgreement, UserAgreementAdmin)

View file

@ -5,12 +5,10 @@ from django.utils.translation import ugettext_lazy as _
# Django 1.4 compatability
try:
from django.contrib.auth import get_user_model
USER_MODEL = get_user_model()
except ImportError:
from django.contrib.auth.models import User
get_user_model = lambda: User
USER_MODEL = get_user_model()
USER_MODEL = User
class NoActiveTermsOfService(ValidationError):
@ -30,13 +28,19 @@ class TermsOfServiceManager(models.Manager):
try:
return self.get(active=True)
except self.model.DoesNotExist:
raise NoActiveTermsOfService(u'Please create an active Terms-of-Service')
raise NoActiveTermsOfService(
u'Please create an active Terms-of-Service'
)
class TermsOfService(BaseModel):
active = models.BooleanField(default=False,
verbose_name=_('active'),
help_text=_(u'Only one terms of service is allowed to be active'))
active = models.BooleanField(
default=False,
verbose_name=_('active'),
help_text=_(
u'Only one terms of service is allowed to be active'
)
)
content = models.TextField(verbose_name=_('content'), blank=True)
objects = TermsOfServiceManager()
@ -62,7 +66,9 @@ class TermsOfService(BaseModel):
if not TermsOfService.objects\
.exclude(id=self.id)\
.filter(active=True):
raise NoActiveTermsOfService(u'One of the terms of service must be marked active')
raise NoActiveTermsOfService(
u'One of the terms of service must be marked active'
)
super(TermsOfService, self).save(*args, **kwargs)
@ -77,6 +83,7 @@ class UserAgreement(BaseModel):
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
return UserAgreement.objects.filter(
terms_of_service=TermsOfService.objects.get_current_tos(),
user=user,
).exists()

View file

@ -2,9 +2,9 @@ from django.conf.urls import url, patterns
from tos.views import check_tos, TosView
urlpatterns = patterns('',
# Terms of Service conform
url(r'^confirm/$', check_tos, name='tos_check_tos'),
# Terms of Service conform
url(r'^confirm/$', check_tos, name='tos_check_tos'),
# Terms of service simple display
url(r'^$', TosView.as_view(), name='tos'),
)
# Terms of service simple display
url(r'^$', TosView.as_view(), name='tos'),
)

View file

@ -15,15 +15,13 @@ from django.utils.translation import ugettext_lazy as _
from tos.models import has_user_agreed_latest_tos, TermsOfService, UserAgreement
# Django 1.4 compatability
try:
from django.contrib.auth import get_user_model
USER_MODEL = get_user_model()
except ImportError:
from django.contrib.auth.models import User
get_user_model = lambda: User
USER = get_user_model()
USER_MODEL = User
class TosView(TemplateView):
@ -74,7 +72,10 @@ def check_tos(request, template_name='tos/tos_check.html',
return HttpResponseRedirect(redirect_to)
else:
messages.error(request, _(u"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,
@ -97,7 +98,8 @@ def login(request, template_name='registration/login.html',
redirect_to = _redirect_to(redirect_to)
# Okay, security checks complete. Check to see if user agrees to terms
# Okay, security checks complete. Check to see if user agrees
# to terms
user = form.get_user()
if has_user_agreed_latest_tos(user):
@ -115,7 +117,8 @@ def login(request, template_name='registration/login.html',
request.session['tos_user'] = user.pk
# Pass the used backend as well since django will require it
# and it can only be optained by calling authenticate, but we got no credentials in check_tos.
# and it can only be optained by calling authenticate, but we
# got no credentials in check_tos.
# see: https://docs.djangoproject.com/en/1.6/topics/auth/default/#how-to-log-a-user-in
request.session['tos_backend'] = user.backend