PyLucid CMS Logo

Plugin preferences

Every Plugin can have his own Preferences. The admin can easy setup the preferences for all plugins with the preferences editor .

Preferences are defined as a django newforms class with initial values .

The newforms class must be inserted in the plugin config file and must be named 'PreferencesForm'.

↑ usage  #

There are two way to access the preferences:

↑ Access in plugin class  #

This is the easiest way:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from PyLucid.system.BasePlugin import PyLucidBasePlugin
from PyLucid.models import Page, Plugin

class MyPlugin(PyLucidBasePlugin):
    def lucidTag(self):
        # Get the preferences from the database:
        preferences = self.get_preferences()
        if preferences == None:
            # preferences not in database -> reinit required
        ...

↑ Access in plugin class example:  #

↑ Access on module level  #

You should use this access way only if it really needed. You need this e.g. if you use preferences value in a newform class:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django import newforms as forms
from PyLucid.models import Plugin


# We used preferences values in a newform. We need these values here.
preferences = Plugin.objects.get_preferences(__file__)


class MyPluginForm(forms.Form):
    FooBar = forms.CharField(
        min_length = preferences["min_term_len"],
        max_length = preferences["max_term_len"],
    )

↑ Access on module level example:  #

↑ low level access  #

The preferences saved into the plugin model. A low level access looks like this:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from PyLucid.models import Plugin


# Get the system_settings plugin
plugin = Plugin.objects.get(plugin_name = "system_settings")



# Get the preferences as a data dict
preferences = plugin.get_preferences()



# Change one value
preferences["auto_shortcuts"] = False



# Check the new preferences via newforms from the plugin config file
unbound_form = plugin.get_pref_form(debug=True)
form = unbound_form(preferences)
assert form.is_valid(), "Error: %s" % repr(form.errors)


# Save the changes data dict
plugin.set_pref_data_string(preferences)
plugin.save()
0 Kommentare für 'preferences':
    Es existiert kein Kommentar für 'preferences'
Kommentar hinterlassen
tag navi plugin | preferences | pylucid

django-processinfo: 5.4 ms of 305.0 ms (1.8%)