Skip to content

Refactor GraphQL and PDF support checks in settings.py to use importlib for module availability #263

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 9 additions & 14 deletions django_ledger/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Django Ledger created by Miguel Sanda <[email protected]>.
Copyright© EDMA Group Inc licensed under the GPLv3 Agreement.
"""

import importlib
import logging
from decimal import Decimal

Expand All @@ -10,21 +12,14 @@
logger = logging.getLogger('Django Ledger Logger')
logger.setLevel(logging.INFO)

try:
from graphene import __version__
from graphene_django import __version__
from oauth2_provider import __version__

DJANGO_LEDGER_GRAPHQL_SUPPORT_ENABLED = True
except ImportError:
DJANGO_LEDGER_GRAPHQL_SUPPORT_ENABLED = False

try:
from fpdf import FPDF
DJANGO_LEDGER_GRAPHQL_SUPPORT_ENABLED = all(
[
importlib.util.find_spec(module) is not None
for module in ["graphene", "graphene_django", "oauth2_provider"]
]
)

DJANGO_LEDGER_PDF_SUPPORT_ENABLED = True
except ImportError:
DJANGO_LEDGER_PDF_SUPPORT_ENABLED = False
DJANGO_LEDGER_PDF_SUPPORT_ENABLED = importlib.util.find_spec("fpdf") is not None

logger.info(f'Django Ledger GraphQL Enabled: {DJANGO_LEDGER_GRAPHQL_SUPPORT_ENABLED}')

Expand Down