Fix entry registration

This commit is contained in:
2025-09-06 00:11:57 +09:00
parent 86ea3a4b0c
commit 99e4561694
6 changed files with 382 additions and 4 deletions

View File

@ -9,6 +9,7 @@ from django.contrib.gis.db import models
from django.contrib.postgres.fields import ArrayField
from django.utils import timezone
from datetime import timedelta
from django.conf import settings
try:
from django.db.models import JSONField
except ImportError:
@ -2006,6 +2007,27 @@ class GpsLog(models.Model):
# ゴール記録用に追加
score = models.IntegerField(default=0, null=True, blank=True)
scoreboard_url = models.URLField(blank=True, null=True)
# 外部システム登録管理用フィールド
external_registration_status = models.CharField(
max_length=20,
choices=[
('pending', 'Pending'),
('success', 'Success'),
('failed', 'Failed'),
('retry', 'Retry Required')
],
default='pending',
help_text="外部システム登録状況"
)
external_registration_attempts = models.IntegerField(default=0, help_text="外部システム登録試行回数")
external_registration_error = models.TextField(null=True, blank=True, help_text="外部システム登録エラー詳細")
last_external_registration_attempt = models.DateTimeField(null=True, blank=True, help_text="最後の外部システム登録試行時刻")
# エントリー関連情報(外部システム登録用)
team_name = models.CharField(max_length=255, null=True, blank=True, help_text="チーム名(外部システム登録用)")
category_name = models.CharField(max_length=255, null=True, blank=True, help_text="カテゴリ名(外部システム登録用)")
user_password_hash = models.TextField(null=True, blank=True, help_text="ユーザーパスワードハッシュ(外部システム登録用)")
class Meta:
db_table = 'gps_information'
@ -2084,6 +2106,102 @@ class GpsLog(models.Model):
return self.create_at
return None
@classmethod
def record_external_registration_request(cls, entry):
"""
外部システム登録要求を記録する
Entry作成時に外部システム登録が失敗した場合の情報を保存
"""
return cls.objects.create(
serial_number=-1, # 外部システム登録要求を表す特別な値
zekken_number=str(entry.zekken_number),
event_code=entry.event.event_name,
cp_number="EXTERNAL_REG",
team_name=entry.team.team_name,
category_name=entry.category.category_name,
user_password_hash=entry.team.owner.password,
external_registration_status='pending',
external_registration_attempts=0,
create_at=timezone.now(),
update_at=timezone.now(),
buy_flag=False,
colabo_company_memo="Entry registration - external system pending"
)
def update_external_registration_status(self, status, error_message=None):
"""
外部システム登録状況を更新する
"""
self.external_registration_status = status
self.external_registration_attempts += 1
self.last_external_registration_attempt = timezone.now()
if error_message:
self.external_registration_error = error_message
self.update_at = timezone.now()
self.save()
def retry_external_registration(self):
"""
外部システム登録をリトライする
"""
import requests
api_url = f"{settings.FRONTEND_URL}/gifuroge/register_team"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"zekken_number": self.zekken_number,
"event_code": self.event_code,
"team_name": self.team_name,
"class_name": self.category_name,
"password": self.user_password_hash
}
try:
response = requests.post(api_url, headers=headers, data=data, timeout=30)
response.raise_for_status()
self.update_external_registration_status('success')
return True
except requests.RequestException as e:
self.update_external_registration_status('failed', str(e))
return False
@classmethod
def get_pending_external_registrations(cls):
"""
外部システム登録が保留中の記録を取得する
"""
return cls.objects.filter(
serial_number=-1,
cp_number="EXTERNAL_REG",
external_registration_status__in=['pending', 'retry']
).order_by('create_at')
@classmethod
def retry_failed_external_registrations(cls, max_attempts=3):
"""
失敗した外部システム登録を一括でリトライする
"""
pending_records = cls.get_pending_external_registrations().filter(
external_registration_attempts__lt=max_attempts
)
success_count = 0
failed_count = 0
for record in pending_records:
if record.retry_external_registration():
success_count += 1
else:
failed_count += 1
return {
'success_count': success_count,
'failed_count': failed_count,
'total_processed': success_count + failed_count
}
class Waypoint(models.Model):