darkor

crud for subscriptions

Feb 22nd, 2023
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. class SubscriptionCompanyAggregationActive:
  2.  
  3.     @staticmethod
  4.     def prepare_keys(company_id: str) -> Dict[str, str]:
  5.         return {"sk": f"total_active#{db_const.SK_SUBSCRIPTION_KEY}", "pk": f"{db_const.PK_COMPANY_KEY}{company_id}"}
  6.  
  7.     @classmethod
  8.     def create(cls, company_id: str, active: int = 0) -> Dict[str, Any]:
  9.         item = {
  10.             **cls.prepare_keys(company_id=company_id),
  11.             "company_id": company_id,
  12.             "active": active,
  13.             "ytg": {}
  14.         }
  15.  
  16.         try:
  17.             db.MAIN_TABLE_OBJECT.put_item(Item=item)
  18.         except ClientError:
  19.             raise db_exceptions.DbInsertException
  20.  
  21.         return item
  22.  
  23.     @classmethod
  24.     def increment(cls, company_id: str) -> Dict[str, Any]:
  25.         try:
  26.             response = db.MAIN_TABLE_OBJECT.update_item(
  27.                 UpdateExpression="SET active = active + :num",
  28.                 Key=cls.prepare_keys(company_id=company_id),
  29.                 ExpressionAttributeValues={":num": 1},
  30.                 ReturnValues="ALL_NEW",
  31.             )
  32.  
  33.         except ClientError as e:
  34.             print(e)
  35.             raise db_exceptions.DbUpdateException
  36.  
  37.         return response.get("Attributes", {})
  38.  
  39.     @classmethod
  40.     def decrement(cls, company_id: str) -> Dict[str, Any]:
  41.         try:
  42.             response = db.MAIN_TABLE_OBJECT.update_item(
  43.                 UpdateExpression="SET active = active - :num",
  44.                 Key=cls.prepare_keys(company_id=company_id),
  45.                 ExpressionAttributeValues={":num": 1},
  46.                 ReturnValues="ALL_NEW",
  47.             )
  48.  
  49.         except ClientError as e:
  50.             print(e)
  51.             raise db_exceptions.DbUpdateException
  52.  
  53.         return response.get("Attributes", {})
  54.  
  55.     @classmethod
  56.     def get(cls, company_id: str) -> Dict[str, Any]:
  57.         try:
  58.             return db.MAIN_TABLE_OBJECT.get_item(Key=cls.prepare_keys(company_id=company_id))["Item"]
  59.  
  60.         except (ClientError, KeyError):
  61.             raise db_exceptions.DbSelectException
  62.  
  63.     @classmethod
  64.     def increment_ytg(
  65.         cls,
  66.         company_id: str,
  67.         price: Decimal,
  68.         time: datetime,
  69.         day_exist: bool = False,
  70.         year_exist: bool = False,
  71.         month_exist: bool = False
  72.     ) -> None:
  73.         kwargs = {
  74.             "Key": cls.prepare_keys(company_id=company_id),
  75.             **create_year_to_go_update_expression(
  76.                 time=time, price=price, day_exist=day_exist, year_exist=year_exist, month_exist=month_exist
  77.             ),
  78.             "ReturnValues": "ALL_NEW",
  79.         }
  80.  
  81.         try:
  82.             response = db.MAIN_TABLE_OBJECT.update_item(**kwargs)
  83.  
  84.         except ClientError as e:
  85.             print(e)
  86.             raise db_exceptions.DbUpdateException
  87.  
  88.         return response.get("Attributes", {})
Advertisement
Add Comment
Please, Sign In to add comment