Show
Ignore:
Timestamp:
04/30/08 13:35:20 (7 months ago)
Author:
JensDiemer
Message:

Experimental new Preferences!

  • New editor used the newforms models in the plugin class
  • Doesn't realy use the preferences (in search and find_and_replace) only display it
  • the system_settings doesn't used, too. Only for display the data.
  • Not ready in all cases
  • there can exist some print debug!
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/system/plugin_manager.py

    r1540 r1544  
    3434from django.http import HttpResponse, Http404 
    3535 
     36from PyLucid.db.preferences import Preferences, preference_cache, \ 
     37                                                        PreferenceDoesntExist 
     38from PyLucid.system.plugin_import import get_plugin_module, get_plugin_config 
    3639from PyLucid.system.exceptions import * 
    37 from PyLucid.models import Plugin, Preference 
    38  
    39  
    40 def _import(request, from_name, object_name): 
    41     """ 
    42     from 'from_name' import 'object_name' 
    43     """ 
    44     try: 
    45         return __import__(from_name, {}, {}, [object_name]) 
    46     except (ImportError, SyntaxError), e: 
    47         if request.user.is_superuser or request.debug: 
    48             raise 
    49         raise ImportError, "Can't import %s from %s: %s" % ( 
    50             object_name, from_name, e 
    51         ) 
    52  
    53 def get_plugin_class(request, package_name, plugin_name): 
    54     """ 
    55     import the plugin/plugin and returns the class object 
    56     """ 
    57     plugin = _import(request, 
    58         from_name = ".".join([package_name, plugin_name, plugin_name]), 
    59         object_name = plugin_name 
    60     ) 
    61     plugin_class = getattr(plugin, plugin_name) 
    62     return plugin_class 
    63  
    64 def debug_plugin_config(page_msg, plugin_config): 
    65     for item in dir(plugin_config): 
    66         if item.startswith("_"): 
    67             continue 
    68         page_msg("'%s':" % item) 
    69         page_msg(getattr(plugin_config, item)) 
    70  
    71 def get_plugin_config(request, package_name, plugin_name, 
    72                             dissolve_version_string=False, extra_verbose=False): 
    73     """ 
    74     imports the plugin and the config plugin and returns a merge config-object 
    75  
    76     dissolve_version_string == True -> get the version string (__version__) 
    77         from the plugin and put it into the config object 
    78     """ 
    79     config_name = "%s_cfg" % plugin_name 
    80  
    81     def get_plugin(object_name): 
    82         from_name = ".".join([package_name, plugin_name, object_name]) 
    83         if extra_verbose: 
    84             print "from %s import %s" % (from_name, object_name) 
    85         return _import(request, from_name, object_name) 
    86  
    87     config_plugin = get_plugin(config_name) 
    88  
    89     if dissolve_version_string: 
    90         plugin_plugin = get_plugin(plugin_name) 
    91  
    92         plugin_version = getattr(plugin_plugin, "__version__", None) 
    93         if plugin_version: 
    94             # Cleanup a SVN Revision Number 
    95             plugin_version = plugin_version.strip("$ ") 
    96         config_plugin.__version__ = plugin_version 
    97  
    98     return config_plugin 
     40from PyLucid.models import Plugin 
     41 
     42 
     43 
    9944 
    10045def _run(context, local_response, plugin_name, method_name, url_args, 
     
    167112    URLs.current_plugin = plugin_name 
    168113 
    169     plugin_class=get_plugin_class(request, plugin.package_name, plugin_name) 
     114    plugin_module = get_plugin_module(request, plugin.package_name, plugin_name) 
     115 
     116    if plugin_name in preference_cache: 
     117        context.preferences = preference_cache[plugin_name] 
     118    else: 
     119        if hasattr(plugin_module, "PreferencesForm"): 
     120            # Get the preferences dict data from the database 
     121            try: 
     122                p = Preferences() 
     123                p.set_plugin(plugin) 
     124                p.load_from_db() 
     125                context.preferences = p.data_dict 
     126            except PreferenceDoesntExist, e: 
     127                error("Can't get preferences: %s" % e) 
     128                return 
     129        else: 
     130            # plugin has no preferences 
     131            context.preferences = None 
     132 
     133    plugin_class = getattr(plugin_module, plugin_name) 
    170134    class_instance = plugin_class(context, local_response) 
    171135    unbound_method = getattr(class_instance, method_name) 
     
    275239    return plugin 
    276240 
    277 def _insert_preferences(plugin, plugin_config): 
    278     preferences = getattr(plugin_config, "preferences", None) 
    279     if preferences == None: 
    280         # The plugin has no preferences 
     241 
     242def _insert_preferences(request, plugin, package_name, plugin_name): 
     243    """ 
     244    insertet the initial values from the newforms preferences class into the 
     245    database. 
     246    """ 
     247    plugin_module = get_plugin_module(request, package_name, plugin_name) 
     248 
     249    pref_form = getattr(plugin_module, "PreferencesForm", None) 
     250    if pref_form == None: 
     251        # Has no preferences newform class 
    281252        return 
    282253 
    283     for pref in preferences: 
    284         print plugin, type(plugin), pref 
    285         Preference( 
    286             plugin = plugin, 
    287             name = pref["name"], 
    288             description = pref["description"], 
    289             value = pref["value"], 
    290         ).save() 
     254    p = Preferences() 
     255    p.set_plugin(plugin) 
     256    p.create_initial(pref_form) 
     257 
    291258 
    292259def install_plugin(request, package_name, plugin_name, active, 
     
    315282        package_name, plugin_name, plugin_config, active, extra_verbose 
    316283    ) 
    317     _insert_preferences(plugin, plugin_config) 
     284    _insert_preferences(request, plugin, package_name, plugin_name) 
    318285 
    319286