Show
Ignore:
Timestamp:
05/01/08 12:24:21 (23 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/plugins_internal/search/search.py

    r1544 r1548  
    2727 
    2828from django import newforms as forms 
     29from django.utils.safestring import mark_safe 
    2930from django.utils.translation import ugettext as _ 
    30 from django.utils.safestring import mark_safe 
    31  
    32 from PyLucid.models import Page 
     31 
    3332from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3433from PyLucid.tools.utils import escape 
    35  
    36  
    37 # How min/max long must a search term be? 
    38 MIN_TERM_LEN = 3 
    39 MAX_TERM_LEN = 50 
    40  
    41 # Number of the paged for the result page: 
    42 MAX_RESULTS = 20 
    43  
    44 # The length of the text-hit-cutouts: 
    45 TEXT_CUTOUT_LEN = 50 
    46  
    47 # Max. cutout lines for every search term: 
    48 MAX_CUTOUTS_LINES = 5 
     34from PyLucid.models import Page, Plugin 
    4935 
    5036 
     
    7157    ) 
    7258 
    73  
    74 class SearchForm(forms.Form): 
    75     # TODO: min und max should be saved in the prefereces. 
    76     search_string = forms.CharField( 
    77         min_length = MIN_TERM_LEN, max_length = MAX_TERM_LEN 
    78     ) 
     59# We used preferences values in a newform. We need these values here. 
     60try: 
     61    preferences = Plugin.objects.get_preferences(__file__) 
     62except Plugin.DoesNotExist, e: 
     63    # in _install section? 
     64    pass 
     65else: 
     66    class SearchForm(forms.Form): 
     67        search_string = forms.CharField( 
     68            min_length = preferences["min_term_len"], 
     69            max_length = preferences["max_term_len"], 
     70        ) 
    7971 
    8072 
     
    8577        Insert a empty search form into the page. 
    8678        """ 
    87         self.page_msg("Preferences:", self.preferences) 
    8879        search_form = SearchForm() 
    8980        context = { 
     
    138129        search_strings = [] 
    139130        for term in raw_search_strings: 
    140             if len(term)<MIN_TERM_LEN: 
     131            if len(term)<preferences["min_term_len"]: 
    141132                self.page_msg("Ignore '%s' (too small)" % cgi.escape(term)) 
    142133            else: 
     
    178169 
    179170        # Use only the first pages: 
    180         results = results[:MAX_RESULTS] 
     171        results = results[:preferences["max_results"]] 
    181172 
    182173        # Build a dict, for the template 
     
    191182        the lines. 
    192183        """ 
     184        text_cutout_len = preferences["text_cutout_len"] 
     185        max_cutout_lines = preferences["max_cutout_lines"] 
     186 
    193187        for result in results: 
    194188            result["cutouts"] = [] 
     
    197191            for term in search_strings: 
    198192                start = 0 
    199                 for _ in xrange(MAX_CUTOUTS_LINES): 
     193                for _ in xrange(max_cutout_lines): 
    200194                    try: 
    201195                        index = content.index(term, start) 
     
    206200                    start = index+1 
    207201 
    208                     if index<TEXT_CUTOUT_LEN: 
    209                         txt = content[:TEXT_CUTOUT_LEN] 
     202                    if index<text_cutout_len: 
     203                        txt = content[:text_cutout_len] 
    210204                    else: 
    211                         txt = content[index-TEXT_CUTOUT_LEN:index+TEXT_CUTOUT_LEN] 
     205                        start = index-text_cutout_len 
     206                        end = index+text_cutout_len 
     207                        txt = content[start:end] 
    212208 
    213209                    txt = escape(txt)