Home > Learning, Projects/Experiments, Python > Python: My Orders Tracker P-2

Python: My Orders Tracker P-2



Learning : Pythn, sqlite, Database, SQL
Subject: Create a system to track the orders

In this part we will write the code to Add new Order to the system, and to Show the orders we have in the database. Also we will write tow new functions that we will use in our application, one is the Date Validation and the other is just to check if the user enter a [q or Q] during collecting data [Q mean Quit] then we will call the quit() function.

Before we start the Add new order function, we will write the def date_validation (the_d) : and we will pass the date that the user enter and will check if it is in the right format, here in our application we will check if it is in [dd-mm-yyyy] format, the function will return the ‘valid’ string if the date in in right format, otherwise it will return a message of error.

Here is the function code ..

# Date Validation function.
def date_validation (the_d) :

if the_d=="" or the_d[2] !='-' or the_d[5] !='-' :
return '\n Date Not Valid. [Date format: dd-mm-yyyy]'
else:

if not(len((the_d.split("-")[2])) == 4 ):
return '\n Date Not Valid "Bad Year". [Date format: dd-mm-yyyy].'
if not (len((the_d.split("-")[1])) == 2 and (int(the_d.split("-")[1]) > 0 and int(the_d.split("-")[1]) 0 and int(the_d.split("-")[0]) <=31)) :
return '\n Date Not Valid "Bad Day". [Date format: dd-mm-yyyy].'

return 'valid'

The other function as we said, we will call it after each data entry to check on user input if it is ‘Q’ or Not. Here is the code..

def q_to_quit(check):

# If the user enter [q or Q] the function will return quit function.
if check in ['q','Q'] :
return quit()

Now, we will start to write the function to Add new order, we will ask the user to enter the date for the order, such as Order date, order number, the description, price and so-on. Here is the code..

# Function to add new order to the system.
def add_order():
os.system('clear')
print("\n==========[ Add New Order ]==========\n")

while True :
print(' NOTE: Enter Q any time to EXIT/Quit. \n')
order_date = input(' Enter the Date of the Order. as[dd-mm-yyyy] > ')

q_to_quit(order_date)

if date_validation (order_date) == 'valid' :
break

else :
print(date_validation (order_date))

order_Num = input(' Enter the order ID or Number. > ')
q_to_quit(order_Num)

order_desc = input(' Enter the order Description. > ')
q_to_quit(order_desc)

order_price = input(' Enter the Order Price. > ')
q_to_quit(order_price)

order_quantity = input(' Enter the quantity of the order. > ')
q_to_quit(order_quantity)

shipment_price = input(' Enter the shipment_price. > ')
q_to_quit(order_price)

order_link = input(' Enter the hyper Link to the Order. > ')
q_to_quit(order_link)

order_img = input(' Enter the Image file path. > ')
q_to_quit(order_img)

order_desc = " ".join([word.capitalize() for word in order_desc.split(" ")])
c.execute ("INSERT INTO orders (order_date, order_num, order_desc, order_price,order_quantity, shipment_price , order_link, order_img ) VALUES(:order_date, :order_Num, :order_desc, :order_price, :order_quantity, :shipment_price , :order_link, :order_img)", {"order_date":order_date, "order_Num":order_Num , "order_desc":order_desc, "order_price":order_price,"order_quantity":order_quantity, "shipment_price":shipment_price , "order_link":order_link, "order_img":order_img})

db_conn.commit()

input('\n Press any key to Contenu..')

After adding a records to the database, now we want to show what we have and print it on the screen, so we will write a function to Show the data. Here is the code..

# Function to display the data on the screen. 


def show_order():
os.system('clear')
print("\n==========[ Show Orders ]==========\n")

c.execute ("select * from orders where o_id >0")
order_list = c.fetchall()

for x in range (0,len(order_list)):
print(" ID: ",order_list[x][0]," "*(10-(len(str(order_list[x][0])))), end='')
print("Date: ",order_list[x][1]," "*2, end='')
print(" Order Number: ",order_list[x][2]," "*(8 - len(order_list[x][2])))
print("Price: ",order_list[x][4]," "*(6 - len(str(order_list[x][4]))), end='')
print("Quantity: ",order_list[x][5]," "*(11 - len(str(order_list[x][5]))), end='')
print("Shipment Price: ",order_list[x][6]," "*(10 - len(str(order_list[x][6]))), end='')
print("[ Total Cost: ",(order_list[x][4]*order_list[x][5]) + order_list[x][6],"]")

print("\nDescription:",order_list[x][3])
print("Image:",order_list[x][8])
print("Link:",order_list[x][7])

print("-------------------------------------------------------------------\n")

input('\n Press any key to Contenu.. ')
ali radwani python project learning sql codeing

In Next Post: In the coming post P3 , we will write the codes to Delete an Order and to Edit an order.

To Download my Python code (.py) files Click-Here


Follow me on Twitter..


By: Ali Radwani

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: