update APIs

This commit is contained in:
2025-09-04 19:25:14 +09:00
parent 32f860af41
commit e0543e2b4e
8 changed files with 759 additions and 64 deletions

View File

@ -49,6 +49,10 @@ def submit_qr_points(request):
longitude = request.data.get('longitude')
image_data = request.data.get('image')
cp_number = request.data.get('cp_number')
point_value = request.data.get('point_value')
location_id = request.data.get('location_id')
timestamp = request.data.get('timestamp')
qr_scan = request.data.get('qr_scan')
# 📋 パラメータをログ出力(デバッグ用)
logger.info(f"[QR_SUBMIT] ID: {request_id} - 📋 Request Parameters:")
@ -58,7 +62,11 @@ def submit_qr_points(request):
logger.info(f"[QR_SUBMIT] ID: {request_id} - 📍 Latitude: {latitude}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 🌍 Longitude: {longitude}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 🏁 CP Number: {cp_number}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 📸 Has Image: {image_data is not None}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - <EFBFBD> Point Value: {point_value}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 📍 Location ID: {location_id}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - ⏰ Timestamp: {timestamp}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 📱 QR Scan: {qr_scan}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - <20>📸 Has Image: {image_data is not None}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 🌐 Client IP: {client_ip}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 👤 User: {user_info}")
logger.info(f"[QR_SUBMIT] ID: {request_id} - 🔧 User Agent: {user_agent[:100]}...")
@ -89,12 +97,21 @@ def submit_qr_points(request):
except Exception as e:
logger.warning(f"[QR_SUBMIT] Failed to log request data: {e}")
# パラメータ検証
if not all([event_code, team_name, qr_code_data]):
logger.warning(f"[QR_SUBMIT] ❌ Missing required parameters - ID: {request_id}")
# パラメータ検証 - 実際のリクエストデータに基づく
# 基本的な必須パラメータ: event_code, team_name
# オプション: qr_code_data, cp_number, point_value, location_id, qr_scan
if not all([event_code, team_name]):
missing_params = []
if not event_code:
missing_params.append('event_code')
if not team_name:
missing_params.append('team_name')
logger.warning(f"[QR_SUBMIT] ❌ Missing required parameters: {missing_params} - ID: {request_id}")
return Response({
"status": "ERROR",
"message": "イベントコード、チーム名、QRコードデータが必要です",
"message": f"必須パラメータが不足しています: {', '.join(missing_params)}",
"missing_parameters": missing_params,
"request_id": request_id
}, status=status.HTTP_400_BAD_REQUEST)
@ -124,51 +141,85 @@ def submit_qr_points(request):
"request_id": request_id
}, status=status.HTTP_404_NOT_FOUND)
# QRコードデータの解析
try:
if isinstance(qr_code_data, str):
# JSON文字列の場合はパース
if qr_code_data.startswith('{'):
qr_data = json.loads(qr_code_data)
# QRコードデータの解析(オプション)
qr_data = None
if qr_code_data:
try:
if isinstance(qr_code_data, str):
# JSON文字列の場合はパース
if qr_code_data.startswith('{'):
qr_data = json.loads(qr_code_data)
else:
# 単純な文字列の場合
qr_data = {"code": qr_code_data}
else:
# 単純な文字列の場合
qr_data = {"code": qr_code_data}
else:
qr_data = qr_code_data
logger.info(f"[QR_SUBMIT] 📱 Parsed QR data: {qr_data} - ID: {request_id}")
except json.JSONDecodeError as e:
logger.warning(f"[QR_SUBMIT] ❌ Invalid QR code data format: {e} - ID: {request_id}")
return Response({
"status": "ERROR",
"message": "QRコードデータの形式が正しくありません",
"request_id": request_id
}, status=status.HTTP_400_BAD_REQUEST)
qr_data = qr_code_data
logger.info(f"[QR_SUBMIT] 📱 Parsed QR data: {qr_data} - ID: {request_id}")
except json.JSONDecodeError as e:
logger.warning(f"[QR_SUBMIT] ⚠️ Invalid QR code data format: {e}, using as string - ID: {request_id}")
qr_data = {"code": str(qr_code_data)}
# チェックポイント情報の取得cp_numberが指定されている場合
# チェックポイント情報の取得
location = None
if cp_number:
calculated_point_value = 0
# location_idが指定されている場合、それを優先
if location_id:
location = Location2025.objects.filter(
id=location_id,
event_id=event.id
).first()
if location:
calculated_point_value = location.cp_point or 0
logger.info(f"[QR_SUBMIT] 📍 Found location by ID: {location_id} - CP{location.cp_number} - {location.cp_name} - Points: {calculated_point_value} - ID: {request_id}")
else:
logger.warning(f"[QR_SUBMIT] ⚠️ Location not found for location_id: {location_id} - ID: {request_id}")
# cp_numberが指定されている場合も確認
elif cp_number:
location = Location2025.objects.filter(
event_id=event.id,
cp_number=cp_number
).first()
if location:
logger.info(f"[QR_SUBMIT] 📍 Found location: CP{cp_number} - {location.cp_name} - ID: {request_id}")
calculated_point_value = location.cp_point or 0
logger.info(f"[QR_SUBMIT] 📍 Found location by CP: CP{cp_number} - {location.cp_name} - Points: {calculated_point_value} - ID: {request_id}")
else:
logger.warning(f"[QR_SUBMIT] ⚠️ Location not found for CP{cp_number} - ID: {request_id}")
# point_valueが明示的に指定されている場合、それを使用
final_point_value = point_value if point_value is not None else calculated_point_value
logger.info(f"[QR_SUBMIT] 💯 Point calculation: provided={point_value}, calculated={calculated_point_value}, final={final_point_value} - ID: {request_id}")
# QRポイント登録処理
current_time = timezone.now()
# GpsCheckinレコードを作成QRコード情報を含む
# timestampが提供されている場合は使用、そうでなければ現在時刻
if timestamp:
try:
# ISO形式のタイムスタンプをパース
checkin_time = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
if checkin_time.tzinfo is None:
checkin_time = timezone.make_aware(checkin_time)
logger.info(f"[QR_SUBMIT] ⏰ Using provided timestamp: {checkin_time} - ID: {request_id}")
except (ValueError, AttributeError) as e:
logger.warning(f"[QR_SUBMIT] ⚠️ Invalid timestamp format, using current time: {e} - ID: {request_id}")
checkin_time = current_time
else:
checkin_time = current_time
# GpsCheckinレコードを作成
checkin_data = {
'event': event,
'entry': entry,
'zekken_number': entry.zekken_number,
'cp_number': cp_number or 0, # cp_numberが指定されていない場合は0
'checkin_time': current_time,
'cp_number': cp_number or (location.cp_number if location else 0),
'checkin_time': checkin_time,
'is_service_checked': True, # QRコードはサービスポイントとして扱う
}
@ -178,9 +229,17 @@ def submit_qr_points(request):
checkin_data['longitude'] = float(longitude)
logger.info(f"[QR_SUBMIT] 🌍 GPS coordinates recorded: {latitude}, {longitude} - ID: {request_id}")
# QRコードデータを格納JSONフィールドがある場合
# QRコードデータを格納qr_scanフラグも含む
qr_metadata = {
'qr_scan': qr_scan,
'location_id': location_id,
'point_value': final_point_value,
'original_qr_data': qr_data,
'timestamp': timestamp
}
if hasattr(GpsCheckin, 'qr_code_data'):
checkin_data['qr_code_data'] = qr_data
checkin_data['qr_code_data'] = qr_metadata
# 画像データを格納URLまたはパスの場合
if image_data:
@ -191,11 +250,6 @@ def submit_qr_points(request):
# レコードを作成
gps_checkin = GpsCheckin.objects.create(**checkin_data)
# ポイント計算
point_value = 0
if location:
point_value = location.cp_point or 0
# 成功レスポンス
response_data = {
"status": "OK",
@ -205,9 +259,11 @@ def submit_qr_points(request):
"event_code": event_code,
"team_name": team_name,
"zekken_number": entry.zekken_number,
"cp_number": cp_number,
"point_value": point_value,
"checkin_time": current_time.isoformat(),
"cp_number": cp_number or (location.cp_number if location else 0),
"point_value": final_point_value,
"location_id": location_id,
"checkin_time": checkin_time.isoformat(),
"qr_scan": qr_scan,
"qr_code_processed": True,
"has_location": bool(latitude and longitude),
"has_image": bool(image_data),
@ -215,7 +271,7 @@ def submit_qr_points(request):
}
}
logger.info(f"[QR_SUBMIT] ✅ SUCCESS - Team: {team_name}, Zekken: {entry.zekken_number}, CP: {cp_number}, Points: {point_value}, QR: {bool(qr_code_data)}, Client IP: {client_ip}, User: {user_info} - ID: {request_id}")
logger.info(f"[QR_SUBMIT] ✅ SUCCESS - Team: {team_name}, Zekken: {entry.zekken_number}, CP: {cp_number or (location.cp_number if location else 0)}, Points: {final_point_value}, QR: {qr_scan}, Location ID: {location_id}, Client IP: {client_ip}, User: {user_info} - ID: {request_id}")
return Response(response_data, status=status.HTTP_200_OK)