django-helpdesk/helpdesk/tests/helpers.py

89 lines
2.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import sys
2016-10-21 17:14:12 +02:00
from django.contrib.auth import get_user_model
from helpdesk.models import Ticket, Queue, UserSettings
User = get_user_model()
def get_staff_user(username='helpdesk.staff', password='password'):
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User.objects.create_user(username=username, password=password, email='staff@example.com')
user.is_staff = True
user.save()
else:
user.set_password(password)
user.save()
return user
def reload_urlconf(urlconf=None):
2015-11-18 15:07:33 +01:00
2017-12-28 15:22:57 +01:00
from django.utils import six
2017-12-28 15:26:14 +01:00
if six.PY2:
from imp import reload
else:
2017-12-28 15:22:57 +01:00
from importlib import reload
2015-11-18 15:07:33 +01:00
if urlconf is None:
from django.conf import settings
urlconf = settings.ROOT_URLCONF
if HELPDESK_URLCONF in sys.modules:
reload(sys.modules[HELPDESK_URLCONF])
if urlconf in sys.modules:
reload(sys.modules[urlconf])
from django.urls import clear_url_caches
clear_url_caches()
def update_user_settings(user, **kwargs):
usersettings = user.usersettings_helpdesk
settings = usersettings.settings
settings.update(kwargs)
usersettings.settings = settings
usersettings.save()
def delete_user_settings(user, *args):
usersettings = user.usersettings_helpdesk
settings = usersettings.settings
for setting in args:
if setting in settings:
del settings[setting]
usersettings.settings = settings
usersettings.save()
def create_ticket(**kwargs):
q = kwargs.get('queue', None)
if q is None:
try:
q = Queue.objects.all()[0]
except IndexError:
q = Queue.objects.create(title='Test Q', slug='test', )
data = {
'title': "I wish to register a complaint",
'queue': q,
}
data.update(kwargs)
2017-10-30 08:17:40 +01:00
return Ticket.objects.create(**data)
HELPDESK_URLCONF = 'helpdesk.urls'
2018-08-30 12:03:54 +02:00
def print_response(response, stdout=False):
content = response.content.decode()
if stdout:
print(content)
else:
with open("response.html", "w") as f: # pragma: no cover
f.write(content) # pragma: no cover