daren_project/user_management/management/commands/publish_video.py

49 lines
1.9 KiB
Python
Raw Permalink Normal View History

2025-04-29 10:22:57 +08:00
from django.core.management.base import BaseCommand
from user_management.models import Video
from user_management.tasks import publish_scheduled_video
class Command(BaseCommand):
help = '手动发布视频'
def add_arguments(self, parser):
parser.add_argument('video_id', type=int, help='视频ID')
def handle(self, *args, **options):
video_id = options['video_id']
try:
# 获取视频对象
video = Video.objects.get(id=video_id)
except Video.DoesNotExist:
self.stderr.write(self.style.ERROR(f'错误: 未找到ID为{video_id}的视频'))
return
# 检查视频状态是否允许发布
if video.status not in ['draft', 'scheduled']:
self.stderr.write(self.style.ERROR(
f'错误: 当前视频状态为 {video.get_status_display()},无法发布'
))
return
self.stdout.write(f'开始发布视频 "{video.title}" (ID: {video.id})...')
# 执行发布任务
try:
result = publish_scheduled_video(video.id)
if isinstance(result, dict) and result.get('success', False):
self.stdout.write(self.style.SUCCESS(
f'视频发布成功!\n'
f'标题: {video.title}\n'
f'平台: {video.platform_account.get_platform_name_display()}\n'
f'账号: {video.platform_account.account_name}\n'
f'视频链接: {result.get("video_url")}\n'
f'发布时间: {result.get("publish_time")}'
))
else:
self.stderr.write(self.style.ERROR(
f'发布失败: {result.get("error", "未知错误")}'
))
except Exception as e:
self.stderr.write(self.style.ERROR(f'发布过程中出错: {str(e)}'))