1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-22 14:04:18 +00:00

Add something. Don't really know what, I wasn't paying attention.

This commit is contained in:
Steffo 2018-02-05 13:32:18 +01:00
parent 2415a53c43
commit af1757050b
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D
2 changed files with 76 additions and 73 deletions

View file

@ -1,5 +1,5 @@
from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint
from sqlalchemy import Integer, BigInteger, String, Text, LargeBinary, DateTime from sqlalchemy import Integer, BigInteger, String, Text, LargeBinary, DateTime, Boolean
from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
import configloader import configloader
@ -70,6 +70,8 @@ class Product(TableDeclarativeBase):
price = Column(Integer) price = Column(Integer)
# Image data # Image data
image = Column(LargeBinary) image = Column(LargeBinary)
# Product is an option (example: express shipping)
boolean_product = Column(Boolean, nullable=False)
# Stock quantity, if null product has infinite stock # Stock quantity, if null product has infinite stock
stock = Column(Integer) stock = Column(Integer)

145
worker.py
View file

@ -8,7 +8,6 @@ import sys
import queue as queuem import queue as queuem
import database as db import database as db
import re import re
import datetime
from html import escape from html import escape
class StopSignal: class StopSignal:
@ -195,76 +194,75 @@ class ChatWorker(threading.Thread):
def __order_menu(self): def __order_menu(self):
"""User menu to order products from the shop.""" """User menu to order products from the shop."""
# Create a list with the requested items raise NotImplementedError()
order_items = [] # # Create a list with the requested items
# Get the products list from the db # order_items = []
products = self.session.query(db.Product).all() # # Get the products list from the db
# TODO: this should be changed # products = self.session.query(db.Product).all()
# Loop exit reason # # TODO: this should be changed
exit_reason = None # # Loop exit reason
# Ask for a list of products to order # exit_reason = None
while True: # # Ask for a list of products to order
# Create a list of product names # while True:
product_names = [product.name for product in products] # # Create a list of product names
# Add a Cancel button at the end of the keyboard # product_names = [product.name for product in products]
product_names.append(strings.menu_cancel) # # Add a Cancel button at the end of the keyboard
# If at least 1 product has been ordered, add a Done button at the start of the keyboard # product_names.append(strings.menu_cancel)
if len(order_items) > 0: # # If at least 1 product has been ordered, add a Done button at the start of the keyboard
product_names.insert(0, strings.menu_done) # if len(order_items) > 0:
# Create a keyboard using the product names # product_names.insert(0, strings.menu_done)
keyboard = [[telegram.KeyboardButton(product_name)] for product_name in product_names] # # Create a keyboard using the product names
# Wait for an answer # keyboard = [[telegram.KeyboardButton(product_name)] for product_name in product_names]
selection = self.__wait_for_specific_message(product_names) # # Wait for an answer
# If the user selected the Cancel option... # selection = self.__wait_for_specific_message(product_names)
if selection == strings.menu_cancel: # # If the user selected the Cancel option...
exit_reason = "Cancel" # if selection == strings.menu_cancel:
break # exit_reason = "Cancel"
# If the user selected the Done option... # break
elif selection == strings.menu_done: # # If the user selected the Done option...
exit_reason = "Done" # elif selection == strings.menu_done:
break # exit_reason = "Done"
# If the user selected a product... # break
else: # # If the user selected a product...
# Find the selected product # else:
product = self.session.query(db.Product).filter_by(name=selection).one() # # Find the selected product
# Add the product to the order_items list # product = self.session.query(db.Product).filter_by(name=selection).one()
order_items.append(product) # # Add the product to the order_items list
# Ask for extra notes # order_items.append(product)
self.bot.send_message(self.chat.id, strings.conversation_extra_notes) # # Ask for extra notes
# Wait for an answer # self.bot.send_message(self.chat.id, strings.conversation_extra_notes)
notes = self.__wait_for_regex("(.+)") # # Wait for an answer
# Create the confirmation message and find the total cost # notes = self.__wait_for_regex("(.+)")
total_cost = 0 # # Create the confirmation message and find the total cost
product_list_string = "" # total_cost = 0
for item in order_items: # product_list_string = ""
# Add to the string and the cost # for item in order_items:
product_list_string += f"{str(item)}\n" # # Add to the string and the cost
total_cost += item.price # product_list_string += f"{str(item)}\n"
# Send the confirmation message # total_cost += item.price
self.bot.send_message(self.chat.id, strings.conversation_confirm_cart.format(product_list=product_list_string, total_cost=strings.currency_format_string.format(symbol=strings.currency_symbol, value=(total_cost / (10 ** int(configloader.config["Payments"]["currency_exp"])))))) # # Send the confirmation message
# TODO: wait for an answer # self.bot.send_message(self.chat.id, strings.conversation_confirm_cart.format(product_list=product_list_string, total_cost=strings.currency_format_string.format(symbol=strings.currency_symbol, value=(total_cost / (10 ** int(configloader.config["Payments"]["currency_exp"]))))))
# TODO: create a new transaction # # TODO: wait for an answer
# TODO: test the code # # TODO: create a new transaction
# TODO: everything # # TODO: test the code
# Create the order record and add it to the session # # TODO: everything
order = db.Order(user=self.user, # # Create the order record and add it to the session
creation_date=datetime.datetime.now(), # order = db.Order(user=self.user,
notes=notes) # creation_date=datetime.datetime.now(),
self.session.add(order) # notes=notes)
# Commit the session so the order record gets an id # self.session.add(order)
self.session.commit() # # Commit the session so the order record gets an id
# Create the orderitems for the selected products # self.session.commit()
for item in order_items: # # Create the orderitems for the selected products
item_record = db.OrderItem(product=item, # for item in order_items:
order_id=order.order_id) # item_record = db.OrderItem(product=item,
# Add the created item to the session # order_id=order.order_id)
self.session.add(item_record) # # Add the created item to the session
# Commit the session # self.session.add(item_record)
self.session.commit() # # Commit the session
# Send a confirmation to the user # self.session.commit()
self.bot.send_message(self.chat.id, strings.success_order_created) # # Send a confirmation to the user
# self.bot.send_message(self.chat.id, strings.success_order_created)
def __order_status(self): def __order_status(self):
raise NotImplementedError() raise NotImplementedError()
@ -472,12 +470,15 @@ class ChatWorker(threading.Thread):
self.bot.send_message(self.chat.id, strings.ask_product_image) self.bot.send_message(self.chat.id, strings.ask_product_image)
# Wait for an answer # Wait for an answer
photo_list = self.__wait_for_photo() photo_list = self.__wait_for_photo()
# TODO: ask for boolean status
# If a new product is being added... # If a new product is being added...
if not product: if not product:
# Create the db record for the product # Create the db record for the product
# TODO: add the boolean status
product = db.Product(name=name, product = db.Product(name=name,
description=description, description=description,
price=price) price=price,
boolean_product=False)
# Add the record to the database # Add the record to the database
self.session.add(product) self.session.add(product)
# If a product is being edited... # If a product is being edited...