Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
Added LOGIN_REQUIRED feature
Browse files Browse the repository at this point in the history
  • Loading branch information
markphilpot committed Jul 19, 2012
1 parent 1b0e32b commit 2198d6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/settings.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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. ``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 KNOWLEDGE_AUTO_PUBLICIZE
------------------------ ------------------------


Expand Down
2 changes: 2 additions & 0 deletions knowledge/settings.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.conf import settings from django.conf import settings


# crowd control # 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) ALLOW_ANONYMOUS = getattr(settings, 'KNOWLEDGE_ALLOW_ANONYMOUS', False)
AUTO_PUBLICIZE = getattr(settings, 'KNOWLEDGE_AUTO_PUBLICIZE', False) AUTO_PUBLICIZE = getattr(settings, 'KNOWLEDGE_AUTO_PUBLICIZE', False)
FREE_RESPONSE = getattr(settings, 'KNOWLEDGE_FREE_RESPONSE', True) FREE_RESPONSE = getattr(settings, 'KNOWLEDGE_FREE_RESPONSE', True)
Expand Down
21 changes: 20 additions & 1 deletion knowledge/views.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
import settings 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.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q from django.db.models import Q
Expand All @@ -25,6 +25,10 @@




def get_my_questions(request): 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(): if request.user.is_anonymous():
return None return None
else: else:
Expand All @@ -35,6 +39,9 @@ def get_my_questions(request):
def knowledge_index(request, def knowledge_index(request,
template='django_knowledge/index.html'): 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)\ questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question')[0:20] .prefetch_related('responses__question')[0:20]
# this is for get_responses() # this is for get_responses()
Expand All @@ -53,6 +60,9 @@ def knowledge_list(request,
template='django_knowledge/list.html', template='django_knowledge/list.html',
Form=QuestionForm): 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) search = request.GET.get('title', None)
questions = Question.objects.can_view(request.user)\ questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question') .prefetch_related('responses__question')
Expand Down Expand Up @@ -89,6 +99,9 @@ def knowledge_thread(request,
slug=None, slug=None,
template='django_knowledge/thread.html', template='django_knowledge/thread.html',
Form=ResponseForm): Form=ResponseForm):

if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)


try: try:
question = Question.objects.can_view(request.user)\ question = Question.objects.can_view(request.user)\
Expand Down Expand Up @@ -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': if request.method != 'POST':
raise Http404 raise Http404


Expand Down Expand Up @@ -181,6 +197,9 @@ def knowledge_ask(request,
template='django_knowledge/ask.html', template='django_knowledge/ask.html',
Form=QuestionForm): 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': if request.method == 'POST':
form = Form(request.user, request.POST) form = Form(request.user, request.POST)
if form and form.is_valid(): if form and form.is_valid():
Expand Down

0 comments on commit 2198d6b

Please sign in to comment.