Possibly fixed #8 #57

Merged
profitroll merged 10 commits from profitroll/timezones-fix into dev 2024-05-31 00:25:54 +03:00
Showing only changes of commit 94e229949d - Show all commits

View File

@ -1,5 +1,6 @@
import logging import logging
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Any, Union from typing import Any, Union
from bson import ObjectId from bson import ObjectId
@ -88,26 +89,32 @@ class PyroUser:
""" """
logger.debug("%s's locale has been set to %s", self.id, locale) logger.debug("%s's locale has been set to %s", self.id, locale)
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}}) await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
self.locale = locale
async def update_state(self, enabled: bool = False) -> None: async def update_state(self, enabled: bool = False) -> None:
logger.debug("%s's state has been set to %s", self.id, enabled) logger.debug("%s's state has been set to %s", self.id, enabled)
await col_users.update_one({"_id": self._id}, {"$set": {"enabled": enabled}}) await col_users.update_one({"_id": self._id}, {"$set": {"enabled": enabled}})
self.enabled = enabled
async def update_location(self, location_id: int = 0) -> None: async def update_location(self, location_id: int = 0) -> None:
logger.debug("%s's location has been set to %s", self.id, location_id) logger.debug("%s's location has been set to %s", self.id, location_id)
await col_users.update_one( await col_users.update_one(
{"_id": self._id}, {"$set": {"location": location_id}} {"_id": self._id}, {"$set": {"location": location_id}}
) )
self.location = await Location.get(location_id)
async def update_offset(self, offset: int = 1) -> None: async def update_offset(self, offset: int = 1) -> None:
logger.debug("%s's offset has been set to %s", self.id, offset) logger.debug("%s's offset has been set to %s", self.id, offset)
await col_users.update_one({"_id": self._id}, {"$set": {"offset": offset}}) await col_users.update_one({"_id": self._id}, {"$set": {"offset": offset}})
self.offset = offset
async def update_time(self, hour: int = 18, minute: int = 0) -> None: async def update_time(self, hour: int = 18, minute: int = 0) -> None:
logger.debug("%s's time has been set to %s h. %s m.", self.id, hour, minute) logger.debug("%s's time has been set to %s h. %s m.", self.id, hour, minute)
await col_users.update_one( await col_users.update_one(
{"_id": self._id}, {"$set": {"time_hour": hour, "time_minute": minute}} {"_id": self._id}, {"$set": {"time_hour": hour, "time_minute": minute}}
) )
self.time_hour = hour
self.time_minute = minute
async def delete(self) -> None: async def delete(self) -> None:
logger.debug("%s's data has been deleted", self.id) logger.debug("%s's data has been deleted", self.id)
@ -125,3 +132,60 @@ class PyroUser:
del db_entry["_id"] # type: ignore del db_entry["_id"] # type: ignore
return db_entry return db_entry
def get_reminder_date(self) -> datetime:
"""Get next reminder date (year, month and day)
### Raises:
* `AttributeError`: Some attribute(s) are missing
### Returns:
* `datetime`: Datetime object of the next reminder date
"""
if self.location is None:
logger.warning(
"Could not get the reminder date for %s: User does not have some attribute(s) set",
self.id,
)
raise AttributeError(
f"Could not get the reminder date for {self.id}: User does not have some attribute(s) set"
)
if not self.location.timezone:
logger.warning("Location %s does not have a timezone set", self.location.id)
return (
datetime.now(self.location.timezone or timezone.utc) + timedelta(days=1)
).replace(hour=0, minute=0, second=0, microsecond=0)
def get_reminder_time(self) -> datetime:
"""Get reminder time (hour and minute)
### Raises:
* `AttributeError`: Some attribute(s) are missing
### Returns:
* `datetime`: Datetime object of the next reminder date
"""
if self.time_hour is None or self.time_minute is None or self.location is None:
logger.warning(
"Could not get the reminder time for %s: User does not have some attribute(s) set",
self.id,
)
raise AttributeError(
f"Could not get the reminder time for {self.id}: User does not have some attribute(s) set"
)
if not self.location.timezone:
logger.warning("Location %s does not have a timezone set", self.location.id)
return (
datetime.now(timezone.utc)
.replace(
hour=self.time_hour,
minute=self.time_minute,
second=0,
microsecond=0,
)
.astimezone(self.location.timezone or timezone.utc)
)