Django

Code

Changeset 4328

Show
Ignore:
Timestamp:
01/15/07 18:09:53 (2 years ago)
Author:
adrian
Message:

newforms-admin: A model's 'class Admin' is now dynamically/magically converted to be a subclass of django.contrib.admin.options.ModelAdmin?, and the admin site now uses that class rather than the separate AdminOptions? class (for list_display only). This means means 'class Admin' can override any functionality of ModelAdmin?, such as has_add_permission(), etc.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/contrib/admin/options.py

    r4326 r4328  
    3333class ModelAdmin(object): 
    3434    "Encapsulates all admin options and functionality for a given model." 
     35 
     36    list_display = ('__str__',) 
     37 
    3538    def __init__(self, model): 
    3639        self.model = model 
     
    267270            raise PermissionDenied 
    268271        try: 
    269             cl = ChangeList(request, self.model
     272            cl = ChangeList(request, self.model, self.list_display
    270273        except IncorrectLookupParameters: 
    271274            # Wacky lookup parameters were given, so redirect to the main 
  • django/branches/newforms-admin/django/contrib/admin/templatetags/admin_list.py

    r4309 r4328  
    7070    lookup_opts = cl.lookup_opts 
    7171 
    72     for i, field_name in enumerate(lookup_opts.admin.list_display): 
     72    for i, field_name in enumerate(cl.list_display): 
    7373        try: 
    7474            f = lookup_opts.get_field(field_name) 
     
    109109    first = True 
    110110    pk = cl.lookup_opts.pk.attname 
    111     for field_name in cl.lookup_opts.admin.list_display: 
     111    for field_name in cl.list_display: 
    112112        row_class = '' 
    113113        try: 
     
    173173            result_repr = ' ' 
    174174        # If list_display_links not defined, add the link tag to the first field 
    175         if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links:  
     175        if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links: 
    176176            table_tag = {True:'th', False:'td'}[first] 
    177177            first = False 
  • django/branches/newforms-admin/django/contrib/admin/views/main.py

    r4326 r4328  
    8282    if not model._meta.admin: 
    8383        raise Http404("This object has no admin interface.") 
    84     mav = ModelAdmin(model) 
     84    mav = model._meta.ModelAdmin(model) 
    8585    return mav(request, rest_of_url) 
    8686model_admin_view = staff_member_required(never_cache(model_admin_view)) 
     
    293293 
    294294class ChangeList(object): 
    295     def __init__(self, request, model): 
     295    def __init__(self, request, model, list_display): 
    296296        self.model = model 
    297297        self.opts = model._meta 
    298298        self.lookup_opts = self.opts 
    299299        self.manager = self.opts.admin.manager 
     300        self.list_display = list_display 
    300301 
    301302        # Get search parameters from the query string. 
     
    405406            try: 
    406407                try: 
    407                     f = lookup_opts.get_field(lookup_opts.admin.list_display[int(params[ORDER_VAR])]) 
     408                    f = lookup_opts.get_field(self.list_display[int(params[ORDER_VAR])]) 
    408409                except models.FieldDoesNotExist: 
    409410                    pass 
     
    432433            qs = qs.select_related() 
    433434        else: 
    434             for field_name in self.lookup_opts.admin.list_display: 
     435            for field_name in self.list_display: 
    435436                try: 
    436437                    f = self.lookup_opts.get_field(field_name) 
  • django/branches/newforms-admin/django/db/models/base.py

    r4265 r4328  
    131131        if name == 'Admin': 
    132132            assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value)) 
     133            from django.contrib.admin.options import ModelAdmin 
     134            # Dynamically create a new ModelAdmin class, which is a subclass 
     135            # of both ModelAdmin and the 'class Admin' on this model. The 
     136            # resulting class is same as if the 'class Admin' were a subclass 
     137            # of ModelAdmin. 
     138            cls._meta.ModelAdmin = type('ModelAdmin', (value, ModelAdmin), {}) 
     139            # This AdminOptions stuff is legacy and will eventually be removed. 
    133140            value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')])) 
    134         if hasattr(value, 'contribute_to_class'): 
     141            value.contribute_to_class(cls, name) 
     142        elif hasattr(value, 'contribute_to_class'): 
    135143            value.contribute_to_class(cls, name) 
    136144        else: 
  • django/branches/newforms-admin/django/db/models/options.py

    r4265 r4328  
    8888    def __repr__(self): 
    8989        return '<Options for %s>' % self.object_name 
    90          
     90 
    9191    def __str__(self): 
    9292        return "%s.%s" % (self.app_label, self.module_name) 
    93          
     93 
    9494    def get_field(self, name, many_to_many=True): 
    9595        "Returns the requested field by name. Raises FieldDoesNotExist on error."