django-helpdeskmig/helpdesk/views/kb.py

64 lines
2.1 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 HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
2011-09-01 13:01:03 +02:00
from helpdesk import settings as helpdesk_settings
from helpdesk.models import KBCategory, KBItem
def index(request):
category_list = KBCategory.objects.all()
# 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': category_list,
'helpdesk_settings': helpdesk_settings,
})
def category(request, slug):
category = get_object_or_404(KBCategory, slug__iexact=slug)
items = category.kbitem_set.all()
2020-01-07 13:33:06 +01:00
selected_item = request.GET.get('kbitem', None)
try:
selected_item = int(selected_item)
except ValueError:
pass
2016-10-21 17:14:12 +02:00
return render(request, 'helpdesk/kb_category.html', {
'category': category,
'items': items,
2020-01-07 13:33:06 +01:00
'selected_item': selected_item,
2016-10-21 17:14:12 +02:00
'helpdesk_settings': helpdesk_settings,
})
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())