From 42f6471f73e0dcf979f238a9849af3fe12a592c0 Mon Sep 17 00:00:00 2001 From: Akira Date: Fri, 29 Aug 2025 20:05:34 +0900 Subject: [PATCH] Fix GpsCheckin --- rog/models.py | 11 +++----- rog/views.py | 70 +++++++++++++++++++++++++++------------------------ 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/rog/models.py b/rog/models.py index fee09fb..e4679d8 100755 --- a/rog/models.py +++ b/rog/models.py @@ -927,6 +927,9 @@ class GpsCheckin(models.Model): event_id = models.BigIntegerField(null=True, blank=True) team_id = models.BigIntegerField(null=True, blank=True) checkpoint_id = models.BigIntegerField(null=True, blank=True) + # データベースの実際のフィールドと一致させる + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = 'rog_gpscheckin' @@ -935,14 +938,6 @@ class GpsCheckin(models.Model): def __str__(self): return f"GPS Checkin {self.id} - {self.zekken}" - def save(self, *args, **kwargs): - # 作成時・更新時のタイムスタンプを自動設定 - from django.utils import timezone - if not self.create_at: - self.create_at = timezone.now() - self.update_at = timezone.now() - super().save(*args, **kwargs) - class RogUser(models.Model): user=models.OneToOneField(CustomUser, on_delete=models.CASCADE) phone=models.CharField(_('Phone Number'), max_length=55) diff --git a/rog/views.py b/rog/views.py index 318a67b..487bf44 100755 --- a/rog/views.py +++ b/rog/views.py @@ -2659,49 +2659,53 @@ def get_checkins(request, *args, **kwargs): logger.debug(f"=== event_code is required.") return Response({"error": "event_code is required"}, status=400) + logger.debug(f"=== Searching for zekken={zekken_number}, event_code={event_code}") + # チェックインデータの取得とCP情報の結合 checkins = GpsCheckin.objects.filter( zekken=zekken_number, event_code=event_code ).order_by('serial_number') + + logger.debug(f"=== Found {checkins.count()} checkins") data = [] for c in checkins: location = Location.objects.filter(cp=c.cp_number,group=event_code).first() - if location: - formatted_time = None - if c.create_at: - try: - # create_atが文字列の場合はdatetimeに変換 - if isinstance(c.create_at, str): - c.create_at = datetime.strptime(c.create_at, '%Y-%m-%d %H:%M:%S') - - # JST(+9時間)に変換して時刻フォーマット - jst_time = c.create_at + timedelta(hours=9) - formatted_time = jst_time.strftime('%H:%M:%S') - - except (ValueError, TypeError, AttributeError) as e: - logger.error(f"Error formatting create_at for checkin {c.id}: {str(e)}") - logger.error(f"Raw create_at value: {c.create_at}") - formatted_time = None + + formatted_time = None + if c.checkin_time: + try: + # checkin_timeが文字列の場合はdatetimeに変換 + if isinstance(c.checkin_time, str): + c.checkin_time = datetime.strptime(c.checkin_time, '%Y-%m-%d %H:%M:%S') + + # JST(+9時間)に変換して時刻フォーマット + jst_time = c.checkin_time + timedelta(hours=9) + formatted_time = jst_time.strftime('%H:%M:%S') + + except (ValueError, TypeError, AttributeError) as e: + logger.error(f"Error formatting checkin_time for checkin {c.id}: {str(e)}") + logger.error(f"Raw checkin_time value: {c.checkin_time}") + formatted_time = None - data.append({ - 'id': c.id, - 'path_order': c.serial_number, - 'cp_number': c.cp_number, - 'sub_loc_id': location.sub_loc_id if location else f"#{c.cp_number}", - 'location_name': location.location_name if location else None, - 'create_at': formatted_time, #(c.create_at + timedelta(hours=9)).strftime('%H:%M:%S') if c.create_at else None, - 'validate_location': c.validate_location, - 'points': c.points or 0, - 'buy_flag': c.buy_flag, - 'photos': location.photos if location else None, - 'image_address': get_image_url(c.image_address), - 'receipt_address': get_image_url(c.image_receipt), - 'location_name': location.location_name if location else None, - 'checkin_point': location.checkin_point if location else None, - 'buy_point': location.buy_point - }) + data.append({ + 'id': c.id, + 'path_order': c.serial_number, + 'cp_number': c.cp_number, + 'sub_loc_id': location.sub_loc_id if location else f"#{c.cp_number}", + 'location_name': location.location_name if location else None, + 'create_at': formatted_time, #(c.checkin_time + timedelta(hours=9)).strftime('%H:%M:%S') if c.checkin_time else None, + 'validate_location': True, # デフォルト値として設定 + 'points': location.checkin_point if location else 0, # locationから取得 + 'buy_flag': False, # デフォルト値として設定 + 'photos': location.photos if location else None, + 'image_address': None, # 現在のモデルには存在しない + 'receipt_address': None, # 現在のモデルには存在しない + 'location_name': location.location_name if location else None, + 'checkin_point': location.checkin_point if location else None, + 'buy_point': location.buy_point if location else 0 + }) #logger.debug(f"data={data}") return Response(data)