-
Notifications
You must be signed in to change notification settings - Fork 288
DocsDevConfig
Ryan Northey edited this page May 3, 2016
·
14 revisions
You can get and set configuration options using Pootle's config app.
Using this system, configuration can be set at 3 levels
- System-wide
- Per-model
- Per-object
Keys do not have to be unique and are a maximum of 255 chars.
Values are stored as JSON strings, so can be any JSON-serializable value.
>>> from pootle.core.delegate import config
>>>
>>> config.get()
[]
>>> from pootle_project.models import Project
>>>
>>> config.get(Project)
[]
>>> project = Project.objects.first()
>>> config.get(Project, instance=project)
[]
>>> type(config.get())
<class 'pootle_config.managers.ConfigQuerySet'>
>>> config.get().filter(key="foo")
[]
>>> config.get().set_config("foo", "global bar")
>>> config.get(Project).get_config("foo")
u'global bar'
>>> config.get(Project).set_config("foo", "model bar")
>>> config.get(Project).get_config("foo")
u'model bar'
>>> config.get(Project, instance=project).set_config("foo", "instance bar")
>>> config.get(Project, instance=project).get_config("foo")
u'instance bar'
>>> config.get(Project, instance=project).set_config("foo", "bar2")
>>> config.get(Project, instance=project).get_config("foo")
u'bar2'
>>> config.get().append_config("appended_foo", "appending1")
>>> config.get().append_config("appended_foo", "appending1")
>>> config.get().append_config("appended_foo", "appending2")
>>> config.get().append_config("appended_foo", "appending3")
>>> config.get(Project).get_config("foo")
u'global bar'
>>> config.get(Project).get_config("foo")
u'model bar'
>>> config.get(Project, instance=project).get_config("foo")
u'instance bar'
>>> config.get(Project).get_config("appended_foo")
Traceback (most recent call last):
...
MultipleObjectsReturned: get() returned more than one Config -- it returned 4!
>>> config.get().set_config("global_foo", "bar")
>>> config.get().none().get_config("global_foo")
u"bar"
>>> config.get(Project).set_config("project_foo", "bar2")
>>> config.get(Project).none().get_config("project_foo")
u"bar2"
>>> config.get(Project).none().query_model
<class 'pootle_project.models.Project'>
>>> project == config.get(Project, instance=project).none().query_model
True
>>> from django.core.exceptions import ValidationError
>>>
>>> from pootle.core.plugin import getter
>>> from pootle_config.delegate import (
... config_should_not_be_set, config_should_not_be_appended)
>>>
>>> @getter([config_should_not_be_set, config_should_not_be_appended])
>>> def config_validator(**kwargs):
>>> if not isinstance(kwargs["value"], list):
>>> return ValidationError("Config '%s' must be a list" % kwargs["key"])
>>> from pootle_project.models import Project
>>>
>>> @getter(config_should_not_be_appended, sender=Project)
>>> def config_validator(**kwargs):
>>> raise ValidationError(
... "Config keys for '%s' must be unique" % kwargs["sender"])