Merge pull request #354 from reduxionist/bug/per-queue-membership-but-no-memberships

Fix QueueMembership bug revealed by django.test's DiscoverRunner
This commit is contained in:
Ross Poulton 2015-11-17 12:00:19 +11:00
commit aae2501cab
2 changed files with 33 additions and 20 deletions

View File

@ -17,6 +17,7 @@ class PerQueueStaffMembershipTestCase(TestCase):
and user_2 with access to queue_2 containing 2 tickets and user_2 with access to queue_2 containing 2 tickets
and superuser who should be able to access both queues and superuser who should be able to access both queues
""" """
self.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP
settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = True settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = True
self.client = Client() self.client = Client()
User = get_user_model() User = get_user_model()
@ -59,6 +60,12 @@ class PerQueueStaffMembershipTestCase(TestCase):
assigned_to=user, assigned_to=user,
) )
def tearDown(self):
"""
Reset HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP to original value
"""
settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = self.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP
def test_dashboard_ticket_counts(self): def test_dashboard_ticket_counts(self):
""" """
Check that the regular users' dashboard only shows 1 of the 2 queues, Check that the regular users' dashboard only shows 1 of the 2 queues,
@ -213,9 +220,3 @@ class PerQueueStaffMembershipTestCase(TestCase):
3, 3,
'Queue choices were improperly limited by queue membership for a superuser' 'Queue choices were improperly limited by queue membership for a superuser'
) )
def tearDown(self):
"""
Don't interfere with subsequent tests that do not expect this setting
"""
settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP = False

View File

@ -39,7 +39,7 @@ except ImportError:
from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm from helpdesk.forms import TicketForm, UserSettingsForm, EmailIgnoreForm, EditTicketForm, TicketCCForm, EditFollowUpForm, TicketDependencyForm
from helpdesk.lib import send_templated_mail, query_to_dict, apply_query, safe_template_context from helpdesk.lib import send_templated_mail, query_to_dict, apply_query, safe_template_context
from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency from helpdesk.models import Ticket, Queue, FollowUp, TicketChange, PreSetReply, Attachment, SavedSearch, IgnoreEmail, TicketCC, TicketDependency, QueueMembership
from helpdesk import settings as helpdesk_settings from helpdesk import settings as helpdesk_settings
if helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE: if helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
@ -78,9 +78,12 @@ def dashboard(request):
) )
limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser
if limit_queues_by_user: if limit_queues_by_user:
try:
unassigned_tickets = unassigned_tickets.filter( unassigned_tickets = unassigned_tickets.filter(
queue__in=request.user.queuemembership.queues.all(), queue__in=request.user.queuemembership.queues.all(),
) )
except QueueMembership.DoesNotExist:
unassigned_tickets = unassigned_tickets.none()
# all tickets, reported by current user # all tickets, reported by current user
all_tickets_reported_by_current_user = '' all_tickets_reported_by_current_user = ''
@ -92,9 +95,12 @@ def dashboard(request):
Tickets = Ticket.objects Tickets = Ticket.objects
if limit_queues_by_user: if limit_queues_by_user:
try:
Tickets = Tickets.filter( Tickets = Tickets.filter(
queue__in=request.user.queuemembership.queues.all(), queue__in=request.user.queuemembership.queues.all(),
) )
except QueueMembership.DoesNotExist:
Tickets = Tickets.none()
basic_ticket_stats = calc_basic_ticket_stats(Tickets) basic_ticket_stats = calc_basic_ticket_stats(Tickets)
# The following query builds a grid of queues & ticket statuses, # The following query builds a grid of queues & ticket statuses,
@ -1001,9 +1007,12 @@ def run_report(request, report):
report_queryset = Ticket.objects.all().select_related() report_queryset = Ticket.objects.all().select_related()
limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser limit_queues_by_user = helpdesk_settings.HELPDESK_ENABLE_PER_QUEUE_STAFF_MEMBERSHIP and not request.user.is_superuser
if limit_queues_by_user: if limit_queues_by_user:
try:
report_queryset = report_queryset.filter( report_queryset = report_queryset.filter(
queue__in=request.user.queuemembership.queues.all(), queue__in=request.user.queuemembership.queues.all(),
) )
except QueueMembership.DoesNotExist:
report_queryset = report_queryset.none()
from_saved_query = False from_saved_query = False
saved_query = None saved_query = None
@ -1065,9 +1074,12 @@ def run_report(request, report):
col1heading = _('User') col1heading = _('User')
queue_options = Queue.objects.all() queue_options = Queue.objects.all()
if limit_queues_by_user: if limit_queues_by_user:
try:
queue_options = queue_options.filter( queue_options = queue_options.filter(
pk__in=request.user.queuemembership.queues.all(), pk__in=request.user.queuemembership.queues.all(),
) )
except QueueMembership.DoesNotExist:
queue_options = queue_options.none()
possible_options = [q.title.encode('utf-8') for q in queue_options] possible_options = [q.title.encode('utf-8') for q in queue_options]
charttype = 'bar' charttype = 'bar'