Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bypass load permission if migrations not completed #178

Merged
merged 1 commit into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions eox_tenant/api/v1/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.contrib.auth.models import Permission, User
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.db.utils import ProgrammingError
from rest_framework import exceptions, permissions

Expand All @@ -13,19 +14,22 @@ def load_permissions():
Helper method to load a custom permission on DB that will be
used to give access to the eox-tenant API.
"""
if settings.EOX_TENANT_LOAD_PERMISSIONS:
try:
content_type = ContentType.objects.get_for_model(User)
Permission.objects.get_or_create(
codename='can_call_eox_tenant',
name='Can access eox-tenant API',
content_type=content_type,
)
except ProgrammingError:
# This code runs when the app is loaded, if a migration has not been
# done a ProgrammingError exception is raised.
# we are bypassing those cases to let migrations run smoothly.
pass
try:
if settings.EOX_TENANT_LOAD_PERMISSIONS:
try:
content_type = ContentType.objects.get_for_model(User)
Permission.objects.get_or_create(
codename='can_call_eox_tenant',
name='Can access eox-tenant API',
content_type=content_type,
)
except ProgrammingError:
# This code runs when the app is loaded, if a migration has not been
# done a ProgrammingError exception is raised.
# we are bypassing those cases to let migrations run smoothly.
pass
except ImproperlyConfigured:
pass


class EoxTenantAPIPermission(permissions.BasePermission):
Expand Down