Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added LOGIN_REQUIRED feature

  • Loading branch information...
commit 2198d6b7588473cfd36dc4eb6cfa4fab43bd0662 1 parent 1b0e32b
@griphiam griphiam authored
Showing with 29 additions and 1 deletion.
  1. +7 −0 docs/settings.rst
  2. +2 −0  knowledge/settings.py
  3. +20 −1 knowledge/views.py
View
7 docs/settings.rst
@@ -13,6 +13,13 @@ Default ``False``. If ``True``, users who are not logged in can ask questions. I
``False`` only registered and logged in users can ask questions.
+KNOWLEDGE_LOGIN_REQUIRED
+------------------------
+
+Default ``False``. If ``True`` users that are not authenticated are redirected to
+LOGIN_URL.
+
+
KNOWLEDGE_AUTO_PUBLICIZE
------------------------
View
2  knowledge/settings.py
@@ -1,6 +1,8 @@
from django.conf import settings
# crowd control
+LOGIN_REQUIRED = getattr(settings, 'KNOWLEDGE_LOGIN_REQUIRED', False)
+LOGIN_URL = getattr(settings, 'LOGIN_URL', '/accounts/login/')
ALLOW_ANONYMOUS = getattr(settings, 'KNOWLEDGE_ALLOW_ANONYMOUS', False)
AUTO_PUBLICIZE = getattr(settings, 'KNOWLEDGE_AUTO_PUBLICIZE', False)
FREE_RESPONSE = getattr(settings, 'KNOWLEDGE_FREE_RESPONSE', True)
View
21 knowledge/views.py
@@ -1,6 +1,6 @@
import settings
-from django.http import Http404
+from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q
@@ -25,6 +25,10 @@
def get_my_questions(request):
+
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
+
if request.user.is_anonymous():
return None
else:
@@ -35,6 +39,9 @@ def get_my_questions(request):
def knowledge_index(request,
template='django_knowledge/index.html'):
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
+
questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question')[0:20]
# this is for get_responses()
@@ -53,6 +60,9 @@ def knowledge_list(request,
template='django_knowledge/list.html',
Form=QuestionForm):
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
+
search = request.GET.get('title', None)
questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question')
@@ -89,6 +99,9 @@ def knowledge_thread(request,
slug=None,
template='django_knowledge/thread.html',
Form=ResponseForm):
+
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
try:
question = Question.objects.can_view(request.user)\
@@ -144,6 +157,9 @@ def knowledge_moderate(
"""
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
+
if request.method != 'POST':
raise Http404
@@ -181,6 +197,9 @@ def knowledge_ask(request,
template='django_knowledge/ask.html',
Form=QuestionForm):
+ if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
+ return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
+
if request.method == 'POST':
form = Form(request.user, request.POST)
if form and form.is_valid():
Please sign in to comment.
Something went wrong with that request. Please try again.