from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from ai.models import * import datetime import json from ai.utils.show_database import GetData from ai.utils.show_process import show_main_process from django_redis import get_redis_connection # Create your views here. # 手动触发ai排片 @csrf_exempt def manual_general_show(request): result = show_main_process() result_dict = { 'status': 'success' if result else 'fail', 'message': '生成成功' if result else '生成失败', } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) # 更新指定日期的排片 @csrf_exempt def get_cinema_show_result(request): zz_code = request.GET.dict().get('cinema_code') show_date = request.GET.dict().get('show_date') print(zz_code, show_date) cinema = TestCinema.objects.filter(zz_code=zz_code).first() start = datetime.datetime.strftime(datetime.datetime.strptime(show_date, '%Y-%m-%d') + datetime.timedelta(hours=6), '%Y-%m-%d %H:%M:%S') end = datetime.datetime.strftime( datetime.datetime.strptime(show_date, '%Y-%m-%d') + datetime.timedelta(hours=29, minutes=59, seconds=59), '%Y-%m-%d %H:%M:%S') show = '' income = '' if cinema: print(cinema) data = GetData(json.loads(cinema.db_config)) show = data.get_show_data(start, end) income = data.get_total_income(start, end) print(show, income) try: AiShow.objects.create( cinema=cinema.name, zz_code=cinema.zz_code, show_date=show_date, is_ai_show=False, show=show, sales=income, prompt='', result='', message='', take_time=0, take_tokens='0' ) except Exception as e: print(e) result_dict = { 'status': 'success', 'message': '生成成功', 'show': show, 'income': income, } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) # 清除redis锁 @csrf_exempt def clear_lock(request): redis_conn = get_redis_connection() redis_key = f'ai_show{datetime.date.today().strftime("%Y%m%d")}' if redis_conn.exists(redis_key): redis_conn.delete(redis_key) result_dict = { 'status': 'success', 'message': '完成', } return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})