48 lines
2.1 KiB
Python
48 lines
2.1 KiB
Python
from app.utils import send_sync_channel_message
|
|
from .utils import transmission_handler
|
|
from .models import Torrent, SharedUser
|
|
|
|
|
|
def on_post_save_torrent(instance: Torrent, created, **kwargs):
|
|
if created:
|
|
send_sync_channel_message(f"user_{instance.user_id}", "add_torrent", instance.id)
|
|
|
|
|
|
def on_pre_delete_torrent(instance: Torrent, **kwargs):
|
|
transmission_handler.delete(instance.id)
|
|
for user_id in instance.related_users:
|
|
send_sync_channel_message(f"user_{user_id}", "remove_torrent", instance.id)
|
|
|
|
|
|
def on_shared_user_changed(sender, instance: Torrent, action, pk_set, **kwargs):
|
|
# print("on_share_user_changed", sender, instance, action, pk_set)
|
|
# on_share_user_changed <class 'torrent.models.SharedUser'> Torrent object (a9164e99d5181cfef0c23c209334103619080908) pre_add {3}
|
|
# on_share_user_changed <class 'torrent.models.SharedUser'> Torrent object (a9164e99d5181cfef0c23c209334103619080908) post_add {3}
|
|
match action:
|
|
case "pre_add":
|
|
pass
|
|
case "post_add":
|
|
for user_id in pk_set:
|
|
send_sync_channel_message(f"user_{user_id}", "add_torrent", instance.id)
|
|
for user_id in instance.related_users:
|
|
send_sync_channel_message(f"user_{user_id}", "update_torrent", {
|
|
"torrent_id": instance.id,
|
|
"updated_fields": {"shared_users": list(instance.shared_users.all().values_list("id", flat=True))}
|
|
})
|
|
case "pre_remove":
|
|
pass
|
|
case "post_remove":
|
|
for user_id in pk_set:
|
|
send_sync_channel_message(f"user_{user_id}", "remove_torrent", instance.id)
|
|
for user_id in instance.related_users:
|
|
send_sync_channel_message(f"user_{user_id}", "update_torrent", {
|
|
"torrent_id": instance.id,
|
|
"updated_fields": {"shared_users": list(instance.shared_users.all().values_list("id", flat=True))}
|
|
})
|
|
case "pre_clear":
|
|
pass
|
|
case "post_clear":
|
|
pass
|
|
case _:
|
|
pass
|