Hi all,
I was browsing tickets and stumbled
upon https://code.djangoproject.com/ticket/10686, which was accepted except
for lack of documentation. The patch in the mentioned ticket makes it so
that a Model class that's used as base class with extra permissions:
class BaseTask(models.Model):
...
class Meta:
permissions = (
('view_%(class)s', 'Can view %(class)s'),
)
will make those permissions specific for any child classes:
class TodoTask(BaseTask):
pass
class AssignedTask(BaseTask):
assignee = models.ForeignKey(settings.AUTH_USER_MODEL)
class Meta:
permissions = (
('reassign_assignedtask', 'Can reassign assigned task')
)
With the current patch, TodoTask would have one extra permission:
view_todotask, and AssignedTask would have two: view_assignedtask and
reassign_assignedtask.
Now there were some concerns on the pull request, and input is needed:
- in its current state, Django will not inherit the base class
permissions, so AssignedTask would end up only with the
reassign_assignedtask permission (so missing view_assignedtask). The patch
ensures that inheritance is applied, and adds an extra Meta option:
inherit_permissions. Setting that to False yields the current behaviour -
no base class permissions are inherited. Disadvantages of this approach:
- yet another Meta attribute
- more tight coupling between Meta and django.contrib.permissions
(pointed out by Tim Graham)
- 'hidden' second feature within the initial enhancee
Two alternatives to avoid having to add inherit_permisisons have been
proposed by Tim:
class AssignedTask(BaseTask):
assignee = models.ForeignKey(settings.AUTH_USER_MODEL)
class Meta:
permissions = BaseTask.Meta.permissions + (
('reassign_assignedtask', 'Can reassign assigned task')
)
It looks like the inheritance of permissions would not be a default then,
which would require the other model to be changed to:
class TodoTask(BaseTask):
class Meta:
permissions = BaseTask.Meta.permissions
In my opinition this introduces boilerplate which you get for free if the
permissions are inherited by default. On the upside, it is more explicit.
Another suggestion was to use good old fashioned Python inheritance:
class TodoTask(BaseTask):
class Meta(BaseTask.Meta):
pass
class AssignedTask(BaseTask):
assignee = models.ForeignKey(settings.AUTH_USER_MODEL)
class Meta(BaseTask.Meta):
permissions = (
('reassign_assignedtask', 'Can reassign assigned task')
)
The latter two suggestions would ofcourse require fixing the patch, but
that's an implementation detail.
Input would be greatly valued.
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/646d7295-5183-42b1-be32-30967794b051%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.