Friday, December 15, 2017

custom permission strings: Avoid magic strings ...

Hi,

We define the permission like in the docs: https://docs.djangoproject.com/en/1.11/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


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/21f0b282-66fb-46fb-9513-8f4db19b4cf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment