Migrate serverconf values to use picklefield
This commit is contained in:
parent
183e759824
commit
c9780d3a0d
3 changed files with 21 additions and 9 deletions
|
|
@ -2,8 +2,21 @@
|
||||||
# Generated by Django 1.11.16 on 2019-01-28 23:11
|
# Generated by Django 1.11.16 on 2019-01-28 23:11
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from base64 import b64encode
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
import evennia.utils.picklefield
|
import evennia.utils.picklefield
|
||||||
|
from pickle import dumps, loads
|
||||||
|
from evennia.utils.utils import to_bytes
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
def forwards(apps, schema_editor):
|
||||||
|
ServerConfig = apps.get_model('server', 'ServerConfig')
|
||||||
|
for conf in ServerConfig.objects.all():
|
||||||
|
# picklefield requires base64 encoding
|
||||||
|
value = loads(to_bytes(conf.db_value), encoding='bytes') # py2->py3 byte encoding
|
||||||
|
conf.db_value = b64encode(dumps(deepcopy(value), protocol=4)).decode()
|
||||||
|
conf.save(update_fields=['db_value'])
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
@ -13,9 +26,11 @@ class Migration(migrations.Migration):
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
migrations.RunPython(forwards, migrations.RunPython.noop),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='serverconfig',
|
model_name='serverconfig',
|
||||||
name='db_value',
|
name='db_value',
|
||||||
field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
|
field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
|
||||||
),
|
),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import pickle
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from evennia.utils.idmapper.models import WeakSharedMemoryModel
|
from evennia.utils.idmapper.models import WeakSharedMemoryModel
|
||||||
from evennia.utils import logger, utils
|
from evennia.utils import logger, utils
|
||||||
|
from evennia.utils.dbserialize import to_pickle, from_pickle
|
||||||
from evennia.server.manager import ServerConfigManager
|
from evennia.server.manager import ServerConfigManager
|
||||||
from evennia.utils import picklefield
|
from evennia.utils import picklefield
|
||||||
|
|
||||||
|
|
@ -86,7 +87,7 @@ class ServerConfig(WeakSharedMemoryModel):
|
||||||
#@property
|
#@property
|
||||||
def __value_get(self):
|
def __value_get(self):
|
||||||
"Getter. Allows for value = self.value"
|
"Getter. Allows for value = self.value"
|
||||||
return pickle.loads(utils.to_bytes(self.db_value))
|
return from_pickle(self.db_value, db_obj=self)
|
||||||
|
|
||||||
#@value.setter
|
#@value.setter
|
||||||
def __value_set(self, value):
|
def __value_set(self, value):
|
||||||
|
|
@ -95,7 +96,7 @@ class ServerConfig(WeakSharedMemoryModel):
|
||||||
# we have to protect against storing db objects.
|
# we have to protect against storing db objects.
|
||||||
logger.log_err("ServerConfig cannot store db objects! (%s)" % value)
|
logger.log_err("ServerConfig cannot store db objects! (%s)" % value)
|
||||||
return
|
return
|
||||||
self.db_value = pickle.dumps(value)
|
self.db_value = to_pickle(value)
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
#@value.deleter
|
#@value.deleter
|
||||||
|
|
@ -113,8 +114,8 @@ class ServerConfig(WeakSharedMemoryModel):
|
||||||
# ServerConfig other methods
|
# ServerConfig other methods
|
||||||
#
|
#
|
||||||
|
|
||||||
def __unicode__(self):
|
def __repr__(self):
|
||||||
return "%s : %s" % (self.key, self.value)
|
return "<{} {}>".format(self.__class__.__name__, self.key, self.value)
|
||||||
|
|
||||||
def store(self, key, value):
|
def store(self, key, value):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -46,17 +46,13 @@ from django.utils.html import format_html
|
||||||
|
|
||||||
from evennia.utils.dbserialize import from_pickle, to_pickle
|
from evennia.utils.dbserialize import from_pickle, to_pickle
|
||||||
from future.utils import with_metaclass
|
from future.utils import with_metaclass
|
||||||
|
from pickle import loads, dumps
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from django.utils.encoding import force_unicode as force_text
|
from django.utils.encoding import force_unicode as force_text
|
||||||
|
|
||||||
# python 3.x does not have cPickle module
|
|
||||||
try:
|
|
||||||
from pickle import loads, dumps # cpython 2.x
|
|
||||||
except ImportError:
|
|
||||||
from pickle import loads, dumps # cpython 3.x, other interpreters
|
|
||||||
|
|
||||||
DEFAULT_PROTOCOL = 4
|
DEFAULT_PROTOCOL = 4
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue