django-helpdesk/helpdesk/views/kb.py

83 lines
2.8 KiB
Python
Raw Normal View History

"""
django-helpdesk - A Django powered ticket tracker for small enterprise.
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
views/kb.py - Public-facing knowledgebase views. The knowledgebase is a
simple categorised question/answer system to show common
resolutions to common problems.
"""
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.views.decorators.clickjacking import xframe_options_exempt
from helpdesk import settings as helpdesk_settings, user
from helpdesk.models import KBCategory, KBItem
def index(request):
2020-01-27 17:37:59 +01:00
huser = user.huser_from_request(request)
# TODO: It'd be great to have a list of most popular items here.
2016-10-21 17:14:12 +02:00
return render(request, 'helpdesk/kb_index.html', {
'kb_categories': huser.get_allowed_kb_categories(),
2016-10-21 17:14:12 +02:00
'helpdesk_settings': helpdesk_settings,
})
2020-01-08 19:36:48 +01:00
def category(request, slug, iframe=False):
category = get_object_or_404(KBCategory, slug__iexact=slug)
2020-01-27 17:37:59 +01:00
if not user.huser_from_request(request).can_access_kbcategory(category):
raise Http404
2020-02-25 14:46:21 +01:00
items = category.kbitem_set.filter(enabled=True)
2020-01-07 13:33:06 +01:00
selected_item = request.GET.get('kbitem', None)
try:
selected_item = int(selected_item)
2020-01-08 18:39:41 +01:00
except TypeError:
pass
qparams = request.GET.copy()
try:
del qparams['kbitem']
except KeyError:
2020-01-07 13:33:06 +01:00
pass
2020-01-08 19:36:48 +01:00
template = 'helpdesk/kb_category.html'
if iframe:
template = 'helpdesk/kb_category_iframe.html'
2020-01-08 22:57:08 +01:00
staff = request.user.is_authenticated and request.user.is_staff
2020-01-08 19:36:48 +01:00
return render(request, template, {
2016-10-21 17:14:12 +02:00
'category': category,
'items': items,
2020-01-07 13:33:06 +01:00
'selected_item': selected_item,
2020-01-08 18:39:41 +01:00
'query_param_string': qparams.urlencode(),
2016-10-21 17:14:12 +02:00
'helpdesk_settings': helpdesk_settings,
2020-01-08 19:36:48 +01:00
'iframe': iframe,
2020-01-08 22:57:08 +01:00
'staff': staff,
2016-10-21 17:14:12 +02:00
})
2020-01-20 14:17:23 +01:00
@xframe_options_exempt
2020-01-08 19:36:48 +01:00
def category_iframe(request, slug):
return category(request, slug, iframe=True)
def vote(request, item):
item = get_object_or_404(KBItem, pk=item)
vote = request.GET.get('vote', None)
if vote == 'up':
if not item.voted_by.filter(pk=request.user.pk):
item.votes += 1
item.voted_by.add(request.user.pk)
item.recommendations += 1
if item.downvoted_by.filter(pk=request.user.pk):
item.votes -= 1
item.downvoted_by.remove(request.user.pk)
if vote == 'down':
if not item.downvoted_by.filter(pk=request.user.pk):
item.votes += 1
item.downvoted_by.add(request.user.pk)
item.recommendations -= 1
if item.voted_by.filter(pk=request.user.pk):
item.votes -= 1
item.voted_by.remove(request.user.pk)
item.save()
return HttpResponseRedirect(item.get_absolute_url())