2008-05-21 23:16:44 +02:00
|
|
|
"""
|
2011-01-26 00:08:41 +01:00
|
|
|
django-helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-05-21 23:16:44 +02:00
|
|
|
|
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
views/kb.py - Public-facing knowledgebase views. The knowledgebase is a
|
2008-05-21 23:16:44 +02:00
|
|
|
simple categorised question/answer system to show common
|
|
|
|
resolutions to common problems.
|
|
|
|
"""
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2020-01-27 17:37:59 +01:00
|
|
|
from django.http import HttpResponseRedirect, Http404
|
2016-06-21 07:18:06 +02:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2020-01-13 21:15:22 +01:00
|
|
|
from django.views.decorators.clickjacking import xframe_options_exempt
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2011-09-01 13:01:03 +02:00
|
|
|
from helpdesk import settings as helpdesk_settings
|
2020-01-27 17:37:59 +01:00
|
|
|
from helpdesk import user
|
2008-05-21 23:16:44 +02:00
|
|
|
from helpdesk.models import KBCategory, KBItem
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-21 23:16:44 +02:00
|
|
|
def index(request):
|
2020-01-27 17:37:59 +01:00
|
|
|
huser = user.huser_from_request(request)
|
2008-08-19 10:50:38 +02:00
|
|
|
# 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', {
|
2020-01-27 19:45:15 +01:00
|
|
|
'kb_categories': huser.get_allowed_kb_categories(),
|
2016-10-21 17:14:12 +02:00
|
|
|
'helpdesk_settings': helpdesk_settings,
|
|
|
|
})
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2020-01-08 19:36:48 +01:00
|
|
|
def category(request, slug, iframe=False):
|
2008-05-21 23:16:44 +02:00
|
|
|
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
|
|
|
})
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2020-01-20 14:17:23 +01:00
|
|
|
|
2020-01-13 21:15:22 +01:00
|
|
|
@xframe_options_exempt
|
2020-01-08 19:36:48 +01:00
|
|
|
def category_iframe(request, slug):
|
|
|
|
return category(request, slug, iframe=True)
|
|
|
|
|
|
|
|
|
2008-05-21 23:16:44 +02:00
|
|
|
def vote(request, item):
|
|
|
|
item = get_object_or_404(KBItem, pk=item)
|
|
|
|
vote = request.GET.get('vote', None)
|
2020-01-07 13:26:02 +01:00
|
|
|
if vote == 'up':
|
|
|
|
if not item.voted_by.filter(pk=request.user.pk):
|
2018-10-17 19:39:43 +02:00
|
|
|
item.votes += 1
|
2020-01-07 13:26:02 +01:00
|
|
|
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()
|
2008-05-21 23:16:44 +02:00
|
|
|
return HttpResponseRedirect(item.get_absolute_url())
|