Skip to content

DocsDevConfig

Ryan Northey edited this page May 3, 2016 · 14 revisions

Plugin Configuration

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.

Retrieving a configuration object

>>> 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")
[]

Setting configuration

>>> config.get().set_config("foo", "global bar")
>>> config.get().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'

Appending configuration

>>> config.get(Project).append_config("appended_foo", "appending1")
>>> config.get(Project).append_config("appended_foo", "appending1")
>>> config.get(Project).append_config("appended_foo", "appending2")
>>> config.get(Project).append_config("appended_foo", "appending3")
>>> config.get(Project).get_config("appended_foo")
Traceback (most recent call last):
...
MultipleObjectsReturned: get() returned more than one Config -- it returned 4!
>>> config.get(Project).set_config("appended_foo", "something else")
Traceback (most recent call last):
...
MultipleObjectsReturned: get() returned more than one Config -- it returned 4!

Getting configuration

>>> config.get().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'

Listing configuration

>>> config.get(Project).list_config("appended_foo")
[("appended_foo", "appending1"), ("appended_foo", "appending1"), ("appended_foo", "appending2"), ("appended_foo", "appending3")]

Queryset chaining

>>> 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

Validating configuration before saving

>>> 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 list_config_validator(**kwargs):
>>>     if kwargs["key"] == "foo":
>>>         if not isinstance(kwargs["value"], list):
>>>             return ValidationError(
...                 "Config '%s' must be a list" % kwargs["key"])
>>> @getter(config_should_not_be_appended, sender=Project)
>>> def unique_config_validator(**kwargs):
>>>     conf = config.get(Project, instance=kwargs["instance"])
>>>     if conf.get_config(kwargs["key"]) is not None:
>>>         return ValidationError(
...             "Config keys for '%s' must be unique" % kwargs["sender"])

Storing JSON data

>>> config.get().set_config("jsonlist", [1, 2, 3])
>>> config.get().get_config("jsonlist")
[1, 2, 3]
>>> config.get().set_config("jsontuple", (4, 5, 6))
>>> config.get().get_config("jsontuple")
[4, 5, 6]
>>> config.get().set_config("jsondict", dict(a=1, b=2, c=3))
>>> config.get().get_config("jsondict")
OrderedDict([(u'a', 1), (u'c', 3), (u'b', 2)])
>>>
>>> from collections import OrderedDict
>>>
>>> config.get().set_config("jsonorderdict", OrderedDict(a=1, b=2, c=3))
>>> config.get().get_config("jsonorderdict")
OrderedDict([(u'a', 1), (u'b', 2), (u'c', 3)])
>>> class Foo(object):
...     pass
>>>
>>> config.get().set_config("badjson", dict(foo=Foo()))
Traceback (most recent call last):
...
TypeError: <Foo object at ...> is not JSON serializable

Catching Config object errors

>>> conf = config.get(Project)
>>> try:
...     conf.get_config("appended_foo")
... except conf.model.MultipleObjectsReturned as e:
...     raise e
Clone this wiki locally