Changeset 1544

Show
Ignore:
Timestamp:
04/30/08 13:35:20 (23 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!
Location:
trunk
Files:
8 added
14 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/media/PyLucid/internal_page/admin_menu/sub_menu.html

    r1539 r1544  
    5555    </li> 
    5656    <li> 
    57         <a href="{{ commandURLprefix }}/preferences/select/">{% trans 'Low level preferneces editor' %}</a> 
     57        <a href="{{ commandURLprefix }}/preferences/select/">{% trans 'Preferences editor' %}</a> 
    5858    </li> 
    5959    <!-- 
  • trunk/pylucid/media/PyLucid/internal_page/preferences/edit_form.html

    r1540 r1544  
    1 <fieldset><legend>{{ plugin_name }} - {{ pref_name }}</legend> 
    2 <p>{{ description }}</p> 
     1<fieldset><legend>{{ plugin_name }}</legend> 
    32<form method="post" action="."> 
    4 {{ form.as_p }} 
     3<ul id="input_fields"> 
     4    {% for field in form %} 
     5        <li title="{{ field.help_text }}" class="{{ field.html_name }}"> 
     6        <label for="{{ field.auto_id }}">{{ field.label }} :</label> 
     7        {{ field }} 
     8        <span class="field_help_text">{{ field.help_text }}</span> 
     9        {% if field.errors %}<li class="field_errors">{{ field.errors }}</li>{% endif %} 
     10    </li> 
     11    {% endfor %} 
     12</ul> 
    513<input type="submit" name="save" value="{% trans 'save' %}" /> 
    6 <input type="submit" name="validate" value="{% trans 'validate' %}" /> 
    714<input onclick="self.location.href='{{ url_abort }}'" name="abort" value="{% trans 'abort' %}" type="reset" /> 
    815</form> 
    9 <p><strong>Note:</strong><br /> 
    10 {% blocktrans %} 
    11 Validate checks only, if the input is a valid Python expression string.<br /> 
    12 It not examined if the data are logically correct. 
    13 {% endblocktrans %} 
    14 </p> 
    1516</fieldset> 
  • trunk/pylucid/media/PyLucid/internal_page/preferences/select.html

    r1540 r1544  
    1 {% for plugin, data in preferences.items %} 
    2 <fieldset><legend>{{ plugin }}</legend> 
     1<fieldset><legend>select for edit</legend> 
    32<ul> 
    4   {% for pref in data %} 
    5   <li><a href="{{ pref.link }}">{{ pref.name }}</a><br /> 
    6   {{ pref.description }}</li> 
    7   {% endfor %} 
     3{% for item in preferences %} 
     4  <li><a href="{{ item.edit_link }}">{{ item.plugin_name }}</a> - {{ item.plugin_description }}</li> 
     5{% endfor %} 
    86</ul> 
     7<small> 
     8({% trans 'Low level edit in the' %} <a href="{{ admin_link }}" onclick="OpenInWindow(this); return false">{% trans 'django admin panel' %}</a>.) 
     9</small> 
    910</fieldset> 
    10 {% endfor %} 
  • trunk/pylucid/PyLucid/install/install.py

    r1466 r1544  
    3131    DROP_TABLES = ( 
    3232        "PyLucid_pagearchiv", 
     33        "PyLucid_preference2", 
    3334    ) 
    3435 
  • trunk/pylucid/PyLucid/models.py

    r1538 r1544  
    542542 
    543543    def __unicode__(self): 
    544         return self.plugin_name.replace("_"," ") 
    545  
     544        txt = u"%s - %s" % (self.package_name, self.plugin_name) 
     545        return txt.replace(u"_",u" ") 
     546 
     547#______________________________________________________________________________ 
     548# Preference 
    546549 
    547550class 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 
     586class PreferenceOld(models.Model): 
    548587    """ 
    549588    Stores preferences for the PyLucid system and all Plugins. 
     
    648687    lastupdateby = models.ForeignKey( 
    649688        User, null=True, blank=True, editable=False, 
    650         related_name="preferences_lastupdateby", 
    651689    ) 
    652690 
     
    669707        return "%s '%s'" % (self.plugin, self.name) 
    670708 
     709    class Meta: 
     710        # Use the old table 
     711        db_table = u'PyLucid_preference' 
     712 
     713 
     714#______________________________________________________________________________ 
    671715 
    672716 
  • trunk/pylucid/PyLucid/plugins_internal/find_and_replace/find_and_replace.py

    r1476 r1544  
    4141MAX_TERM_LEN = 150 
    4242 
     43class PreferencesForm(forms.Form): 
     44    min_term_len = forms.IntegerField( 
     45        help_text="Min length of a search term", 
     46        initial=2, min_value=1 
     47    ) 
     48    max_term_len = forms.IntegerField( 
     49        help_text="Max length of a search term", 
     50        initial=150, min_value=1, max_value=500 
     51    ) 
     52 
    4353 
    4454# for FindReplaceForm ChoiceField 
     
    6979 
    7080    def find_and_replace(self): 
     81        self.page_msg("Preferences:", self.preferences) 
     82 
    7183        context = {} 
    7284        if self.request.method == 'POST': 
  • trunk/pylucid/PyLucid/plugins_internal/plugin_admin/plugin_admin.py

    r1532 r1544  
    2525import os 
    2626 
     27from django.utils.translation import ugettext as _ 
    2728from django import newforms as forms 
    28 from django.utils.translation import ugettext as _ 
    2929from django.conf import settings 
    3030 
     31from PyLucid.system.plugin_manager import get_plugin_list, install_plugin 
     32from PyLucid.system.plugin_import import get_plugin_config 
     33from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3134from PyLucid.models import Plugin 
    32 from PyLucid.system.plugin_manager import get_plugin_list, get_plugin_config, \ 
    33                                                                 install_plugin 
    34 from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3535 
    3636 
  • trunk/pylucid/PyLucid/plugins_internal/preferences/preferences.py

    • Property svn:eol-style set to LF
    • Property svn:keywords set to Rev Author
    r1540 r1544  
    1212    $LastChangedDate$ 
    1313    $Rev$ 
    14     $Author: JensDiemer $ 
     14    $Author$ 
    1515 
    1616    :copyleft: 2008 by the PyLucid team. 
     
    2626from django.utils.translation import ugettext as _ 
    2727 
    28 from PyLucid.models import Preference 
     28from PyLucid.db.preferences import get_all_prefs, Preferences 
    2929from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3030from PyLucid.tools.data_eval import data_eval, DataEvalError 
    31  
    32 INTERNAL_NAME = "[system]" 
    33  
    34  
    35 class DataEvalField(forms.CharField): 
    36     def clean(self, raw_value): 
    37         raw_value = super(DataEvalField, self).clean(raw_value) 
    38         try: 
    39             value = data_eval(raw_value) 
    40         except DataEvalError, e: 
    41             raise ValidationError(_(u"data eval error: %s") % e) 
    42         else: 
    43             return value 
    44  
    45 class PformatWidget(forms.Textarea): 
    46     def __init__(self, attrs=None): 
    47         self.attrs={'rows': '10'} 
    48  
    49     def render(self, name, value, attrs=None): 
    50         if not isinstance(value, basestring): 
    51             value = pformat(value) 
    52         return super(PformatWidget, self).render(name, value, attrs=None) 
    53  
    54 class EditForm(forms.Form): 
    55     """ 
    56     Form for editing a preferences 
    57     """ 
    58     raw_value = DataEvalField(widget=PformatWidget()) 
    5931 
    6032 
     
    6335 
    6436    def _vebose_plugin_name(self, pref): 
    65         if pref.plugin == None: 
    66             return INTERNAL_NAME 
    67         else: 
    68             return pref.plugin.plugin_name.replace("_", " ") 
     37        return pref.plugin.plugin_name.replace("_", " ") 
    6938 
    7039    def select(self): 
     
    7241        Display the sub menu 
    7342        """ 
    74         self.context["PAGE"].title = _("Low level preferences editor") 
     43        self.context["PAGE"].title = _("preferences editor") 
    7544 
    76         preferences = {} 
    77         for pref in Preference.objects.all(): 
    78             plugin_name = self._vebose_plugin_name(pref) 
     45        items = [] 
     46        for pref in get_all_prefs(): 
     47            edit_link = self.URLs.methodLink("edit", args=pref.id) 
    7948 
    80             pref.link = self.URLs.methodLink("edit", args=(pref.id, pref.name)) 
    81  
    82             if plugin_name not in preferences: 
    83                 preferences[plugin_name] = [pref] 
    84             else: 
    85                 preferences[plugin_name].append(pref) 
     49            items.append({ 
     50                "plugin_name": self._vebose_plugin_name(pref), 
     51                "plugin_description": pref.plugin.description, 
     52                "edit_link": edit_link, 
     53            }) 
    8654 
    8755        context = { 
    88             "preferences": preferences, 
     56            "preferences": items, 
     57            "admin_link": self.URLs.adminLink("PyLucid/preference"), 
    8958        } 
    9059        self._render_template("select", context)#, debug=True) 
    9160 
    92     def edit(self, url_args=None): 
     61    def edit(self, url_args): 
    9362        try: 
    94             url_args = url_args.strip("/").split("/",1)[0] 
     63            url_args = url_args.strip("/") 
    9564            pref_id = int(url_args) 
    9665        except Exception, e: 
     
    9867            return 
    9968 
    100         p = Preference.objects.get(id = pref_id) 
     69        p = Preferences() 
     70        p.init_via_id(pref_id) 
     71        data_dict = p.data_dict 
     72 
     73        p.load_form(self.request) 
     74        unbound_form = p.form 
    10175 
    10276        if self.request.method == 'POST': 
    103             form = EditForm(self.request.POST) 
     77            form = unbound_form(self.request.POST) 
    10478            if form.is_valid(): 
    105                 value = form.cleaned_data["raw_value"] 
    106                 if "validate" in self.request.POST: 
    107                     # rebuild the form for pformat with the eval data 
    108                     form = EditForm({"raw_value": value}) 
    109                     self.page_msg("validate only...") 
    110                 else: 
    111                     # save the form 
    112                     p.value = value 
    113                     p.save() 
    114                     self.page_msg.green("new value saved!") 
    115                     return self.select() # Display the menu 
     79                new_data_dict = form.cleaned_data 
     80                p.update_and_save(new_data_dict) 
     81                self.page_msg("New preferences saved.") 
     82                return self.select() # Display the menu 
    11683        else: 
    117             form = EditForm({"raw_value": p.value}) 
     84            form = unbound_form(data_dict) 
    11885 
    11986        context = { 
    12087            "plugin_name": self._vebose_plugin_name(p), 
    121             "pref_name": p.name, 
    122             "description": p.description, 
    12388            "form": form, 
    12489            "url_abort": self.URLs.methodLink("select"), 
    12590        } 
    12691        self._render_template("edit_form", context)#, debug=True) 
    127 #        self._render_string_template(EDIT_TEMPLATE, context)#, debug=True) 
    12892 
    12993 
     
    13397 
    13498 
    135  
    136  
    137  
    138  
    139  
    140  
    141  
    142  
  • trunk/pylucid/PyLucid/plugins_internal/preferences/preferences_cfg.py

    • Property svn:eol-style set to LF
  • trunk/pylucid/PyLucid/plugins_internal/preferences/__init__.py

    • Property svn:eol-style set to LF
  • trunk/pylucid/PyLucid/plugins_internal/search/search.py

    r1418 r1544  
    4949 
    5050 
     51class PreferencesForm(forms.Form): 
     52    min_term_len = forms.IntegerField( 
     53        help_text="Min length of a search term", 
     54        initial=3, min_value=1 
     55    ) 
     56    max_term_len = forms.IntegerField( 
     57        help_text="Max length of a search term", 
     58        initial=50, min_value=1, max_value=200 
     59    ) 
     60    max_results = forms.IntegerField( 
     61        help_text="Number of the paged for the result page", 
     62        initial=20, min_value=1, max_value=200 
     63    ) 
     64    text_cutout_len = forms.IntegerField( 
     65        help_text="The length of the text-hit-cutouts", 
     66        initial=50, min_value=1, max_value=200 
     67    ) 
     68    text_cutout_lines = forms.IntegerField( 
     69        help_text="Max. cutout lines for every search term", 
     70        initial=5, min_value=1, max_value=20 
     71    ) 
     72 
    5173 
    5274class SearchForm(forms.Form): 
     
    6385        Insert a empty search form into the page. 
    6486        """ 
     87        self.page_msg("Preferences:", self.preferences) 
    6588        search_form = SearchForm() 
    6689        context = { 
  • trunk/pylucid/PyLucid/system/BasePlugin.py

    r1536 r1544  
    4040from PyLucid.tools.utils import escape 
    4141from PyLucid.system.internal_page import InternalPage, InternalPageNotFound 
    42 from PyLucid.system.plugin_manager import get_plugin_config, debug_plugin_config 
     42from PyLucid.system.plugin_import import get_plugin_config, debug_plugin_config 
    4343from PyLucid.models import Plugin 
    4444 
     
    5959 
    6060        self.current_page = self.context["PAGE"] 
     61        self.preferences = self.context.preferences 
    6162 
    6263    def build_menu(self): 
  • trunk/pylucid/PyLucid/system/page_msg.py

    r1313 r1544  
    5454 
    5555        request = context["request"] 
    56         self.debug_mode = request.debug 
     56        self.debug_mode = getattr(request, "debug", False) 
    5757 
    5858        self._charset = settings.DEFAULT_CHARSET 
  • 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