Skip to content

Commit

Permalink
Merge pull request #48 from Ed-XCF/feature/hybrid-property-to-dict
Browse files Browse the repository at this point in the history
fix _asdict
  • Loading branch information
JoshYuJump authored Dec 15, 2021
2 parents edf4951 + 5550cd6 commit 2e48db6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions bali/db/connection.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class BaseModel:
updated_time: _DateTimeField
is_active: _BooleanField

def _asdict(self, include_hybrid_properties=__asdict_include_hybrid_properties__) -> Dict[str, Any]: ...
def _asdict(self, **kwargs) -> Dict[str, Any]: ...

def to_dict(self, include_hybrid_properties=__asdict_include_hybrid_properties__) -> Dict[str, Any]: ...
def to_dict(self, **kwargs) -> Dict[str, Any]: ...

def dict(self, include_hybrid_properties=__asdict_include_hybrid_properties__) -> Dict[str, Any]: ...
def dict(self, **kwargs) -> Dict[str, Any]: ...

@classmethod
def exists(cls: Type[_M], **attrs) -> bool: ...
Expand Down
7 changes: 6 additions & 1 deletion bali/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ def delete(self):
db.session.delete(self)
db.session.commit() if context_auto_commit.get() else db.session.flush()

def _asdict(self, include_hybrid_properties=__asdict_include_hybrid_properties__):
def _asdict(self, **kwargs):
include_hybrid_properties = kwargs.setdefault(
"include_hybrid_properties",
self.__asdict_include_hybrid_properties__
)

output_fields = []
for i in inspect(type(self)).all_orm_descriptors:
if isinstance(i, InstrumentedAttribute):
Expand Down
28 changes: 25 additions & 3 deletions bali/utils/timezone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import calendar
import os
from datetime import datetime
from datetime import datetime, date, timedelta
from typing import Union

import pytz
Expand Down Expand Up @@ -66,7 +67,7 @@ def make_naive(
return value.astimezone(timezone).replace(tzinfo=None)


def localtime(value: datetime = None, timezone: StrTzInfoType = None):
def localtime(value: datetime = None, timezone: StrTzInfoType = None) -> datetime:
value, timezone = value or now(), timezone or get_current_timezone()
if isinstance(timezone, str):
timezone = pytz.timezone(timezone)
Expand All @@ -75,5 +76,26 @@ def localtime(value: datetime = None, timezone: StrTzInfoType = None):
return value.astimezone(timezone)


def localdate(value: datetime = None, timezone: StrTzInfoType = None):
def localdate(value: datetime = None, timezone: StrTzInfoType = None) -> date:
return localtime(value, timezone).date()


def start_of(
granularity: str,
value: datetime = None,
*,
timezone: StrTzInfoType = None,
) -> datetime:
value = localtime(value, timezone)
if granularity == "year":
value = value.replace(month=1, day=1)
elif granularity == "month":
value = value.replace(day=1)
elif granularity == "week":
value = value - timedelta(days=calendar.weekday(value.year, value.month, value.day))
elif granularity == "day":
pass
else:
raise ValueError("Granularity must be year, month, week or day")

return value.replace(hour=0, minute=0, second=0, microsecond=0)

0 comments on commit 2e48db6

Please sign in to comment.