custom permission strings: Avoid magic strings ...

34 views
Skip to first unread message

guettli

unread,
Aug 5, 2015, 10:29:23 AM8/5/15
to Django users
Hi,

We define the permission like in the docs: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#custom-permissions

To avoid the magic string 'fooapp.fooperm' in the code which uses the permission very often, I choose this solution:

{{{
# file models.py
class FooModel(Model):

    FOOPERM_PERMISSION_STRING='fooapp.fooperm'

    class Meta:
        permissions = (
            ('fooperm', 'Foo Permission'),
        )
}}}


{{{
# file views.py
# This and other files use the permission string very often.

...
    if request.user.has_perm(FooModel.FOOPERM_PERMISSION_STRING):
        ...
}}}


But still I am not happy with the above solution. I think it looks ugly.

How do you avoid the magic permission string in your code?

Regards,
  Thomas Güttler
Reply all
Reply to author
Forward
0 new messages