def handle(self, restock_string): user = self.msg.connection.contact #Check permissions. Unregistered users will receive an unhelpful error message. if not user.role: self.respond("You do not have permission to use this command.") return True restock_list = self.parse_restock_string(restock_string) errors = [] response = "" for code, amount in restock_list: #ignore incorrect codes if amount <= 0: #no matching product code errors.append(code) else: target_product = Product.by_code(code) current_stock = Stock.get_existing(user.alias, code) if current_stock is None: s = Stock(seller=user, product=target_product, stock_amount=amount) s.save() response += "%s %s, " % (amount, target_product.display_name) else: current_stock.stock_amount += amount current_stock.save() response += "%s %s, " % (amount, target_product.display_name) if response: response = response[:-2] + " added. " if errors: for err in errors: response += "%s " % err response += "not recognized. " self.respond("%sCurrent stock: %s" % (response, self.get_current_stock(user)))
def handle(self, text): stocker = self.msg.connection.contact args = text.partition(" ") if args[0] == "cancel": self.cancel_restocks(stocker) return True target_alias = args[0].lower() restock_list = self.parse_restock_string(args[2].lower()) if not restock_list: self.help() return True # confirm that alias exists try: target = Contact.objects.get(alias=target_alias) except: self.respond("Sorry, user %s was not found. Please check your spelling and try again" % target_alias) return True errors = [] stockouts = [] pending = [] response = "" notification = "" # admin must create this user, for holding pending stock transactions nobody = Contact.by_alias("nobody") for code, amount in restock_list: if amount == 0: errors.append(code) if amount == -1 or code == "": self.respond( "Missing product code or amount. Restock messages cannot contain spaces.\nExample: restock dnombo 5ew" ) return True else: current_stock = Stock.get_existing(stocker.alias, code) # make sure the initiator of the transaction has enough stock if current_stock is None or current_stock.stock_amount < amount: stockouts.append(code) else: target_product = Product.by_code(code) s = Stock(seller=nobody, product=target_product, stock_amount=amount) s.save() pending.append(s) current_stock.stock_amount -= amount current_stock.save() if pending: # create the transaction object and add stock to be moved trans = StockTransaction(initiator=stocker, recipient=target, status=2, date_initiated=datetime.now()) trans.save() # need to generate a primary key before adding products for p in pending: trans.to_transfer.add(p) response += "%s %s " % (p.stock_amount, p.product.display_name) notification += "%s %s " % (p.stock_amount, p.product.display_name) trans.save() response += "sent to %s. " % target.alias if stockouts: for out in stockouts: response += "%s " % out response += "out of stock. " if errors: for err in errors: response += "%s " % err response += "not recognized." self.respond(response) if notification: notification += "being sent by %s. Reply 'yes' to accept, 'no' to reject." % stocker.alias target.message(notification)
def handle(self, text): stocker = self.msg.connection.contact args = text.partition(' ') if args[0] == 'cancel': self.cancel_restocks(stocker) return True target_alias = args[0].lower() restock_list = self.parse_restock_string(args[2].lower()) if not restock_list: self.help() return True # confirm that alias exists try: target = Contact.objects.get(alias=target_alias) except: self.respond( "Sorry, user %s was not found. Please check your spelling and try again" % target_alias) return True errors = [] stockouts = [] pending = [] response = "" notification = "" #admin must create this user, for holding pending stock transactions nobody = Contact.by_alias("nobody") for code, amount in restock_list: if amount == 0: errors.append(code) if amount == -1 or code == '': self.respond( "Missing product code or amount. Restock messages cannot contain spaces.\nExample: restock dnombo 5ew" ) return True else: current_stock = Stock.get_existing(stocker.alias, code) #make sure the initiator of the transaction has enough stock if current_stock is None or current_stock.stock_amount < amount: stockouts.append(code) else: target_product = Product.by_code(code) s = Stock(seller=nobody, product=target_product, stock_amount=amount) s.save() pending.append(s) current_stock.stock_amount -= amount current_stock.save() if pending: #create the transaction object and add stock to be moved trans = StockTransaction(initiator=stocker, recipient=target, status=2, date_initiated=datetime.now()) trans.save( ) #need to generate a primary key before adding products for p in pending: trans.to_transfer.add(p) response += "%s %s " % (p.stock_amount, p.product.display_name) notification += "%s %s " % (p.stock_amount, p.product.display_name) trans.save() response += "sent to %s. " % target.alias if stockouts: for out in stockouts: response += "%s " % out response += "out of stock. " if errors: for err in errors: response += "%s " % err response += "not recognized." self.respond(response) if notification: notification += "being sent by %s. Reply 'yes' to accept, 'no' to reject." % stocker.alias target.message(notification)