Ticket #10686: permission_inheritance.2.diff

File permission_inheritance.2.diff, 2.9 KB (added by Soby, 13 years ago)

Updated patch that works on rev 16527. Also supports de-duping permissions and combining permissions from multiple inheritance on the model.

  • AUTHORS

     
    540540    Gasper Zejn <zejn@kiberpipa.org>
    541541    Jarek Zgoda <jarek.zgoda@gmail.com>
    542542    Cheng Zhang
     543    Forrest Aldridge <forrest.aldridge@gmail.com>
    543544
    544545A big THANK YOU goes to:
    545546
  • django/db/models/base.py

     
    4444            meta = getattr(new_class, 'Meta', None)
    4545        else:
    4646            meta = attr_meta
     47            # Append any inherited permissions to any explicitly declared ones.
     48            if hasattr(meta, 'permissions') and getattr(meta, 'inherit_permissions', True):
     49                all_perms = set(meta.permissions)
     50                for parent in parents:
     51                    if hasattr(parent, 'Meta') and hasattr(parent.Meta, 'permissions'):
     52                        # De-dupe inherited permission in case the Meta class also inherited from another Meta
     53                        all_perms.update(parent.Meta.permissions)
     54                meta.permissions = list(all_perms)
    4755        base_meta = getattr(new_class, '_meta', None)
    4856
    4957        if getattr(meta, 'app_label', None) is None:
  • django/db/models/options.py

     
    1717DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
    1818                 'unique_together', 'permissions', 'get_latest_by',
    1919                 'order_with_respect_to', 'app_label', 'db_tablespace',
    20                  'abstract', 'managed', 'proxy', 'auto_created')
     20                 'abstract', 'managed', 'proxy', 'auto_created', 'inherit_permissions')
    2121
    2222class Options(object):
    2323    def __init__(self, meta, app_label=None):
     
    8787            if ut and not isinstance(ut[0], (tuple, list)):
    8888                ut = (ut,)
    8989            self.unique_together = ut
     90         
     91            # check for the %(class)s escape used for inherited permissions.
     92            # If present, replace it with the appropriate text based off of
     93            # the class name for both the name and codename of the permission.
     94            perms = meta_attrs.pop('permissions', self.permissions)
     95            translated_perms = []
     96            if perms:
     97                for codename, name in perms:
     98                    codename = codename % {'class': cls.__name__.lower()}
     99                    name = name % {'class': self.verbose_name}
     100                    translated_perms.append((codename, name),)
     101            self.permissions = translated_perms
    90102
    91103            # verbose_name_plural is a special case because it uses a 's'
    92104            # by default.
Back to Top