예제 #1
0
def _get_all_permissions(opts):
    """Copied from django.contrib.auth.management._get_all_permissions
    and modified to supply permission 'view'"""
    perms = []
    for action in ('view',):
        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
    return perms + list(opts.permissions)
예제 #2
0
def _get_all_permissions(opts):
    """Copied from django.contrib.auth.management._get_all_permissions
    and modified to supply permission 'view'"""
    perms = []
    for action in ('view', ):
        perms.append((_get_permission_codename(action, opts),
                      u'Can %s %s' % (action, opts.verbose_name_raw)))
    return perms + list(opts.permissions)
예제 #3
0
    def __init__(self, model, admin_site):
        """
        Every model that uses an AdminWithReadOnly needs a view_ permission
        as well. Rather than forcing users to hard-code this, I hope we
        can create it here. As the Admin class is instantiated when it's
        registered, and that usually happens when the file that registers
        it is read, this should be early enough that we can create fixtures
        for group permissions, trusting that the permission records already
        exist by that point.
        """

        # Copied from django/contrib/management/__init__.py, which
        # is unfortunately not reusable.

        # This will hold the permissions we're looking for as
        # (content_type, (codename, name))

        # The codenames and ctypes that should exist.
        from django.contrib.contenttypes.models import ContentType
        ctype = ContentType.objects.get_for_model(model)
        ctypes = set((ctype,))

        from django.contrib.auth.management import _get_permission_codename
        view_perm = (_get_permission_codename('view', model._meta),
            u'Can %s %s' % ('view', model._meta.verbose_name_raw))
        expected_perms = ((ctype, view_perm),)

        # Find all the Permissions that have a context_type for a model we're
        # looking for.  We don't need to check for codenames since we already have
        # a list of the ones we're going to create.
        from django.contrib.auth import models as auth_app
        found_perms = set(auth_app.Permission.objects.filter(
            content_type__in=ctypes,
        ).values_list(
            "content_type", "codename"
        ))

        objs = [
            auth_app.Permission(codename=codename, name=name, content_type=ctype)
            for ctype, (codename, name) in expected_perms
            if (ctype.pk, codename) not in found_perms
        ]

        for ctype, (codename, name) in expected_perms:
            # If the permissions exists, move on.
            if (ctype.pk, codename) in found_perms:
                continue
            p = auth_app.Permission.objects.create(
                codename=codename,
                name=name,
                content_type=ctype
            )

        super(AdminWithReadOnly, self).__init__(model, admin_site)
예제 #4
0
def create_extra_permission(sender, **kwargs):
    from django.contrib.auth.models import Permission
    from django.db.models.loading import get_models
    from django.contrib.contenttypes.models import ContentType

    for model in get_models(sender):
        for action in ('read', ):
            opts = model._meta
            codename = _get_permission_codename(action, opts)
            label = u'Can %s %s' % (action, opts.verbose_name_raw)
            ct = ContentType.objects.get_for_model(model)
            Permission.objects.get_or_create(codename=codename, content_type=ct, defaults={'name': label})
예제 #5
0
def create_extra_permission(sender, **kwargs):
    from django.contrib.auth.models import Permission
    from django.db.models.loading import get_models
    from django.contrib.contenttypes.models import ContentType

    for model in get_models(sender):
        for action in ('export', 'massupdate', 'import'):
            opts = model._meta
            codename = _get_permission_codename(action, opts)
            label = u'Can %s %s' % (action, opts.verbose_name_raw)
            ct = ContentType.objects.get_for_model(model)
            Permission.objects.get_or_create(codename=codename,
                                             content_type=ct,
                                             defaults={'name': label})
def _get_all_permissions(opts):
    "Returns (codename, name) for all permissions in the given opts."
    perms = []
    for action in settings.OBJECT_PERMISSION_EXTRA_DEFAULT_PERMISSIONS:
        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
    return perms + list(opts.permissions)
예제 #7
0
def init_perms_for_groups():

    from gasistafelice.base.models import Person, Place, Contact
    from gasistafelice.gas.models import GAS, GASConfig, GASMember
    from gasistafelice.supplier.models import (
        SupplierConfig, SupplierProductCategory, ProductCategory,
        SupplierStock, Product, Supplier
    )
    from django.contrib.auth.models import User
    from django.contrib.auth.management import _get_permission_codename
    
    g_techs = Group.objects.get(name=GROUP_TECHS)
    g_suppliers = Group.objects.get(name=GROUP_SUPPLIERS)
    g_referrers_suppliers = Group.objects.get(name=GROUP_REFERRER_SUPPLIERS)
    g_gasmembers = Group.objects.get(name=GROUP_MEMBERS)

    techs_perms_d = {
        Person : ('add', 'change', 'delete'),
        Place : ('add', 'change', 'delete'),
        Contact : ('add', 'change', 'delete'),
        GAS : ('change',),
        GASConfig : ('change',),
        SupplierConfig : ('change',),
        GASMember : ('add', 'change', 'delete'),
        SupplierProductCategory : ('add', 'change', 'delete'),
        ProductCategory : ('add', 'change', 'delete'),
        SupplierStock : ('add', 'change', 'delete'),
        Product : ('add', 'change', 'delete'),
        Supplier : ('add', 'change'),
        User : ('add', 'change',), # add User is important for Add GASMember Form! Leave it here now. TODO
    }

    supplier_perms_d = {
        Person : ('add', 'change'),
        Place : ('add', 'change'),
        Contact : ('add', 'change'),
        SupplierConfig : ('change',),
        SupplierProductCategory : ('add', 'change', 'delete'),
        SupplierStock : ('add', 'change', 'delete'),
        Product : ('add', 'change', 'delete'),
        Supplier : ('change',),
    }

    gas_referrer_supplier_perms_d = supplier_perms_d.copy()
    gas_referrer_supplier_perms_d.update({
        Supplier : ('add', 'change'),
    })

    gm_perms_d = {
        Person : ('change',),
        Place : ('add', 'change',),
        Contact : ('add', 'change',),
    }

    group_perms_d_tuples = (
        (g_techs , techs_perms_d),
        (g_suppliers , supplier_perms_d),
        (g_referrers_suppliers , gas_referrer_supplier_perms_d),
        (g_gasmembers , gm_perms_d),
    )

    for gr, perms_d in group_perms_d_tuples:
        for klass, actions in perms_d.items():
            ctype = ContentType.objects.get_for_model(klass)
            for action in actions:
                codename = _get_permission_codename(action, klass._meta)
                log.debug("Adding perm %s to group %s" % (codename, gr))
                p = Permission.objects.get(
                    content_type=ctype, codename=codename
                )
                gr.permissions.add(p)
예제 #8
0
def _get_all_permissions(opts):
    "Returns (codename, name) for all permissions in the given opts."
    perms = []
    for action in ('view',):
        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
    return perms + list(opts.permissions)