Show
Ignore:
Timestamp:
05/01/08 12:24:21 (7 months ago)
Author:
JensDiemer
Message:

put the plugin preferences into the plugin model.
TODO: update detect_page and set system_settings at install.

Files:
1 modified

Legend:

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

    r1544 r1548  
    1919 
    2020import os, posixpath, pickle 
     21from pprint import pformat 
    2122 
    2223from django.conf import settings 
     
    2627from django.utils.translation import ugettext as _ 
    2728 
     29from PyLucid.tools.newforms_utils import get_init_dict, setup_help_text 
     30from PyLucid.tools.data_eval import data_eval, DataEvalError 
    2831from PyLucid.tools.shortcuts import getUniqueShortcut 
    2932from PyLucid.tools import crypt 
    3033from PyLucid.system.utils import get_uri_base 
     34from PyLucid.system.plugin_import import get_plugin_module 
     35 
    3136#from PyLucid.db.cache import delete_page_cache 
    3237 
     
    175180        """ 
    176181        try: 
    177             entry = Preference.objects.get(name="index page") 
    178         except Preference.DoesNotExist: 
     182            preferences = Plugin.objects.get_preferences("system_settings") 
     183        except Plugin.DoesNotExist, e: 
    179184            # Update old PyLucid installation? 
    180185            return 
    181186 
    182         index_page_id = entry.value 
     187        index_page_id = preferences["index_page"] 
    183188 
    184189        if int(self.id) != int(index_page_id): 
     
    224229        """ 
    225230        try: 
    226             auto_shortcuts = Preference.objects.get(name='auto shortcuts').value 
    227         except Preference.DoesNotExist: 
     231            preferences = Plugin.objects.get_preferences("system_settings") 
     232        except Plugin.DoesNotExist, e: 
    228233            # Update old PyLucid installation? 
    229234            auto_shortcuts = True 
    230  
    231         if auto_shortcuts in (1, True, "1"): 
     235        else: 
     236            print ">>>", preferences 
     237            auto_shortcuts = preferences["auto_shortcuts"] 
     238 
     239        if auto_shortcuts: 
    232240            # We should rebuild the shortcut 
    233241            self.shortcut = self.name 
     
    485493 
    486494 
    487 #class Markup(models.Model): 
    488 #    # Obsolete! 
    489 #    pass 
    490  
    491 #class PagesInternal(models.Model): 
    492 #    # Obsolete! 
    493 #    pass 
     495class Preference(models.Model): 
     496    # Obsolete! 
     497    pass 
    494498 
    495499#____________________________________________________________________ 
    496500 
     501preference_cache = {} 
     502 
     503class PluginManager(models.Manager): 
     504    def get_preferences(self, plugin_name): 
     505        """ 
     506        returns the preference data dict, use the cache 
     507        """ 
     508        # Get the name of the plugin, if __file__ used 
     509        plugin_name = os.path.splitext(os.path.basename(plugin_name))[0] 
     510        print "plugin name: '%s'" % plugin_name 
     511 
     512        if plugin_name in preference_cache: 
     513            return preference_cache[plugin_name] 
     514 
     515        plugin = self.get(plugin_name = plugin_name) 
     516        return plugin.get_preferences() 
     517 
     518 
    497519class Plugin(models.Model): 
     520    objects = PluginManager() 
     521 
     522    plugin_name = models.CharField(max_length=90, unique=True) 
     523 
    498524    package_name = models.CharField(max_length=255) 
    499     plugin_name = models.CharField(max_length=90, unique=True) 
    500     version = models.CharField(null=True, blank=True, max_length=45) 
    501525    author = models.CharField(blank=True, max_length=150) 
    502526    url = models.CharField(blank=True, max_length=255) 
    503527    description = models.CharField(blank=True, max_length=255) 
    504     long_description = models.TextField(blank=True) 
     528 
     529    pref_data_string = models.TextField( 
     530        null=True, blank=True, 
     531        help_text="printable representation of the newform data dictionary" 
     532    ) 
    505533    can_deinstall = models.BooleanField(default=True, 
    506534        help_text=( 
     
    512540        help_text="Is this plugin is enabled and useable?" 
    513541    ) 
     542 
     543    def init_pref_form(self, pref_form): 
     544        """ 
     545        Set self.pref_data_string from the given newforms form and his initial 
     546        values. 
     547        """ 
     548        init_dict = get_init_dict(pref_form) 
     549        preference_cache[self.plugin_name] = init_dict 
     550        self.set_pref_data_string(init_dict) 
     551 
     552    def set_pref_data_string(self, data_dict): 
     553        """ 
     554        set the dict via pformat 
     555        """ 
     556        preference_cache[self.plugin_name] = data_dict 
     557        self.pref_data_string = pformat(data_dict) 
     558 
     559    def get_preferences(self): 
     560        """ 
     561        evaluate the pformat string into a dict and return it. 
     562        """ 
     563        data_dict = data_eval(self.pref_data_string) 
     564        preference_cache[self.plugin_name] = data_dict 
     565        return data_dict 
     566 
     567    def get_pref_form(self, debug): 
     568        """ 
     569        Get the 'PreferencesForm' newform class from the plugin modul, insert 
     570        initial information into the help text and return the form. 
     571        """ 
     572        plugin_module = get_plugin_module( 
     573            self.package_name, self.plugin_name, debug 
     574        ) 
     575        form = getattr(plugin_module, "PreferencesForm") 
     576        setup_help_text(form) 
     577        return form 
    514578 
    515579    def save(self): 
     
    535599    class Admin: 
    536600        list_display = ( 
    537             "active", "plugin_name", "description", "version", "can_deinstall" 
     601            "active", "plugin_name", "description", "can_deinstall" 
    538602        ) 
    539603        list_display_links = ("plugin_name",) 
    540604        ordering = ('package_name', 'plugin_name') 
    541         list_filter = ("author",) 
     605        list_filter = ("author","package_name", "can_deinstall") 
    542606 
    543607    def __unicode__(self): 
    544608        txt = u"%s - %s" % (self.package_name, self.plugin_name) 
    545609        return txt.replace(u"_",u" ") 
    546  
    547 #______________________________________________________________________________ 
    548 # Preference 
    549  
    550 class Preference(models.Model): 
    551     """ 
    552     Stores preferences 
    553  
    554     Any pickleable Python object can be stored. 
    555  
    556     Use a small cache, so the pickle.loads() method would only be used on the 
    557     first access. 
    558  
    559     Note: 
    560         - This model has no Admin class. Because it makes no sense to edit 
    561             pickled data strings in the django admin panel ;) 
    562     """ 
    563     plugin = models.ForeignKey("Plugin", help_text="The associated plugin") 
    564     repr_string = models.TextField( 
    565         help_text="printable representation of the newform data dictionary" 
    566     ) 
    567  
    568     lastupdatetime = models.DateTimeField(auto_now=True, editable=False) 
    569     lastupdateby = models.ForeignKey( 
    570         User, null=True, blank=True, editable=False, 
    571     ) 
    572  
    573     #__________________________________________________________________________ 
    574  
    575     def __unicode__(self): 
    576         return u"Preference object for '%s'" % self.plugin 
    577  
    578     class Admin: 
    579         pass 
    580  
    581     class Meta: 
    582         # Use a new table 
    583         db_table = u'PyLucid_preference2' 
    584  
    585  
    586 class PreferenceOld(models.Model): 
    587     """ 
    588     Stores preferences for the PyLucid system and all Plugins. 
    589  
    590     Any pickleable Python object can be stored. 
    591     Use a small cache, so the pickle.loads() method would only be used on the 
    592     first get_value access. The default_value used no cache. 
    593  
    594     Usage: 
    595     --------------------------------------------------------------------------- 
    596     Preference(name = "value name", value = my_python_data_object).save() 
    597  
    598     p = Preference.objects.get(name = "value name") 
    599     print p.value 
    600     --------------------------------------------------------------------------- 
    601  
    602     Note: 
    603         - This model has no Admin class. Because it makes no sense to edit 
    604             pickled data strings in the django admin panel ;) 
    605         - value and default_value are properties 
    606     """ 
    607     __cache = {} # Cache the pickleable Python object 
    608  
    609     #__________________________________________________________________________ 
    610  
    611     def _get_cache_key(self): 
    612         """ returns plugin name + preferences name as a string """ 
    613         return "%s-%s" % (self.plugin, self.name) 
    614  
    615     #__________________________________________________________________________ 
    616     # get and set methods for the value property of the entry 
    617  
    618     def __get_value(self): 
    619         """ 
    620         returns the pickleable Python object back. Use the cache first. 
    621         """ 
    622         cache_key = self._get_cache_key() 
    623  
    624         if cache_key in self.__cache: 
    625             value = self.__cache[cache_key] 
    626         else: 
    627             self._value = str(self._value) 
    628             value = pickle.loads(self._value) 
    629             self.__cache[cache_key] = value 
    630         return value 
    631  
    632     def __set_value(self, value): 
    633         """ 
    634         Save a the pickleable Python object into the database. Remember the 
    635         source object in the cache 
    636         """ 
    637         cache_key = self._get_cache_key() 
    638  
    639         self.__cache[cache_key] = value 
    640         self._value = pickle.dumps(value) 
    641  
    642     #__________________________________________________________________________ 
    643     # get and set methods for the default value property of the entry 
    644  
    645     def __get_default_value(self): 
    646         """ 
    647         returns the default value back 
    648         """ 
    649         self._default_value = str(self._default_value) 
    650         default_value = pickle.loads(self._default_value) 
    651         return default_value 
    652  
    653     def __set_default_value(self, default_value): 
    654         """ 
    655         save a default value 
    656         """ 
    657         self._default_value = pickle.dumps(default_value) 
    658  
    659     #__________________________________________________________________________ 
    660     # The attributes 
    661  
    662     plugin = models.ForeignKey( 
    663         "Plugin", help_text="The associated plugin", 
    664         null=True, blank=True, editable=False 
    665     ) 
    666     name = models.CharField( 
    667         max_length=150, db_index=True, editable=False, 
    668         help_text="Name of the preferences entry", 
    669     ) 
    670     description = models.TextField(editable=False) 
    671  
    672     _value = models.TextField( 
    673         editable=False, help_text="Pickled Python object" 
    674     ) 
    675     value = property(__get_value, __set_value) 
    676  
    677     _default_value = models.TextField( 
    678         editable=False, help_text="Pickled Python object" 
    679     ) 
    680     default_value = property(__get_default_value, __set_default_value) 
    681  
    682     field_type = models.CharField(max_length=150, editable=False, 
    683         help_text="The data type for this entry (For building a html form)." 
    684     ) 
    685  
    686     lastupdatetime = models.DateTimeField(auto_now=True) 
    687     lastupdateby = models.ForeignKey( 
    688         User, null=True, blank=True, editable=False, 
    689     ) 
    690  
    691     #__________________________________________________________________________ 
    692  
    693     def save(self): 
    694         """ 
    695         Save a new or update a preference entry 
    696         """ 
    697         if self.id == None: 
    698             # A new preferences should be saved. 
    699             if self._default_value == "": 
    700                 # No default value given in the __init__ -> Use current value 
    701                 self._default_value = self._value 
    702  
    703         super(Preference, self).save() # Call the "real" save() method 
    704  
    705     def __unicode__(self): 
    706 #        <Preference: Preference object> 
    707         return "%s '%s'" % (self.plugin, self.name) 
    708  
    709     class Meta: 
    710         # Use the old table 
    711         db_table = u'PyLucid_preference' 
    712610 
    713611