From 12da94eea24d34fa35f7b396e5cab39be34b389b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 26 Apr 2018 09:59:47 +0200 Subject: [PATCH] First user on the bot becomes admin --- database.py | 2 +- strings.py | 8 ++++---- worker.py | 20 ++++++++++++++++++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/database.py b/database.py index 34bec3f..4ecc4e0 100644 --- a/database.py +++ b/database.py @@ -253,7 +253,7 @@ class Order(TableDeclarativeBase): else: status_emoji = strings.emoji_not_processed status_text = strings.text_not_processed - if user and configloader.config["Appearance"]["full_order_info"] == "yes": + if user and configloader.config["Appearance"]["full_order_info"] == "no": return strings.user_order_format_string.format(status_emoji=status_emoji, status_text=status_text, items=items, diff --git a/strings.py b/strings.py index 027272b..a42142e 100644 --- a/strings.py +++ b/strings.py @@ -28,16 +28,16 @@ order_format_string = "di {user}\n" \ "Creato {date}\n" \ "\n" \ "{items}\n" \ - "TOTALE: {value}\n" \ + "TOTALE: {value}\n" \ "\n" \ - "Note del cliente: {notes}" + "Note del cliente: {notes}\n" # Order info string, shown to the user user_order_format_string = "{status_emoji} Ordine {status_text}\n" \ "{items}\n" \ - "TOTALE: {value}\n" \ + "TOTALE: {value}\n" \ "\n" \ - "Note: {notes}" + "Note: {notes}\n" # Transaction page is loading loading_transactions = "Caricamento delle transazioni in corso...\n" \ diff --git a/worker.py b/worker.py index 1265e90..9cd2710 100644 --- a/worker.py +++ b/worker.py @@ -65,10 +65,24 @@ class ChatWorker(threading.Thread): self.admin = self.session.query(db.Admin).filter(db.Admin.user_id == self.chat.id).one_or_none() # If the user isn't registered, create a new record and add it to the db if self.user is None: + # Check if there are other registered users: if there aren't any, the first user will be owner of the bot + will_be_owner = (self.session.query(db.User).first() is None) # Create the new record self.user = db.User(self.chat) # Add the new record to the db self.session.add(self.user) + # Flush the session to get an userid + self.session.flush() + # If the will be owner flag is set + if will_be_owner: + # Become owner + self.admin = db.Admin(user_id=self.user.user_id, + edit_products=True, + receive_orders=True, + create_transactions=True, + display_on_help=True, + is_owner=True, + live_mode=False) # Commit the transaction self.session.commit() # Capture exceptions that occour during the conversation @@ -873,7 +887,8 @@ class ChatWorker(threading.Thread): message_id=update.message.message_id) # Notify the user of the completition self.bot.send_message(order.user_id, - strings.notification_order_completed.format(order=order.get_text(self.session))) + strings.notification_order_completed.format(order=order.get_text(self.session, + user=True))) # If the user pressed the refund order button, refund the order... elif update.data == "order_refund": # Ask for a refund reason @@ -902,7 +917,8 @@ class ChatWorker(threading.Thread): message_id=update.message.message_id) # Notify the user of the refund self.bot.send_message(order.user_id, - strings.notification_order_refunded.format(order=order.get_text(self.session))) + strings.notification_order_refunded.format(order=order.get_text(self.session, + user=True))) # Notify the admin of the refund self.bot.send_message(self.chat.id, strings.success_order_refunded.format(order_id=order.order_id))