mirror of
https://github.com/simple-login/app.git
synced 2024-11-16 17:08:30 +01:00
chore: emit audit log for manual subscription (#2277)
* chore: emit audit log for manual subscription * chore: emit event to event_dispatcher on manual subscription action
This commit is contained in:
parent
6446f0eaa7
commit
a585a84302
1 changed files with 56 additions and 20 deletions
|
@ -16,6 +16,8 @@ from flask_admin.contrib import sqla
|
||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
|
|
||||||
from app.db import Session
|
from app.db import Session
|
||||||
|
from app.events.event_dispatcher import EventDispatcher
|
||||||
|
from app.events.generated.event_pb2 import EventContent, UserPlanChanged
|
||||||
from app.models import (
|
from app.models import (
|
||||||
User,
|
User,
|
||||||
ManualSubscription,
|
ManualSubscription,
|
||||||
|
@ -39,6 +41,7 @@ from app.models import (
|
||||||
UserAuditLog,
|
UserAuditLog,
|
||||||
)
|
)
|
||||||
from app.newsletter_utils import send_newsletter_to_user, send_newsletter_to_address
|
from app.newsletter_utils import send_newsletter_to_user, send_newsletter_to_address
|
||||||
|
from app.user_audit_log_utils import emit_user_audit_log, UserAuditLogAction
|
||||||
|
|
||||||
|
|
||||||
def _admin_action_formatter(view, context, model, name):
|
def _admin_action_formatter(view, context, model, name):
|
||||||
|
@ -351,15 +354,40 @@ def manual_upgrade(way: str, ids: [int], is_giveaway: bool):
|
||||||
manual_sub.end_at = manual_sub.end_at.shift(years=1)
|
manual_sub.end_at = manual_sub.end_at.shift(years=1)
|
||||||
else:
|
else:
|
||||||
manual_sub.end_at = arrow.now().shift(years=1, days=1)
|
manual_sub.end_at = arrow.now().shift(years=1, days=1)
|
||||||
|
emit_user_audit_log(
|
||||||
|
user=user,
|
||||||
|
action=UserAuditLogAction.Upgrade,
|
||||||
|
message=f"Admin {current_user.email} extended manual subscription to user {user.email}",
|
||||||
|
)
|
||||||
|
EventDispatcher.send_event(
|
||||||
|
user=user,
|
||||||
|
content=EventContent(
|
||||||
|
user_plan_change=UserPlanChanged(
|
||||||
|
plan_end_time=manual_sub.end_at.timestamp
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
flash(f"Subscription extended to {manual_sub.end_at.humanize()}", "success")
|
flash(f"Subscription extended to {manual_sub.end_at.humanize()}", "success")
|
||||||
continue
|
else:
|
||||||
|
emit_user_audit_log(
|
||||||
ManualSubscription.create(
|
user=user,
|
||||||
|
action=UserAuditLogAction.Upgrade,
|
||||||
|
message=f"Admin {current_user.email} created manual subscription to user {user.email}",
|
||||||
|
)
|
||||||
|
manual_sub = ManualSubscription.create(
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
end_at=arrow.now().shift(years=1, days=1),
|
end_at=arrow.now().shift(years=1, days=1),
|
||||||
comment=way,
|
comment=way,
|
||||||
is_giveaway=is_giveaway,
|
is_giveaway=is_giveaway,
|
||||||
)
|
)
|
||||||
|
EventDispatcher.send_event(
|
||||||
|
user=user,
|
||||||
|
content=EventContent(
|
||||||
|
user_plan_change=UserPlanChanged(
|
||||||
|
plan_end_time=manual_sub.end_at.timestamp
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
flash(f"New {way} manual subscription for {user} is created", "success")
|
flash(f"New {way} manual subscription for {user} is created", "success")
|
||||||
Session.commit()
|
Session.commit()
|
||||||
|
@ -453,14 +481,7 @@ class ManualSubscriptionAdmin(SLModelView):
|
||||||
"Extend 1 year more?",
|
"Extend 1 year more?",
|
||||||
)
|
)
|
||||||
def extend_1y(self, ids):
|
def extend_1y(self, ids):
|
||||||
for ms in ManualSubscription.filter(ManualSubscription.id.in_(ids)):
|
self.__extend_manual_subscription(ids, msg="1 year", years=1)
|
||||||
ms.end_at = ms.end_at.shift(years=1)
|
|
||||||
flash(f"Extend subscription for 1 year for {ms.user}", "success")
|
|
||||||
AdminAuditLog.extend_subscription(
|
|
||||||
current_user.id, ms.user.id, ms.end_at, "1 year"
|
|
||||||
)
|
|
||||||
|
|
||||||
Session.commit()
|
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
"extend_1m",
|
"extend_1m",
|
||||||
|
@ -468,11 +489,26 @@ class ManualSubscriptionAdmin(SLModelView):
|
||||||
"Extend 1 month more?",
|
"Extend 1 month more?",
|
||||||
)
|
)
|
||||||
def extend_1m(self, ids):
|
def extend_1m(self, ids):
|
||||||
|
self.__extend_manual_subscription(ids, msg="1 month", months=1)
|
||||||
|
|
||||||
|
def __extend_manual_subscription(self, ids: List[int], msg: str, **kwargs):
|
||||||
for ms in ManualSubscription.filter(ManualSubscription.id.in_(ids)):
|
for ms in ManualSubscription.filter(ManualSubscription.id.in_(ids)):
|
||||||
ms.end_at = ms.end_at.shift(months=1)
|
sub: ManualSubscription = ms
|
||||||
flash(f"Extend subscription for 1 month for {ms.user}", "success")
|
sub.end_at = sub.end_at.shift(**kwargs)
|
||||||
|
flash(f"Extend subscription for {msg} for {sub.user}", "success")
|
||||||
|
emit_user_audit_log(
|
||||||
|
user=sub.user,
|
||||||
|
action=UserAuditLogAction.Upgrade,
|
||||||
|
message=f"Admin {current_user.email} extended manual subscription for {msg} for {sub.user}",
|
||||||
|
)
|
||||||
AdminAuditLog.extend_subscription(
|
AdminAuditLog.extend_subscription(
|
||||||
current_user.id, ms.user.id, ms.end_at, "1 month"
|
current_user.id, sub.user.id, sub.end_at, msg
|
||||||
|
)
|
||||||
|
EventDispatcher.send_event(
|
||||||
|
user=sub.user,
|
||||||
|
content=EventContent(
|
||||||
|
user_plan_change=UserPlanChanged(plan_end_time=sub.end_at.timestamp)
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
Session.commit()
|
Session.commit()
|
||||||
|
|
Loading…
Reference in a new issue