Django

Code

Changeset 1286

Show
Ignore:
Timestamp:
11/18/05 16:06:17 (3 years ago)
Author:
rjwittams
Message:

Moved filterspecs to their own module, minor cleanup of bound manipulator

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/new-admin/django/contrib/admin/templates/admin/edit_inline_stacked.html

    r986 r1286  
    66      {% endif %}{% endif %} 
    77      {% for bound_field in fcw.bound_fields %} 
    8          {% if bound_field.not_in_table %} 
     8         {% if bound_field.hidden %} 
    99            {% field_widget bound_field %} 
    1010         {% else %} 
  • django/branches/new-admin/django/contrib/admin/templates/admin/edit_inline_tabular.html

    r1028 r1286  
    2121      <tr class="{% cycle row1,row2 %}"> 
    2222      {% for bound_field in fcw.bound_fields %} 
    23          {% if not bound_field.not_in_table %} 
     23         {% if not bound_field.hidden %} 
    2424         <td {{bound_field.cell_class_attribute}}>  
    2525            {% field_widget bound_field %} 
     
    3636   {% for fcw in bound_related_object.form_field_collection_wrappers %} 
    3737      {% for bound_field in fcw.bound_fields %} 
    38          {% if bound_field.not_in_table %}   
     38         {% if bound_field.hidden %}   
    3939            {% field_widget bound_field %} 
    4040         {% endif %} 
  • django/branches/new-admin/django/contrib/admin/templatetags/admin_modify.py

    r1175 r1286  
    127127    def __init__(self, related_object, field_mapping, original): 
    128128        super(TabularBoundRelatedObject, self).__init__(related_object, field_mapping, original) 
    129         self.field_wrapper_list = self.relation.editable_fields(FieldWrapper) 
     129        self.field_wrapper_list = [FieldWrapper(field) for field in self.relation.editable_fields()] 
    130130         
    131131        fields = self.relation.editable_fields() 
  • django/branches/new-admin/django/contrib/admin/views/main.py

    r1248 r1286  
    11# Generic admin views. 
    22from django.contrib.admin.views.decorators import staff_member_required 
     3from django.contrib.admin.filterspecs import FilterSpec 
    34from django.core import formfields, meta, template 
    45from django.core.template import loader 
     
    78from django.core.extensions import DjangoContext as Context 
    89from django.core.extensions import get_object_or_404, render_to_response 
     10from django.core.paginator import ObjectPaginator, InvalidPage 
     11from django.conf.settings import ADMIN_MEDIA_PREFIX 
    912from django.models.admin import log 
    1013from django.utils.html import strip_tags 
    1114from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect 
    1215from django.utils.text import capfirst, get_text_list 
    13 from django.conf.settings import ADMIN_MEDIA_PREFIX 
    14 from django.core.paginator import ObjectPaginator, InvalidPage 
    1516from django.utils import dateformat 
    1617from django.utils.dates import MONTHS 
    1718from django.utils.html import escape 
    1819import operator 
    19 import datetime 
    2020 
    2121# The system will display a "Show all" link only if the total result count 
     
    5454    pass 
    5555 
    56 class FilterSpec(object): 
    57     filter_specs = [] 
    58     def __init__(self, f, request, params): 
    59         self.field = f 
    60         self.params = params 
    61          
    62     def register(cls, test, factory): 
    63         cls.filter_specs.append( (test, factory) ) 
    64     register = classmethod(register) 
    65      
    66     def create(cls, f, request, params): 
    67         for test, factory in cls.filter_specs: 
    68             if test(f): 
    69                 return factory(f, request, params)     
    70     create = classmethod(create) 
    71      
    72     def has_output(self): 
    73         return True 
    74      
    75     def choices(self, cl): 
    76         raise NotImplementedException() 
    77      
    78     def title(self): 
    79         return self.field.verbose_name 
    80      
    81     def output(self, cl): 
    82         t = [] 
    83         if self.has_output(): 
    84             t.append(_('<h3>By %s:</h3>\n<ul>\n') % self.title()) 
    85              
    86             for choice in self.choices(cl): 
    87                 t.append('<li%s><a href="%s">%s</a></li>\n' % \ 
    88                     ((choice['selected'] and ' class="selected"' or ''), 
    89                      choice['query_string'] ,  
    90                      choice['display']))             
    91             t.append('</ul>\n\n') 
    92         return "".join(t) 
    93      
    94 class RelatedFilterSpec(FilterSpec): 
    95     def __init__(self, f, request, params): 
    96         super(RelatedFilterSpec, self).__init__(f, request, params)     
    97         if isinstance(f, meta.ManyToManyField): 
    98             self.lookup_title = f.rel.to.verbose_name 
    99         else: 
    100             self.lookup_title = f.verbose_name 
    101         self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to.pk.name) 
    102         self.lookup_val = request.GET.get(self.lookup_kwarg, None) 
    103         self.lookup_choices = f.rel.to.get_model_module().get_list() 
    104      
    105     def has_output(self): 
    106         return len(self.lookup_choices) > 1 
    107          
    108     def title(self): 
    109         return self.lookup_title      
    110          
    111     def choices(self, cl): 
    112         yield {'selected': self.lookup_val is None, 
    113                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),  
    114                'display': _('All') } 
    115         for val in self.lookup_choices: 
    116             pk_val = getattr(val, self.field.rel.to.pk.attname) 
    117             yield { 'selected': self.lookup_val == str(pk_val),  
    118                     'query_string': cl.get_query_string( {self.lookup_kwarg: pk_val}), 
    119                     'display' : val } 
    120 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) 
    121  
    122 class ChoicesFilterSpec(FilterSpec): 
    123     def __init__(self, f, request, params): 
    124         super(ChoicesFilterSpec, self).__init__(f, request, params) 
    125         self.lookup_kwarg = '%s__exact' % f.name 
    126         self.lookup_val = request.GET.get(self.lookup_kwarg, None) 
    127          
    128     def choices(self, cl): 
    129         yield {'selected' : self.lookup_val is None,  
    130                'query_string': cl.get_query_string( {}, [self.lookup_kwarg]),  
    131                'display': _('All') } 
    132         for k, v in self.field.choices: 
    133             yield {'selected': str(k) == self.lookup_val,  
    134                     'query_string' : cl.get_query_string( {self.lookup_kwarg: k}), 
    135                     'display' : v } 
    136 FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) 
    137  
    138 class DateFieldFilterSpec(FilterSpec): 
    139      
    140     def __init__(self, f, request, params): 
    141         super(DateFieldFilterSpec, self).__init__(f, request, params) 
    142          
    143         self.field_generic = '%s__' % self.field.name 
    144          
    145         self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)]) 
    146          
    147         today = datetime.date.today() 
    148         one_week_ago = today - datetime.timedelta(days=7) 
    149         today_str = isinstance(self.field, meta.DateTimeField) and today.strftime('%Y-%m-%d 23:59:59') or today.strftime('%Y-%m-%d') 
    150          
    151         self.links = ( 
    152             (_('Any date'), {}), 
    153             (_('Today'), {'%s__year' % self.field.name: str(today.year),  
    154                        '%s__month' % self.field.name: str(today.month),  
    155                        '%s__day' % self.field.name: str(today.day)}), 
    156             (_('Past 7 days'), {'%s__gte' % self.field.name: one_week_ago.strftime('%Y-%m-%d'),  
    157                              '%s__lte' % f.name: today_str}), 
    158             (_('This month'), {'%s__year' % self.field.name: str(today.year),  
    159                              '%s__month' % f.name: str(today.month)}), 
    160             (_('This year'), {'%s__year' % self.field.name: str(today.year)}) 
    161         )  
    162      
    163     def title(self): 
    164         return self.field.verbose_name 
    165      
    166     def choices(self, cl): 
    167         for title, param_dict in self.links: 
    168             yield { 'selected' : self.date_params == param_dict,  
    169                     'query_string' : cl.get_query_string( param_dict, self.field_generic), 
    170                     'display' : title } 
    171                      
    172 FilterSpec.register(lambda f: isinstance(f, meta.DateField), DateFieldFilterSpec) 
    173  
    174 class BooleanFieldFilterSpec(FilterSpec): 
    175     def __init__(self, f, request, params): 
    176         super(BooleanFieldFilterSpec, self).__init__(f, request, params) 
    177         self.lookup_kwarg = '%s__exact' % f.name 
    178         self.lookup_kwarg2 = '%s__isnull' % f.name 
    179         self.lookup_val = request.GET.get(self.lookup_kwarg, None) 
    180         self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None) 
    181          
    182     def title(self): 
    183         return self.field.verbose_name 
    184      
    185     def choices(self, cl): 
    186         for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')): 
    187             yield { 'selected' : self.lookup_val == v and not self.lookup_val2,  
    188                     'query_string' : cl.get_query_string( {self.lookup_kwarg: v}, [self.lookup_kwarg2]),  
    189                     'display': k  
    190                   } 
    191         if isinstance(self.field, meta.NullBooleanField): 
    192             yield { 'selected' : self.lookup_val2 == 'True',  
    193                     'query_string' : cl.get_query_string( {self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),  
    194                     'display': _('Unknown')                                       
    195                   } 
    196 FilterSpec.register(lambda f: isinstance(f, meta.BooleanField) or  
    197                                    isinstance(f, meta.NullBooleanField), BooleanFieldFilterSpec) 
    19856 
    19957class ChangeList(object): 
     
    251109        else: 
    252110            self.lookup_mod, self.lookup_opts = self.mod, self.opts 
    253                  
    254111 
    255112    def get_search_parameters(self, request): 
     
    433290    return js 
    434291 
     292 
    435293class AdminBoundField(BoundField): 
    436294    def __init__(self, field, field_mapping, original): 
     
    443301        self.is_file_field = isinstance(field, meta.FileField) 
    444302        self.needs_add_label = field.rel and isinstance(field.rel, meta.ManyToOne) or isinstance(field.rel, meta.ManyToMany) and field.rel.to.admin 
    445         self.not_in_table = isinstance(self.field, meta.AutoField) 
     303        self.hidden = isinstance(self.field, meta.AutoField) 
    446304        self.first = False 
    447305         
     
    494352        super(AdminBoundFieldSet, self).__init__(field_set, field_mapping, original, AdminBoundFieldLine) 
    495353         
    496  
    497 class AdminBoundManipulator(object): 
     354class BoundManipulator(object): 
    498355    def __init__(self, opts, manipulator, field_mapping): 
    499         self.inline_related_objects = opts.get_followed_related_objects() 
    500          
    501         field_sets = opts.admin.get_field_sets(opts) 
     356        self.inline_related_objects = opts.get_followed_related_objects(manipulator.follow) 
    502357        self.original = hasattr(manipulator, 'original_object') and manipulator.original_object or None 
    503358        self.bound_field_sets = [field_set.bind(field_mapping, self.original, AdminBoundFieldSet)  
    504                                  for field_set in field_sets] 
    505                                          
     359                                 for field_set in opts.admin.get_field_sets(opts)] 
     360        self.ordered_objects = opts.get_ordered_objects()[:] 
     361 
     362class AdminBoundManipulator(BoundManipulator): 
     363    def __init__(self, opts, manipulator, field_mapping): 
     364        super(AdminBoundManipulator, self).__init__(opts, manipulator, field_mapping) 
     365        field_sets = opts.admin.get_field_sets(opts) 
     366         
    506367        
    507         self.ordered_objects = opts.get_ordered_objects()[:] 
     368         
    508369        self.auto_populated_fields = [f for f in opts.fields if f.prepopulate_from] 
    509370        self.javascript_imports = get_javascript_imports(opts, self.auto_populated_fields, self.ordered_objects, field_sets);                          
     
    514375                                'enctype="multipart/form-data" ' or '' 
    515376         
    516          
    517         
    518377        self.first_form_field_id = self.bound_field_sets[0].bound_field_lines[0].bound_fields[0].form_fields[0].get_id();                 
    519378        self.ordered_object_pk_names = [o.pk.name for o in self.ordered_objects] 
  • django/branches/new-admin/django/core/meta/__init__.py

    r1282 r1286  
    214214 
    215215     
    216     def editable_fields(self, wrapping_func = lambda x: x): 
    217         """Get the fields in this class that should be edited inline. 
    218         Pass a callable, eg a class, as the second argument to wrap the fields. 
    219         This can be useful to add extra attributes for use in templates.""" 
     216    def editable_fields(self): 
     217        """Get the fields in this class that should be edited inline.""" 
    220218         
    221         return [wrapping_func(f) for f in self.opts.fields + self.opts.many_to_many if f.editable and f != self.field ] 
     219        return [f for f in self.opts.fields + self.opts.many_to_many if f.editable and f != self.field ] 
    222220       
    223221    def get_follow(self, override=None):