mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2026-01-02 01:27:03 -05:00
feat: Implement initial schema and add various API, service, and management command enhancements across the application.
This commit is contained in:
@@ -48,12 +48,10 @@ def handle_submission_claimed(instance, source, target, user, context=None, **kw
|
||||
user: The user who claimed.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'CLAIMED':
|
||||
if target != "CLAIMED":
|
||||
return
|
||||
|
||||
logger.info(
|
||||
f"Submission {instance.pk} claimed by {user.username if user else 'system'}"
|
||||
)
|
||||
logger.info(f"Submission {instance.pk} claimed by {user.username if user else 'system'}")
|
||||
|
||||
# Broadcast for real-time dashboard updates
|
||||
_broadcast_submission_status_change(instance, source, target, user)
|
||||
@@ -72,12 +70,10 @@ def handle_submission_unclaimed(instance, source, target, user, context=None, **
|
||||
user: The user who unclaimed.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if source != 'CLAIMED' or target != 'PENDING':
|
||||
if source != "CLAIMED" or target != "PENDING":
|
||||
return
|
||||
|
||||
logger.info(
|
||||
f"Submission {instance.pk} unclaimed by {user.username if user else 'system'}"
|
||||
)
|
||||
logger.info(f"Submission {instance.pk} unclaimed by {user.username if user else 'system'}")
|
||||
|
||||
# Broadcast for real-time dashboard updates
|
||||
_broadcast_submission_status_change(instance, source, target, user)
|
||||
@@ -96,25 +92,21 @@ def handle_submission_approved(instance, source, target, user, context=None, **k
|
||||
user: The user who approved.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'APPROVED':
|
||||
if target != "APPROVED":
|
||||
return
|
||||
|
||||
logger.info(
|
||||
f"Submission {instance.pk} approved by {user if user else 'system'}"
|
||||
)
|
||||
logger.info(f"Submission {instance.pk} approved by {user if user else 'system'}")
|
||||
|
||||
# Trigger notification (handled by NotificationCallback)
|
||||
# Invalidate cache (handled by CacheInvalidationCallback)
|
||||
|
||||
# Apply the submission changes if applicable
|
||||
if hasattr(instance, 'apply_changes'):
|
||||
if hasattr(instance, "apply_changes"):
|
||||
try:
|
||||
instance.apply_changes()
|
||||
logger.info(f"Applied changes for submission {instance.pk}")
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Failed to apply changes for submission {instance.pk}: {e}"
|
||||
)
|
||||
logger.exception(f"Failed to apply changes for submission {instance.pk}: {e}")
|
||||
|
||||
|
||||
def handle_submission_rejected(instance, source, target, user, context=None, **kwargs):
|
||||
@@ -130,13 +122,12 @@ def handle_submission_rejected(instance, source, target, user, context=None, **k
|
||||
user: The user who rejected.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'REJECTED':
|
||||
if target != "REJECTED":
|
||||
return
|
||||
|
||||
reason = context.extra_data.get('reason', '') if context else ''
|
||||
reason = context.extra_data.get("reason", "") if context else ""
|
||||
logger.info(
|
||||
f"Submission {instance.pk} rejected by {user if user else 'system'}"
|
||||
f"{f': {reason}' if reason else ''}"
|
||||
f"Submission {instance.pk} rejected by {user if user else 'system'}" f"{f': {reason}' if reason else ''}"
|
||||
)
|
||||
|
||||
|
||||
@@ -153,13 +144,12 @@ def handle_submission_escalated(instance, source, target, user, context=None, **
|
||||
user: The user who escalated.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'ESCALATED':
|
||||
if target != "ESCALATED":
|
||||
return
|
||||
|
||||
reason = context.extra_data.get('reason', '') if context else ''
|
||||
reason = context.extra_data.get("reason", "") if context else ""
|
||||
logger.info(
|
||||
f"Submission {instance.pk} escalated by {user if user else 'system'}"
|
||||
f"{f': {reason}' if reason else ''}"
|
||||
f"Submission {instance.pk} escalated by {user if user else 'system'}" f"{f': {reason}' if reason else ''}"
|
||||
)
|
||||
|
||||
# Create escalation task if task system is available
|
||||
@@ -179,15 +169,13 @@ def handle_report_resolved(instance, source, target, user, context=None, **kwarg
|
||||
user: The user who resolved.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'RESOLVED':
|
||||
if target != "RESOLVED":
|
||||
return
|
||||
|
||||
logger.info(
|
||||
f"ModerationReport {instance.pk} resolved by {user if user else 'system'}"
|
||||
)
|
||||
logger.info(f"ModerationReport {instance.pk} resolved by {user if user else 'system'}")
|
||||
|
||||
# Update related queue items
|
||||
_update_related_queue_items(instance, 'COMPLETED')
|
||||
_update_related_queue_items(instance, "COMPLETED")
|
||||
|
||||
|
||||
def handle_queue_completed(instance, source, target, user, context=None, **kwargs):
|
||||
@@ -203,12 +191,10 @@ def handle_queue_completed(instance, source, target, user, context=None, **kwarg
|
||||
user: The user who completed.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
if target != 'COMPLETED':
|
||||
if target != "COMPLETED":
|
||||
return
|
||||
|
||||
logger.info(
|
||||
f"ModerationQueue {instance.pk} completed by {user if user else 'system'}"
|
||||
)
|
||||
logger.info(f"ModerationQueue {instance.pk} completed by {user if user else 'system'}")
|
||||
|
||||
# Update moderation statistics
|
||||
_update_moderation_stats(instance, user)
|
||||
@@ -227,18 +213,17 @@ def handle_bulk_operation_status(instance, source, target, user, context=None, *
|
||||
user: The user who initiated the change.
|
||||
context: Optional TransitionContext.
|
||||
"""
|
||||
logger.info(
|
||||
f"BulkOperation {instance.pk} transitioned: {source} → {target}"
|
||||
)
|
||||
logger.info(f"BulkOperation {instance.pk} transitioned: {source} → {target}")
|
||||
|
||||
if target == 'COMPLETED':
|
||||
if target == "COMPLETED":
|
||||
_finalize_bulk_operation(instance, success=True)
|
||||
elif target == 'FAILED':
|
||||
elif target == "FAILED":
|
||||
_finalize_bulk_operation(instance, success=False)
|
||||
|
||||
|
||||
# Helper functions
|
||||
|
||||
|
||||
def _create_escalation_task(instance, user, reason):
|
||||
"""Create an escalation task for admin review."""
|
||||
try:
|
||||
@@ -247,7 +232,7 @@ def _create_escalation_task(instance, user, reason):
|
||||
# Create a queue item for the escalated submission
|
||||
ModerationQueue.objects.create(
|
||||
content_object=instance,
|
||||
priority='HIGH',
|
||||
priority="HIGH",
|
||||
reason=f"Escalated: {reason}" if reason else "Escalated for review",
|
||||
created_by=user,
|
||||
)
|
||||
@@ -287,10 +272,10 @@ def _update_moderation_stats(instance, user):
|
||||
|
||||
try:
|
||||
# Update user's moderation count if they have a profile
|
||||
profile = getattr(user, 'profile', None)
|
||||
if profile and hasattr(profile, 'moderation_count'):
|
||||
profile = getattr(user, "profile", None)
|
||||
if profile and hasattr(profile, "moderation_count"):
|
||||
profile.moderation_count += 1
|
||||
profile.save(update_fields=['moderation_count'])
|
||||
profile.save(update_fields=["moderation_count"])
|
||||
logger.debug(f"Updated moderation count for {user}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to update moderation stats: {e}")
|
||||
@@ -302,7 +287,7 @@ def _finalize_bulk_operation(instance, success):
|
||||
from django.utils import timezone
|
||||
|
||||
instance.completed_at = timezone.now()
|
||||
instance.save(update_fields=['completed_at'])
|
||||
instance.save(update_fields=["completed_at"])
|
||||
|
||||
if success:
|
||||
logger.info(
|
||||
@@ -312,8 +297,7 @@ def _finalize_bulk_operation(instance, success):
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
f"BulkOperation {instance.pk} failed: "
|
||||
f"{getattr(instance, 'error_message', 'Unknown error')}"
|
||||
f"BulkOperation {instance.pk} failed: " f"{getattr(instance, 'error_message', 'Unknown error')}"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to finalize bulk operation: {e}")
|
||||
@@ -355,9 +339,9 @@ def _broadcast_submission_status_change(instance, source, target, user):
|
||||
}
|
||||
|
||||
# Add claim information if available
|
||||
if hasattr(instance, 'claimed_by') and instance.claimed_by:
|
||||
if hasattr(instance, "claimed_by") and instance.claimed_by:
|
||||
payload["locked_by"] = instance.claimed_by.username
|
||||
if hasattr(instance, 'claimed_at') and instance.claimed_at:
|
||||
if hasattr(instance, "claimed_at") and instance.claimed_at:
|
||||
payload["locked_at"] = instance.claimed_at.isoformat()
|
||||
|
||||
# Emit the signal for downstream notification handlers
|
||||
@@ -371,16 +355,14 @@ def _broadcast_submission_status_change(instance, source, target, user):
|
||||
payload=payload,
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
f"Broadcast status change: {submission_type}#{instance.pk} "
|
||||
f"{source} -> {target}"
|
||||
)
|
||||
logger.debug(f"Broadcast status change: {submission_type}#{instance.pk} " f"{source} -> {target}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to broadcast submission status change: {e}")
|
||||
|
||||
|
||||
# Signal handler registration
|
||||
|
||||
|
||||
def register_moderation_signal_handlers():
|
||||
"""
|
||||
Register all moderation signal handlers.
|
||||
@@ -399,70 +381,31 @@ def register_moderation_signal_handlers():
|
||||
)
|
||||
|
||||
# EditSubmission handlers
|
||||
register_transition_handler(
|
||||
EditSubmission, '*', 'APPROVED',
|
||||
handle_submission_approved, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
EditSubmission, '*', 'REJECTED',
|
||||
handle_submission_rejected, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
EditSubmission, '*', 'ESCALATED',
|
||||
handle_submission_escalated, stage='post'
|
||||
)
|
||||
register_transition_handler(EditSubmission, "*", "APPROVED", handle_submission_approved, stage="post")
|
||||
register_transition_handler(EditSubmission, "*", "REJECTED", handle_submission_rejected, stage="post")
|
||||
register_transition_handler(EditSubmission, "*", "ESCALATED", handle_submission_escalated, stage="post")
|
||||
|
||||
# PhotoSubmission handlers
|
||||
register_transition_handler(
|
||||
PhotoSubmission, '*', 'APPROVED',
|
||||
handle_submission_approved, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
PhotoSubmission, '*', 'REJECTED',
|
||||
handle_submission_rejected, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
PhotoSubmission, '*', 'ESCALATED',
|
||||
handle_submission_escalated, stage='post'
|
||||
)
|
||||
register_transition_handler(PhotoSubmission, "*", "APPROVED", handle_submission_approved, stage="post")
|
||||
register_transition_handler(PhotoSubmission, "*", "REJECTED", handle_submission_rejected, stage="post")
|
||||
register_transition_handler(PhotoSubmission, "*", "ESCALATED", handle_submission_escalated, stage="post")
|
||||
|
||||
# ModerationReport handlers
|
||||
register_transition_handler(
|
||||
ModerationReport, '*', 'RESOLVED',
|
||||
handle_report_resolved, stage='post'
|
||||
)
|
||||
register_transition_handler(ModerationReport, "*", "RESOLVED", handle_report_resolved, stage="post")
|
||||
|
||||
# ModerationQueue handlers
|
||||
register_transition_handler(
|
||||
ModerationQueue, '*', 'COMPLETED',
|
||||
handle_queue_completed, stage='post'
|
||||
)
|
||||
register_transition_handler(ModerationQueue, "*", "COMPLETED", handle_queue_completed, stage="post")
|
||||
|
||||
# BulkOperation handlers
|
||||
register_transition_handler(
|
||||
BulkOperation, '*', '*',
|
||||
handle_bulk_operation_status, stage='post'
|
||||
)
|
||||
register_transition_handler(BulkOperation, "*", "*", handle_bulk_operation_status, stage="post")
|
||||
|
||||
# Claim/Unclaim handlers for EditSubmission
|
||||
register_transition_handler(
|
||||
EditSubmission, 'PENDING', 'CLAIMED',
|
||||
handle_submission_claimed, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
EditSubmission, 'CLAIMED', 'PENDING',
|
||||
handle_submission_unclaimed, stage='post'
|
||||
)
|
||||
register_transition_handler(EditSubmission, "PENDING", "CLAIMED", handle_submission_claimed, stage="post")
|
||||
register_transition_handler(EditSubmission, "CLAIMED", "PENDING", handle_submission_unclaimed, stage="post")
|
||||
|
||||
# Claim/Unclaim handlers for PhotoSubmission
|
||||
register_transition_handler(
|
||||
PhotoSubmission, 'PENDING', 'CLAIMED',
|
||||
handle_submission_claimed, stage='post'
|
||||
)
|
||||
register_transition_handler(
|
||||
PhotoSubmission, 'CLAIMED', 'PENDING',
|
||||
handle_submission_unclaimed, stage='post'
|
||||
)
|
||||
register_transition_handler(PhotoSubmission, "PENDING", "CLAIMED", handle_submission_claimed, stage="post")
|
||||
register_transition_handler(PhotoSubmission, "CLAIMED", "PENDING", handle_submission_unclaimed, stage="post")
|
||||
|
||||
logger.info("Registered moderation signal handlers")
|
||||
|
||||
@@ -471,14 +414,14 @@ def register_moderation_signal_handlers():
|
||||
|
||||
|
||||
__all__ = [
|
||||
'submission_status_changed',
|
||||
'register_moderation_signal_handlers',
|
||||
'handle_submission_approved',
|
||||
'handle_submission_rejected',
|
||||
'handle_submission_escalated',
|
||||
'handle_submission_claimed',
|
||||
'handle_submission_unclaimed',
|
||||
'handle_report_resolved',
|
||||
'handle_queue_completed',
|
||||
'handle_bulk_operation_status',
|
||||
"submission_status_changed",
|
||||
"register_moderation_signal_handlers",
|
||||
"handle_submission_approved",
|
||||
"handle_submission_rejected",
|
||||
"handle_submission_escalated",
|
||||
"handle_submission_claimed",
|
||||
"handle_submission_unclaimed",
|
||||
"handle_report_resolved",
|
||||
"handle_queue_completed",
|
||||
"handle_bulk_operation_status",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user