Once and for all reformat.
Will be using black code formatter
This commit is contained in:
@@ -8,7 +8,12 @@ from token_bot.token_database.region import Region
|
||||
|
||||
|
||||
class User:
|
||||
def __init__(self, user_id: int, region: Region = None, subscribed_alerts: List['pdb.Alert'] = None) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
user_id: int,
|
||||
region: Region = None,
|
||||
subscribed_alerts: List["pdb.Alert"] = None,
|
||||
) -> None:
|
||||
self.user_id: int = user_id
|
||||
self._loaded: bool = False
|
||||
self.region: Region = region
|
||||
@@ -21,7 +26,9 @@ class User:
|
||||
return hash(self.user_id)
|
||||
|
||||
@classmethod
|
||||
def from_item(cls, primary_key: int, region: Region, subscribed_alerts: List[str]) -> 'User':
|
||||
def from_item(
|
||||
cls, primary_key: int, region: Region, subscribed_alerts: List[str]
|
||||
) -> "User":
|
||||
alerts = [pdb.Alert.from_str(alert_str) for alert_str in subscribed_alerts]
|
||||
return cls(primary_key, region, alerts)
|
||||
|
||||
@@ -31,17 +38,18 @@ class User:
|
||||
|
||||
@property
|
||||
def primary_key_name(self) -> str:
|
||||
return 'user_id'
|
||||
return "user_id"
|
||||
|
||||
@property
|
||||
def key(self) -> Dict[str, str]:
|
||||
return {
|
||||
self.primary_key_name: self.primary_key
|
||||
}
|
||||
|
||||
return {self.primary_key_name: self.primary_key}
|
||||
|
||||
def _subscribed_alerts_as_trinity_list(self) -> List[str]:
|
||||
return [str(alert) for alert in self.subscribed_alerts] if self.subscribed_alerts else []
|
||||
return (
|
||||
[str(alert) for alert in self.subscribed_alerts]
|
||||
if self.subscribed_alerts
|
||||
else []
|
||||
)
|
||||
|
||||
async def _lazy_load(self, table: Table, consistent: bool = False) -> None:
|
||||
if consistent or not self._loaded:
|
||||
@@ -51,8 +59,8 @@ class User:
|
||||
await table.put_item(
|
||||
item={
|
||||
self.primary_key_name: self.primary_key,
|
||||
'region': self.region,
|
||||
'subscribed_alerts': self._subscribed_alerts_as_trinity_list()
|
||||
"region": self.region,
|
||||
"subscribed_alerts": self._subscribed_alerts_as_trinity_list(),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -65,26 +73,27 @@ class User:
|
||||
|
||||
async def get(self, table: Table, consistent: bool = False) -> bool:
|
||||
try:
|
||||
response = await table.get_item(
|
||||
key=self.key,
|
||||
consistent_read=consistent
|
||||
)
|
||||
response = await table.get_item(key=self.key, consistent_read=consistent)
|
||||
except ItemNotFound:
|
||||
return False
|
||||
|
||||
self.subscribed_alerts = []
|
||||
for string_trinity in response['subscribed_alerts']:
|
||||
for string_trinity in response["subscribed_alerts"]:
|
||||
self.subscribed_alerts.append(pdb.Alert.from_str(string_trinity))
|
||||
self.region = Region(response['region'])
|
||||
self.region = Region(response["region"])
|
||||
return True
|
||||
|
||||
async def add_alert(self, table: Table, alert: 'pdb.Alert', consistent: bool = False) -> None:
|
||||
async def add_alert(
|
||||
self, table: Table, alert: "pdb.Alert", consistent: bool = False
|
||||
) -> None:
|
||||
await self._lazy_load(table, consistent=consistent)
|
||||
if alert not in self.subscribed_alerts:
|
||||
self.subscribed_alerts.append(alert)
|
||||
await self.put(table)
|
||||
|
||||
async def remove_alert(self, table: Table, alert: 'pdb.Alert', consistent: bool = True) -> None:
|
||||
async def remove_alert(
|
||||
self, table: Table, alert: "pdb.Alert", consistent: bool = True
|
||||
) -> None:
|
||||
await self._lazy_load(table, consistent=consistent)
|
||||
if alert in self.subscribed_alerts:
|
||||
self.subscribed_alerts.remove(alert)
|
||||
|
||||
Reference in New Issue
Block a user