stages/invitation: move invite signals from core to app
This commit is contained in:
parent
b048a1fb4f
commit
6bc6f947dd
|
@ -11,10 +11,10 @@ from django.views.generic import ListView
|
||||||
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
||||||
|
|
||||||
from passbook.admin.views.utils import DeleteMessageView
|
from passbook.admin.views.utils import DeleteMessageView
|
||||||
from passbook.core.signals import invitation_created
|
|
||||||
from passbook.lib.views import CreateAssignPermView
|
from passbook.lib.views import CreateAssignPermView
|
||||||
from passbook.stages.invitation.forms import InvitationForm
|
from passbook.stages.invitation.forms import InvitationForm
|
||||||
from passbook.stages.invitation.models import Invitation
|
from passbook.stages.invitation.models import Invitation
|
||||||
|
from passbook.stages.invitation.signals import invitation_created
|
||||||
|
|
||||||
|
|
||||||
class InvitationListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
class InvitationListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
"""passbook audit signal listener"""
|
"""passbook audit signal listener"""
|
||||||
from typing import Dict
|
from threading import Thread
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from django.contrib.auth.signals import (
|
from django.contrib.auth.signals import (
|
||||||
user_logged_in,
|
user_logged_in,
|
||||||
|
@ -11,21 +12,43 @@ from django.http import HttpRequest
|
||||||
|
|
||||||
from passbook.audit.models import Event, EventAction
|
from passbook.audit.models import Event, EventAction
|
||||||
from passbook.core.models import User
|
from passbook.core.models import User
|
||||||
from passbook.core.signals import invitation_created, invitation_used, user_signed_up
|
from passbook.core.signals import user_signed_up
|
||||||
|
from passbook.stages.invitation.signals import invitation_created, invitation_used
|
||||||
|
|
||||||
|
class EventNewThread(Thread):
|
||||||
|
"""Create Event in background thread"""
|
||||||
|
|
||||||
|
action: EventAction
|
||||||
|
request: HttpRequest
|
||||||
|
kwargs: Dict[str, Any]
|
||||||
|
user: Optional[User]
|
||||||
|
|
||||||
|
def __init__(self, action: EventAction, request: HttpRequest, **kwargs):
|
||||||
|
super().__init__()
|
||||||
|
self.action = action
|
||||||
|
self.request = request
|
||||||
|
self.kwargs = kwargs
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
Event.new(self.action, **self.kwargs).from_http(self.request, user=self.user)
|
||||||
|
|
||||||
|
|
||||||
@receiver(user_logged_in)
|
@receiver(user_logged_in)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def on_user_logged_in(sender, request: HttpRequest, user: User, **_):
|
def on_user_logged_in(sender, request: HttpRequest, user: User, **_):
|
||||||
"""Log successful login"""
|
"""Log successful login"""
|
||||||
Event.new(EventAction.LOGIN).from_http(request)
|
thread = EventNewThread(EventAction.LOGIN, request)
|
||||||
|
thread.user = user
|
||||||
|
thread.run()
|
||||||
|
|
||||||
|
|
||||||
@receiver(user_logged_out)
|
@receiver(user_logged_out)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def on_user_logged_out(sender, request: HttpRequest, user: User, **_):
|
def on_user_logged_out(sender, request: HttpRequest, user: User, **_):
|
||||||
"""Log successfully logout"""
|
"""Log successfully logout"""
|
||||||
Event.new(EventAction.LOGOUT).from_http(request)
|
thread = EventNewThread(EventAction.LOGOUT, request)
|
||||||
|
thread.user = user
|
||||||
|
thread.run()
|
||||||
|
|
||||||
|
|
||||||
@receiver(user_login_failed)
|
@receiver(user_login_failed)
|
||||||
|
@ -34,29 +57,25 @@ def on_user_login_failed(
|
||||||
sender, credentials: Dict[str, str], request: HttpRequest, **_
|
sender, credentials: Dict[str, str], request: HttpRequest, **_
|
||||||
):
|
):
|
||||||
"""Failed Login"""
|
"""Failed Login"""
|
||||||
Event.new(EventAction.LOGIN_FAILED, **credentials).from_http(request)
|
thread = EventNewThread(EventAction.LOGIN_FAILED, request, **credentials)
|
||||||
|
thread.run()
|
||||||
|
|
||||||
@receiver(user_signed_up)
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def on_user_signed_up(sender, request: HttpRequest, user: User, **_):
|
|
||||||
"""Log successfully signed up"""
|
|
||||||
Event.new(EventAction.SIGN_UP).from_http(request)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(invitation_created)
|
@receiver(invitation_created)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def on_invitation_created(sender, request: HttpRequest, invitation, **_):
|
def on_invitation_created(sender, request: HttpRequest, invitation, **_):
|
||||||
"""Log Invitation creation"""
|
"""Log Invitation creation"""
|
||||||
Event.new(
|
thread = EventNewThread(
|
||||||
EventAction.INVITE_CREATED, invitation_uuid=invitation.uuid.hex
|
EventAction.INVITE_CREATED, request, invitation_uuid=invitation.uuid.hex
|
||||||
).from_http(request)
|
)
|
||||||
|
thread.run()
|
||||||
|
|
||||||
|
|
||||||
@receiver(invitation_used)
|
@receiver(invitation_used)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def on_invitation_used(sender, request: HttpRequest, invitation, **_):
|
def on_invitation_used(sender, request: HttpRequest, invitation, **_):
|
||||||
"""Log Invitation usage"""
|
"""Log Invitation usage"""
|
||||||
Event.new(EventAction.INVITE_USED, invitation_uuid=invitation.uuid.hex).from_http(
|
thread = EventNewThread(
|
||||||
request
|
EventAction.INVITE_USED, request, invitation_uuid=invitation.uuid.hex
|
||||||
)
|
)
|
||||||
|
thread.run()
|
||||||
|
|
|
@ -2,6 +2,4 @@
|
||||||
from django.core.signals import Signal
|
from django.core.signals import Signal
|
||||||
|
|
||||||
user_signed_up = Signal(providing_args=["request", "user"])
|
user_signed_up = Signal(providing_args=["request", "user"])
|
||||||
invitation_created = Signal(providing_args=["request", "invitation"])
|
|
||||||
invitation_used = Signal(providing_args=["request", "invitation", "user"])
|
|
||||||
password_changed = Signal(providing_args=["user", "password"])
|
password_changed = Signal(providing_args=["user", "password"])
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
"""passbook invitation signals"""
|
||||||
|
from django.core.signals import Signal
|
||||||
|
|
||||||
|
invitation_created = Signal(providing_args=["request", "invitation"])
|
||||||
|
invitation_used = Signal(providing_args=["request", "invitation"])
|
|
@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404
|
||||||
from passbook.flows.stage import StageView
|
from passbook.flows.stage import StageView
|
||||||
from passbook.stages.invitation.models import Invitation, InvitationStage
|
from passbook.stages.invitation.models import Invitation, InvitationStage
|
||||||
from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT
|
from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT
|
||||||
|
from passbook.stages.invitation.signals import invitation_used
|
||||||
|
|
||||||
INVITATION_TOKEN_KEY = "token"
|
INVITATION_TOKEN_KEY = "token"
|
||||||
INVITATION_IN_EFFECT = "invitation_in_effect"
|
INVITATION_IN_EFFECT = "invitation_in_effect"
|
||||||
|
@ -25,4 +26,5 @@ class InvitationStageView(StageView):
|
||||||
invite: Invitation = get_object_or_404(Invitation, pk=token)
|
invite: Invitation = get_object_or_404(Invitation, pk=token)
|
||||||
self.executor.plan.context[PLAN_CONTEXT_PROMPT] = invite.fixed_data
|
self.executor.plan.context[PLAN_CONTEXT_PROMPT] = invite.fixed_data
|
||||||
self.executor.plan.context[INVITATION_IN_EFFECT] = True
|
self.executor.plan.context[INVITATION_IN_EFFECT] = True
|
||||||
|
invitation_used.send(sender=self, request=request, invitation=invite)
|
||||||
return self.executor.stage_ok()
|
return self.executor.stage_ok()
|
||||||
|
|
Reference in New Issue