mirror of
https://github.com/django-helpdesk/django-helpdesk.git
synced 2025-01-19 04:19:54 +01:00
Merge pull request #287 from djcoin/master
Populate user settings w/ data migration, Django1.7 compatible fix #276
This commit is contained in:
commit
0732e1f2d3
50
helpdesk/migrations/0003_populate_usersettings.py
Normal file
50
helpdesk/migrations/0003_populate_usersettings.py
Normal file
@ -0,0 +1,50 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import models, migrations
|
||||
|
||||
from helpdesk.settings import DEFAULT_USER_SETTINGS
|
||||
|
||||
|
||||
def picke_settings(data):
|
||||
"""Pickling as defined at migration's creation time"""
|
||||
import cPickle
|
||||
from helpdesk.lib import b64encode
|
||||
return b64encode(cPickle.dumps(data))
|
||||
|
||||
|
||||
# https://docs.djangoproject.com/en/1.7/topics/migrations/#data-migrations
|
||||
def populate_usersettings(apps, schema_editor):
|
||||
"""Create a UserSettings entry for each existing user.
|
||||
This will only happen once (at install time, or at upgrade)
|
||||
when the UserSettings model doesn't already exist."""
|
||||
|
||||
_User = get_user_model()
|
||||
User = apps.get_model(_User._meta.app_label, _User._meta.model_name)
|
||||
|
||||
# Import historical version of models
|
||||
UserSettings = apps.get_model("helpdesk", "UserSettings")
|
||||
|
||||
settings_pickled = picke_settings(DEFAULT_USER_SETTINGS)
|
||||
|
||||
for u in User.objects.all():
|
||||
try:
|
||||
UserSettings.objects.get(user=u)
|
||||
except UserSettings.DoesNotExist:
|
||||
UserSettings.objects.create(user=u, settings_pickled=settings_pickled)
|
||||
|
||||
|
||||
noop = lambda *args, **kwargs: None
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('helpdesk', '0002_socks_proxy'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(populate_usersettings, reverse_code=noop),
|
||||
]
|
||||
|
||||
|
44
helpdesk/migrations/0004_initial_data_import.py
Normal file
44
helpdesk/migrations/0004_initial_data_import.py
Normal file
@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
from sys import path
|
||||
|
||||
from django.db import models, migrations
|
||||
from django.core import serializers
|
||||
|
||||
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
|
||||
fixture_filename = 'emailtemplate.json'
|
||||
|
||||
def deserialize_fixture():
|
||||
fixture_file = os.path.join(fixture_dir, fixture_filename)
|
||||
|
||||
with open(fixture_file, 'rb') as fixture:
|
||||
return list(serializers.deserialize('json', fixture, ignorenonexistent=True))
|
||||
|
||||
|
||||
def load_fixture(apps, schema_editor):
|
||||
objects = deserialize_fixture()
|
||||
|
||||
for obj in objects:
|
||||
obj.save()
|
||||
|
||||
|
||||
def unload_fixture(apps, schema_editor):
|
||||
"Delete all EmailTemplate objects"
|
||||
|
||||
objects = deserialize_fixture()
|
||||
|
||||
EmailTemplate = apps.get_model("helpdesk", "emailtemplate")
|
||||
EmailTemplate.objects.filter(pk__in=[ obj.object.pk for obj in objects ]).delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('helpdesk', '0003_populate_usersettings'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
|
||||
]
|
@ -1027,7 +1027,7 @@ class UserSettings(models.Model):
|
||||
verbose_name_plural = _('User Settings')
|
||||
|
||||
|
||||
def create_usersettings(sender, created_models=[], instance=None, created=False, **kwargs):
|
||||
def create_usersettings(sender, instance, created, **kwargs):
|
||||
"""
|
||||
Helper function to create UserSettings instances as
|
||||
required, eg when we first create the UserSettings database
|
||||
@ -1037,30 +1037,16 @@ def create_usersettings(sender, created_models=[], instance=None, created=False,
|
||||
'DoesNotExist: UserSettings matching query does not exist.' errors.
|
||||
"""
|
||||
from helpdesk.settings import DEFAULT_USER_SETTINGS
|
||||
if sender == settings.AUTH_USER_MODEL and created:
|
||||
# This is a new user, so lets create their settings entry.
|
||||
s, created = UserSettings.objects.get_or_create(user=instance, defaults={'settings': DEFAULT_USER_SETTINGS})
|
||||
s.save()
|
||||
elif UserSettings in created_models:
|
||||
User = get_user_model()
|
||||
# We just created the UserSettings model, lets create a UserSettings
|
||||
# entry for each existing user. This will only happen once (at install
|
||||
# time, or at upgrade) when the UserSettings model doesn't already
|
||||
# exist.
|
||||
for u in User.objects.all():
|
||||
try:
|
||||
s = UserSettings.objects.get(user=u)
|
||||
except UserSettings.DoesNotExist:
|
||||
s = UserSettings(user=u, settings=DEFAULT_USER_SETTINGS)
|
||||
s.save()
|
||||
if created:
|
||||
UserSettings.objects.create(user=instance, settings=DEFAULT_USER_SETTINGS)
|
||||
|
||||
models.signals.post_syncdb.connect(create_usersettings)
|
||||
try:
|
||||
models.signals.post_save.connect(create_usersettings, sender=settings.AUTH_USER_MODEL)
|
||||
except:
|
||||
signal_user = get_user_model()
|
||||
models.signals.post_save.connect(create_usersettings, sender=signal_user)
|
||||
|
||||
|
||||
class IgnoreEmail(models.Model):
|
||||
"""
|
||||
This model lets us easily ignore e-mails from certain senders when
|
||||
|
@ -5,6 +5,8 @@ from django.test.client import Client
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
class TicketBasicsTestCase(TestCase):
|
||||
fixtures = ['emailtemplate.json']
|
||||
|
||||
def setUp(self):
|
||||
self.queue_public = Queue.objects.create(title='Queue 1', slug='q1', allow_public_submission=True, new_ticket_cc='new.public@example.com', updated_ticket_cc='update.public@example.com')
|
||||
self.queue_private = Queue.objects.create(title='Queue 2', slug='q2', allow_public_submission=False, new_ticket_cc='new.private@example.com', updated_ticket_cc='update.private@example.com')
|
||||
|
20
quicktest.py
20
quicktest.py
@ -1,8 +1,11 @@
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
import django
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class QuickDjangoTest(object):
|
||||
"""
|
||||
A quick way to run the Django test suite without a fully-configured project.
|
||||
@ -25,6 +28,14 @@ class QuickDjangoTest(object):
|
||||
'django.contrib.humanize',
|
||||
'bootstrapform',
|
||||
)
|
||||
MIDDLEWARE_CLASSES = [
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.apps = args
|
||||
@ -40,8 +51,7 @@ class QuickDjangoTest(object):
|
||||
"""
|
||||
Figure out which version of Django's test suite we have to play with.
|
||||
"""
|
||||
from django import VERSION
|
||||
if VERSION[0] == 1 and VERSION[1] >= 2:
|
||||
if django.VERSION >= (1, 2):
|
||||
return 'new'
|
||||
else:
|
||||
return 'old'
|
||||
@ -64,6 +74,7 @@ class QuickDjangoTest(object):
|
||||
"""
|
||||
Fire up the Django test suite developed for version 1.2
|
||||
"""
|
||||
|
||||
settings.configure(
|
||||
DEBUG = True,
|
||||
DATABASES = {
|
||||
@ -77,8 +88,13 @@ class QuickDjangoTest(object):
|
||||
}
|
||||
},
|
||||
INSTALLED_APPS = self.INSTALLED_APPS + self.apps,
|
||||
MIDDLEWARE_CLASSES = self.MIDDLEWARE_CLASSES,
|
||||
ROOT_URLCONF = self.apps[0] + '.urls',
|
||||
)
|
||||
|
||||
if django.VERSION >= (1, 7):
|
||||
django.setup()
|
||||
|
||||
from django.test.simple import DjangoTestSuiteRunner
|
||||
failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1)
|
||||
if failures:
|
||||
|
Loading…
Reference in New Issue
Block a user