Skip to content

Commit

Permalink
feat(schemas): enhance timezone adjustment for created_at field and r…
Browse files Browse the repository at this point in the history
…efactor UserSchema and WebhookSchema
  • Loading branch information
fcoagz committed Nov 25, 2024
1 parent 13d9be3 commit 0f1644a
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/data/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def _adjust_timezone_not_object(self, data):
if 'last_update' in data and data['last_update']:
last_update_str = data.get('last_update')
if last_update_str:
last_update = datetime.fromisoformat(data['last_update']).astimezone(TIME_ZONE)
if self.custom_format == 'iso':
data['last_update'] = last_update.isoformat()
Expand All @@ -24,6 +25,10 @@ def _adjust_timezone_object(self, data):
if hasattr(data, 'last_update') and data.last_update:
last_update = data.last_update.astimezone(TIME_ZONE)
data.last_update = last_update.isoformat()

if hasattr(data, 'created_at') and data.created_at:
created_at = data.created_at.astimezone(TIME_ZONE)
data.created_at = created_at.isoformat()
return data

@pre_dump
Expand All @@ -32,15 +37,7 @@ def adjust_timezone(self, data, **kwargs):
return self._adjust_timezone_not_object(data)
return self._adjust_timezone_object(data)

class UserSchema(ma.Schema):
created_at = fields.DateTime(format='%d/%m/%Y, %I:%M %p')

@pre_dump
def adjust_timezone(self, data, **kwargs):
if data.created_at:
data.created_at = data.created_at.astimezone(TIME_ZONE)
return data

class UserSchema(BaseSchema):
class Meta:
fields = ("id", "name", "token", "is_premium", "created_at")

Expand Down Expand Up @@ -68,10 +65,8 @@ class MonitorsWebhooksSchema(ma.Schema):
class Meta:
fields = ("monitor_id", )

class WebhookSchema(ma.Schema):
class WebhookSchema(BaseSchema):
monitors = fields.Nested(MonitorsWebhooksSchema, many=True)
created_at = fields.String()

class Meta:
fields = ("id", "url", "token", "certificate_ssl", "status", "monitors", "created_at")

0 comments on commit 0f1644a

Please sign in to comment.