oncall-engine/engine/apps/api/views/features.py
Vadim Stepanov b2f4ffb98a
apps.get_model -> import (#2619)
# What this PR does

Remove
[`apps.get_model`](https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.apps.get_model)
invocations and use inline `import` statements in places where models
are imported within functions/methods to avoid circular imports.

I believe `import` statements are more appropriate for most use cases as
they allow for better static code analysis & formatting, and solve the
issue of circular imports without being unnecessarily dynamic as
`apps.get_model`. With `import` statements, it's possible to:

- Jump to model definitions in most IDEs
- Automatically sort inline imports with `isort`
- Find import errors faster/easier (most IDEs highlight broken imports)
- Have more consistency across regular & inline imports when importing
models

This PR also adds a flake8 rule to ban imports of `django.apps.apps`, so
it's harder to use `apps.get_model` by mistake (it's possible to ignore
this rule by using `# noqa: I251`). The rule is not enforced on
directories with migration files, because `apps.get_model` is often used
to get a historical state of a model, which is useful when writing
migrations ([see this SO answer for more
details](https://stackoverflow.com/a/37769213)). So `apps.get_model` is
considered OK in migrations (even necessary in some cases).

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-25 09:43:23 +00:00

65 lines
2.3 KiB
Python

from django.conf import settings
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.auth_token.auth import PluginAuthentication
from apps.base.utils import live_settings
FEATURE_SLACK = "slack"
FEATURE_TELEGRAM = "telegram"
FEATURE_LIVE_SETTINGS = "live_settings"
FEATURE_GRAFANA_CLOUD_NOTIFICATIONS = "grafana_cloud_notifications"
FEATURE_GRAFANA_CLOUD_CONNECTION = "grafana_cloud_connection"
FEATURE_WEB_SCHEDULES = "web_schedules"
FEATURE_WEBHOOKS2 = "webhooks2"
class FeaturesAPIView(APIView):
"""
Return whitelist of enabled features.
It is needed to disable features for On-prem installations.
"""
authentication_classes = (PluginAuthentication,)
def get(self, request):
return Response(self._get_enabled_features(request))
def _get_enabled_features(self, request):
from apps.base.models import DynamicSetting
enabled_features = []
if settings.FEATURE_SLACK_INTEGRATION_ENABLED:
enabled_features.append(FEATURE_SLACK)
if settings.FEATURE_TELEGRAM_INTEGRATION_ENABLED:
enabled_features.append(FEATURE_TELEGRAM)
if settings.IS_OPEN_SOURCE:
# Features below should be enabled only in OSS
enabled_features.append(FEATURE_GRAFANA_CLOUD_CONNECTION)
if settings.FEATURE_LIVE_SETTINGS_ENABLED:
enabled_features.append(FEATURE_LIVE_SETTINGS)
if live_settings.GRAFANA_CLOUD_NOTIFICATIONS_ENABLED:
enabled_features.append(FEATURE_GRAFANA_CLOUD_NOTIFICATIONS)
if settings.FEATURE_WEB_SCHEDULES_ENABLED:
enabled_features.append(FEATURE_WEB_SCHEDULES)
else:
# allow enabling web schedules per org, independently of global status flag
enabled_web_schedules_orgs = DynamicSetting.objects.get_or_create(
name="enabled_web_schedules_orgs",
defaults={
"json_value": {
"org_ids": [],
}
},
)[0]
if request.auth.organization.pk in enabled_web_schedules_orgs.json_value["org_ids"]:
enabled_features.append(FEATURE_WEB_SCHEDULES)
if settings.FEATURE_WEBHOOKS_2_ENABLED:
enabled_features.append(FEATURE_WEBHOOKS2)
return enabled_features