mirror of
https://gitea.mueller.network/extern/django-helpdesk.git
synced 2024-12-25 16:18:51 +01:00
Populate user settings w/ data migration, Django1.7 compatible, fix #276
This commit is contained in:
parent
7b07f56972
commit
f0156cfcb3
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),
|
||||
]
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user