Giter Site home page Giter Site logo

Comments (5)

lefterisnik avatar lefterisnik commented on August 18, 2024

Hi @ciarancourtney,

OK, I see your problem.

You could try to trigger the signal manually like this:

# -*- coding: utf-8 -*-

"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
base model app_label (eg: for "article" instead of "about", for the About proxy
model).
What we need, however, is that the permission be created for the proxy model
itself, in order to have the proper entries displayed in the admin.
"""

import sys

from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.apps import apps
from admin_view_permission.apps import update_permissions


class Command(BaseCommand):
    help = "Fix permissions for proxy models."

    def handle(self, *args, **options):
        update_permissions(
            apps.get_app_config('app'),  # where app is the app of the management command
            apps.get_app_config('app'),
            verbosity=1,
            interactive=True,
            using='default',
        )

        for model in apps.get_models():
            opts = model._meta
            ctype, created = ContentType.objects.get_or_create(
                app_label=opts.app_label,
                model=opts.object_name.lower())

            for codename, name in _get_all_permissions(opts):
                p, created = Permission.objects.get_or_create(
                    codename=codename,
                    content_type=ctype,
                    defaults={'name': name})
                if created:
                    sys.stdout.write('Adding permission {}\n'.format(p))

Although, the admin_view_permission seems not to work correctly with the proxy models (doesn't return any field to the view). You could try it too. I will have a better look on this tomorrow.

I hope this could help you.

Thanks,
Lefteris

from django-admin-view-permission.

ciarancourtney avatar ciarancourtney commented on August 18, 2024

Thanks, I got around the issue by manually creating the admin-view-permission in my proxy meta class.

    permissions = (
        ('view_myuser', "Can view My User"),
    )

Then I added a RunPython Data Migration to the autogenerated migration script:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-05-31 16:17
from __future__ import unicode_literals

from django.db import migrations
from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType


# forward func
def add_myuser_proxy_permissions(apps, schema_editor):
    app_config = apps.get_app_config('profiles')
    model = app_config.get_model('MyUser')
    opts = model._meta
    ctype, created = ContentType.objects.get_or_create(
        app_label=opts.app_label,
        model=opts.object_name.lower(),
    )

    for codename, name in _get_all_permissions(opts):
        p, created = Permission.objects.get_or_create(
            codename=codename,
            content_type=ctype,
            defaults={'name': f'{name} (Proxy)'})
        if created:
            print(f'Adding permission: {p}')


# reverse func
def delete_myuser_proxy_permissions(apps, schema_editor):
    permissions = Permission.objects.filter(codename__icontains='myuser')
    permissions.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('profiles', '0015_auto_20170531_0922'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='myuser',
            options={'permissions': (('view_myuser', 'Can view My User'),), 'verbose_name': 'My User', 'verbose_name_plural': 'My Users'},
        ),
        migrations.RunPython(add_myuser_proxy_permissions, delete_myuser_proxy_permissions),
    ]

When a user with view_myuser (proxy) and view_profile visits the change page of a MyUser, the Django User fields are editable, but this is probably a quirk of the builtin UserAdmin form, The Save buttons do not appear, as expected

from django-admin-view-permission.

lefterisnik avatar lefterisnik commented on August 18, 2024

Hi @ciarancourtney,

Which Django version do you use? I am testing the behaviour on Django 1.11 and it doesn't return any fields because the code tries to get the local fields and the proxy model doesn't contain any local fields (I will push a fix to master shortly).

Thanks,
Lefteris

from django-admin-view-permission.

ciarancourtney avatar ciarancourtney commented on August 18, 2024

I'm on the latest, Django 1.11.2 and DAVP 0.9. Perhaps its because I'm subclassing the UserCreationForm and UserChangeForm

from django-admin-view-permission.

lefterisnik avatar lefterisnik commented on August 18, 2024

Hi @ciarancourtney,

I am going to close this ticket. I've added the management command(which fixes the permission for the proxy models) to the package and also I've fixed the get_readonly_fields to return the appropriate fields.

Thanks,
Lefteris

from django-admin-view-permission.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.