Skip to content

Commit 35898ee

Browse files
committed
Refactor AuditAction into Target & Action
Refactor AuditAction into AuditTarget & AuditAction
1 parent 313c143 commit 35898ee

File tree

5 files changed

+38
-30
lines changed

5 files changed

+38
-30
lines changed

django/thunderstore/community/models/package_listing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
AuditAction,
2323
AuditEvent,
2424
AuditEventField,
25+
AuditTarget,
2526
fire_audit_event,
2627
)
2728

@@ -168,6 +169,7 @@ def get_full_url(self):
168169
def build_audit_event(
169170
self,
170171
*,
172+
target: AuditTarget,
171173
action: AuditAction,
172174
user_id: Optional[int],
173175
message: Optional[str] = None,
@@ -176,6 +178,7 @@ def build_audit_event(
176178
timestamp=timezone.now(),
177179
user_id=user_id,
178180
community_id=self.community.pk,
181+
target=target,
179182
action=action,
180183
message=message,
181184
related_url=self.get_full_url(),
@@ -223,7 +226,8 @@ def reject(
223226
message = "\n\n".join(filter(bool, (rejection_reason, internal_notes)))
224227
fire_audit_event(
225228
self.build_audit_event(
226-
action=AuditAction.PACKAGE_REJECTED,
229+
target=AuditTarget.LISTING,
230+
action=AuditAction.REJECTED,
227231
user_id=agent.pk if agent else None,
228232
message=message,
229233
)
@@ -249,7 +253,8 @@ def approve(
249253
)
250254
fire_audit_event(
251255
self.build_audit_event(
252-
action=AuditAction.PACKAGE_APPROVED,
256+
target=AuditTarget.LISTING,
257+
action=AuditAction.APPROVED,
253258
user_id=agent.pk if agent else None,
254259
message=internal_notes,
255260
)

django/thunderstore/repository/models/package_version.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
AuditAction,
2727
AuditEvent,
2828
AuditEventField,
29+
AuditTarget,
2930
fire_audit_event,
3031
)
3132
from thunderstore.webhooks.models.release import Webhook
@@ -329,13 +330,15 @@ def log_download_event(version_id: int, client_ip: Optional[str]):
329330
def build_audit_event(
330331
self,
331332
*,
333+
target: AuditTarget,
332334
action: AuditAction,
333335
user_id: Optional[int],
334336
message: Optional[str] = None,
335337
) -> AuditEvent:
336338
return AuditEvent(
337339
timestamp=timezone.now(),
338340
user_id=user_id,
341+
target=target,
339342
action=action,
340343
message=message,
341344
related_url=self.package.get_view_on_site_url(),
@@ -363,7 +366,8 @@ def reject(
363366

364367
fire_audit_event(
365368
self.build_audit_event(
366-
action=AuditAction.VERSION_REJECTED,
369+
target=AuditTarget.VERSION,
370+
action=AuditAction.REJECTED,
367371
user_id=agent.pk if agent else None,
368372
message=message,
369373
)
@@ -387,7 +391,8 @@ def approve(
387391

388392
fire_audit_event(
389393
self.build_audit_event(
390-
action=AuditAction.VERSION_APPROVED,
394+
target=AuditTarget.VERSION,
395+
action=AuditAction.APPROVED,
391396
user_id=agent.pk if agent else None,
392397
message=message,
393398
)

django/thunderstore/webhooks/audit.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
from pydantic import BaseModel
77

88

9+
class AuditTarget(str, Enum):
10+
PACKAGE = "PACKAGE"
11+
LISTING = "LISTING"
12+
VERSION = "VERSION"
13+
14+
915
class AuditAction(str, Enum):
10-
PACKAGE_REJECTED = "PACKAGE_REJECTED"
11-
PACKAGE_APPROVED = "PACKAGE_APPROVED"
12-
PACKAGE_WARNING = "PACKAGE_WARNING"
13-
LISTING_REJECTED = "LISTING_REJECTED"
14-
LISTING_APPROVED = "LISTING_APPROVED"
15-
LISTING_WARNING = "LISTING_WARNING"
16-
VERSION_REJECTED = "VERSION_REJECTED"
17-
VERSION_APPROVED = "VERSION_APPROVED"
18-
VERSION_WARNING = "VERSION_WARNING"
16+
REJECTED = "REJECTED"
17+
APPROVED = "APPROVED"
18+
WARNING = "WARNING"
1919

2020

2121
class AuditEventField(BaseModel):
@@ -27,6 +27,7 @@ class AuditEvent(BaseModel):
2727
timestamp: datetime
2828
user_id: Optional[int]
2929
community_id: Optional[int]
30+
target: AuditTarget
3031
action: AuditAction
3132
message: Optional[str]
3233
related_url: Optional[str]

django/thunderstore/webhooks/models/audit.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ def get_for_event(cls, event: AuditEvent) -> QuerySet["AuditWebhook"]:
4040

4141
@staticmethod
4242
def get_event_color(action: AuditAction) -> int:
43-
action_str = str(action).lower()
44-
if "approve" in action_str:
43+
if action == AuditAction.APPROVED:
4544
return 5763719
46-
if "reject" in action_str:
45+
if action == AuditAction.REJECTED:
4746
return 15548997
48-
if "warning" in action_str:
47+
if action == AuditAction.WARNING:
4948
return 16705372
5049
return 9807270
5150

@@ -64,7 +63,7 @@ def render_event(event: AuditEvent) -> DiscordPayload:
6463
return DiscordPayload(
6564
embeds=[
6665
DiscordEmbed(
67-
title=event.action,
66+
title=event.target + "_" + event.action,
6867
description=event.message,
6968
url=event.related_url,
7069
color=AuditWebhook.get_event_color(event.action),

django/thunderstore/webhooks/tasks/tests/test_audit.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1+
import itertools
12
from typing import Optional
23

34
import pytest
45

56
from thunderstore.community.models import PackageListing
67
from thunderstore.core.types import UserType
7-
from thunderstore.webhooks.audit import AuditAction
8+
from thunderstore.webhooks.audit import AuditAction, AuditTarget
89
from thunderstore.webhooks.models import AuditWebhook
910
from thunderstore.webhooks.tasks import process_audit_event
1011

1112

1213
@pytest.mark.django_db
1314
@pytest.mark.parametrize(
14-
"action",
15-
(
16-
AuditAction.PACKAGE_APPROVED,
17-
AuditAction.PACKAGE_REJECTED,
18-
AuditAction.PACKAGE_WARNING,
19-
AuditAction.LISTING_REJECTED,
20-
AuditAction.LISTING_APPROVED,
21-
AuditAction.LISTING_WARNING,
22-
AuditAction.VERSION_REJECTED,
23-
AuditAction.VERSION_APPROVED,
24-
AuditAction.VERSION_WARNING,
15+
("target", "action"),
16+
list(
17+
itertools.product(
18+
[AuditTarget.PACKAGE, AuditTarget.LISTING, AuditTarget.VERSION],
19+
[AuditAction.APPROVED, AuditAction.REJECTED, AuditAction.WARNING],
20+
)
2521
),
2622
)
2723
@pytest.mark.parametrize("message", (None, "Test message"))
2824
def test_process_audit_event(
2925
active_package_listing: PackageListing,
3026
user: UserType,
27+
target: AuditTarget,
3128
action: AuditAction,
3229
message: Optional[str],
3330
mocker,
@@ -41,6 +38,7 @@ def test_process_audit_event(
4138
)
4239
webhook.match_communities.set([active_package_listing.community])
4340
event = active_package_listing.build_audit_event(
41+
target=target,
4442
action=action,
4543
user_id=user.pk,
4644
message=message,

0 commit comments

Comments
 (0)