Changeset 1601

Show
Ignore:
Timestamp:
05/28/08 13:35:42 (22 months ago)
Author:
JensDiemer
Message:

plugin preferences: Verify before install

Location:
trunk/pylucid/PyLucid
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/models/Plugin.py

    r1585 r1601  
    2727from PyLucid.tools.newforms_utils import get_init_dict, setup_help_text 
    2828from PyLucid.tools.data_eval import data_eval, DataEvalError 
     29from PyLucid.system.exceptions import PluginPreferencesError 
    2930 
    3031preference_cache = {} 
     
    8182    # Special methods 
    8283 
    83     def init_pref_form(self, pref_form): 
     84    def init_pref_form(self, pref_form, debug=False): 
    8485        """ 
    8586        Set self.pref_data_string from the given newforms form and his initial 
    8687        values. 
     88        Before we save the data dict into the database, we validate it with 
     89        preferences newform class. This is needed for two cased: 
     90         - A plugin developer has inserted a wrong initial value 
     91         - The initial value must be cleaned. e.g. admin_menu_cfg.WeightField 
    8792        """ 
    8893        init_dict = get_init_dict(pref_form) 
    89         preference_cache[self.plugin_name] = init_dict 
    90         self.set_pref_data_string(init_dict) 
     94 
     95        # Validate the init_dict 
     96        unbound_form = self.get_pref_form(debug) 
     97        form = unbound_form(init_dict) 
     98        if form.is_valid(): 
     99            cleaned_data_dict = form.cleaned_data 
     100        else: 
     101            msg = ( 
     102                "Can't save preferences into the database!" 
     103                " Newforms validate error: %r" 
     104            ) % form.errors 
     105            raise PluginPreferencesError(msg) 
     106 
     107        preference_cache[self.plugin_name] = cleaned_data_dict 
     108        self.set_pref_data_string(cleaned_data_dict) 
    91109 
    92110    #__________________________________________________________________________ 
  • trunk/pylucid/PyLucid/system/exceptions.py

    r1545 r1601  
    3939    """ 
    4040    pass 
     41 
     42class PluginPreferencesError(Exception): 
     43    pass