Skip to content

Commit

Permalink
Rename get_trigger_data_func to get_trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Sep 12, 2022
1 parent c65a313 commit ac430eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tests/zapier/triggers/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.request import Request

from zapier.triggers.models import TriggerSubscription
from zapier.triggers.settings import get_trigger_data_func
from zapier.triggers.settings import get_trigger
from zapier.triggers.types import TriggerData
from zapier.triggers.views import TriggerView

Expand All @@ -30,7 +30,7 @@ def test_auth_check__missing(client: Client, active_token: Token) -> None:
@pytest.mark.django_db
class TestTriggerView:
def get_new_book_data(self, request: Request) -> TriggerData:
return get_trigger_data_func("new_book")(request)
return get_trigger("new_book")(request)

def test_get(self, rf: RequestFactory, active_token: Token) -> None:
view = TriggerView.as_view()
Expand Down
26 changes: 13 additions & 13 deletions zapier/triggers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

from .types import TriggerViewFunc


def import_from_path(path: str) -> Type | Callable:
"""Import function from string path."""
module, func_or_klass = path.rsplit(".", 1)
m = importlib.import_module(module)
return getattr(m, func_or_klass)


# read in settings that have been overridden in django settings.py
_settings = getattr(django_settings, "ZAPIER_TRIGGERS", {})
_settings.setdefault("AUTHENTICATOR", None)
Expand All @@ -32,20 +24,28 @@ def import_from_path(path: str) -> Type | Callable:
TRIGGERS = _settings["TRIGGERS"]


def import_from_path(path: str) -> Type | Callable:
"""Import function from string path."""
module, func_or_klass = path.rsplit(".", 1)
m = importlib.import_module(module)
return getattr(m, func_or_klass)


def get_authenticator() -> BaseAuthentication:
"""Return authentication class imported from AUTHENTICATOR setting."""
if not (path := _settings.get("AUTHENTICATOR")):
if not (path := _settings["AUTHENTICATOR"]):
raise ImproperlyConfigured("Missing AUTHENTICATOR.")
return import_from_path(path)


def get_trigger_data_func(trigger: str) -> TriggerViewFunc:
def get_trigger(trigger: str) -> TriggerViewFunc:
"""Return view data function configured for the trigger."""
if not (func_path := _settings.get("TRIGGERS", {}).get(trigger)):
try:
return import_from_path(_settings["TRIGGERS"][trigger])
except KeyError:
raise ImproperlyConfigured("Missing trigger view function.")
return import_from_path(func_path)


def trigger_exists(trigger: str) -> bool:
"""Check that the trigger is configured."""
return trigger in _settings.get("TRIGGERS", {})
return trigger in _settings["TRIGGERS"]
9 changes: 2 additions & 7 deletions zapier/triggers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@
from .models import TriggerEvent, TriggerSubscription
from .permissions import IsZapier
from .response import JsonResponse
from .settings import (
HOOK_URL_KEY,
get_authenticator,
get_trigger_data_func,
trigger_exists,
)
from .settings import HOOK_URL_KEY, get_authenticator, get_trigger, trigger_exists
from .subscription import subscribe, unsubscribe
from .types import TriggerData, TriggerViewMethod

Expand Down Expand Up @@ -92,7 +87,7 @@ class TriggerView(APIView):

def get_trigger_data(self, request: Request, trigger: str) -> TriggerData:
"""Call the configured trigger view function."""
return get_trigger_data_func(trigger)(request)
return get_trigger(trigger)(request)

@trigger_method
def get(self, request: Request, trigger: str) -> JsonResponse:
Expand Down

0 comments on commit ac430eb

Please sign in to comment.