diff --git a/database.py b/database.py index cc022a7..c307b4e 100644 --- a/database.py +++ b/database.py @@ -54,6 +54,13 @@ class User(TableDeclarativeBase): else: return self.first_name + def mention(self): + """Mention the user in the best way possible given the available data.""" + if self.username is not None: + return f"@{self.username}" + else: + return f"[{self.first_name}](tg://user?id={self.user_id})" + def __repr__(self): return f"" @@ -182,6 +189,7 @@ class Admin(TableDeclarativeBase): user_id = Column(BigInteger, ForeignKey("users.user_id"), primary_key=True) user = relationship("User") # Permissions + receive_orders = Column(Boolean, default=True) # TODO: unfinished # Extra table parameters @@ -215,6 +223,17 @@ class Order(TableDeclarativeBase): def __repr__(self): return f"" + def __str__(self): + items = "" + for item in self.items: + items += str(item) + "\n" + return strings.order_format_string.format(id=self.order_id, + user=self.user.mention(), + date=self.creation_date.isoformat(), + items=items, + notes=self.notes if self.notes is not None else "", + value=#TODO) + class OrderItem(TableDeclarativeBase): """A product that has been purchased as part of an order.""" @@ -230,6 +249,9 @@ class OrderItem(TableDeclarativeBase): # Extra table parameters __tablename__ = "orderitems" + def __str__(self): + return f"{self.product.name} - {self.product.price}" + def __repr__(self): return f"" diff --git a/strings.py b/strings.py index f39e6c1..f5a372c 100644 --- a/strings.py +++ b/strings.py @@ -14,6 +14,16 @@ in_stock_format_string = "{quantity} disponibili" # Copies of a product in cart in_cart_format_string = "{quantity} nel carrello" +# Order info displayed string +order_format_string = "Ordine #{id}\n" \ + "di {user}\n" \ + "{value}\n" \ + "Creato {date}\n" \ + "\n" \ + "{items}\n" \ + "\n" \ + "{notes}" + # Conversation: the start command was sent and the bot should welcome the user conversation_after_start = "Ciao!\n" \ "Benvenuto su greed!" @@ -150,6 +160,10 @@ payment_invoice_label = "Ricarica" # Payment: label of the labeled price on the invoice payment_invoice_fee_label = "Supplemento carta" +# Notification: order has been placed +notification_order_placed = "*️⃣ E' stato piazzato un nuovo ordine:\n" \ + "{order}" + # Info: informazioni sul bot bot_info = 'Questo bot utilizza greed,' \ ' un framework di @Steffo per i pagamenti su Telegram rilasciato sotto la' \ diff --git a/utils.py b/utils.py index ff1b39c..5e57d7c 100644 --- a/utils.py +++ b/utils.py @@ -1,3 +1,5 @@ +import time + from configloader import config from strings import currency_format_string, currency_symbol import typing @@ -77,4 +79,3 @@ class Price: def __ifloordiv__(self, other): self.value //= Price(other).value return self - diff --git a/worker.py b/worker.py index a7974e6..98c5abc 100644 --- a/worker.py +++ b/worker.py @@ -371,6 +371,11 @@ class ChatWorker(threading.Thread): self.session.commit() # Notify the user of the order result self.bot.send_message(self.chat.id, strings.success_order_created) + # Notify the admins (with Order Notifications enabled) of the new order created + admins = self.session.query(db.Admin).filter_by(receive_orders=True).all() + # Notify them of the new placed order + for admin in admins: + self.bot.send_message(admin.user_id, f"{strings.notification_order_placed.format(order=str(order))}") def __order_status(self): raise NotImplementedError()