Skip to content

Commit

Permalink
added db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdurahman Ahmed committed Aug 1, 2019
1 parent 45890cd commit c7835a9
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ venv/
.pytest_cache/
__pycache__/
secrets.py
seed.txt
seed.txt
config.py
98 changes: 81 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,71 @@
from livereload import Server
from flask import Flask, render_template,request, redirect
from flask_mysqldb import MySQL
from config import user,password,db,host
import datetime
app = Flask(__name__)
this_dict ={
"customer": "First Last",
"address": "1234 Something Ave, Edmonton",
"sale_order": "1234",
"date": datetime.datetime.now()
}
items = {
"1":"Bananas",
"2":"Limes",
"3":"Mangos"
}
app.config['MYSQL_USER'] = user
app.config['MYSQL_PASSWORD'] = password
app.config['MYSQL_DB'] = db
app.config['MYSQL_HOST'] = host
mysql = MySQL(app)

open_order = False
curCID = 0
curOID = 0
order={}
count=0
total=0
orders={}
counter=0

def getProducts():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM products")
mysql.connection.commit()
myresult = cur.fetchall()
cur.close()
return myresult

def getCustomers():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM customers")
mysql.connection.commit()
myresult = cur.fetchall()
cur.close()
return myresult
def getCustomer(id):
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM customers where cid="+id)
mysql.connection.commit()
myresult = cur.fetchone()
cur.close()
return myresult
def setOrder(id):
cur = mysql.connection.cursor()
cur.execute("INSERT INTO sale_orders (cid) VALUES("+id+");")
mysql.connection.commit()
cur.execute("SELECT LAST_INSERT_ID()")
mysql.connection.commit()
myresult = cur.fetchone()
cur.close()
return myresult
def saveItem(item):
cur = mysql.connection.cursor()
cur.execute("INSERT INTO single_order VALUES ("+str(curOID)+","+str(item['id'])+","+str(item['price'])+","+str(item['quantity'])+")")
mysql.connection.commit()
cur.execute("SELECT * from single_order where oid="+curOID+" and pid="+item['id']+"")
mysql.connection.commit()
myresult = cur.fetchone()
cur.close()
return myresult
def getOrderItems():
cur = mysql.connection.cursor()
cur.execute("SELECT products.pid, products.name, single_order.price, single_order.quantity from single_order inner join products on products.pid = single_order.pid where single_order.oid = "+curOID+"")
mysql.connection.commit()
myresult = cur.fetchall()
return myresult

@app.route('/',methods =['POST','GET'])
def index():
global count
Expand All @@ -32,13 +80,16 @@ def index():
'quantity':result['quantity'],
'total':int(result['price']) * int(result['quantity'])
}
print(item)
order.update({count:item})
print(order)
count +=1
myresult = saveItem(item)
#print(item)
#print(myresult)
order = getOrderItems();
#print(order)
#count +=1
total += item['total']
return render_template('index.html', this_dict=this_dict, items=items, result=order,total=total)
return render_template('index.html',this_dict=this_dict,items=items, result=order,total=total)
if open_order == True:
return render_template('index.html',customer=getCustomer(curCID),items=getProducts(),result=order,total=total,curOID=curOID)
return render_template('index.html',customers=getCustomers(),items=getProducts(), result=order,total=total)

@app.route('/delete/<value>',methods=['GET'])
def delete_item(value):
Expand Down Expand Up @@ -75,6 +126,19 @@ def showOrders():
print(orders)
return render_template('sale_orders.html',orders=orders)

@app.route('/setCustomer/<id>',methods=['GET'])
def setCustomer(id):
global open_order
global curCID
global curOID
if (open_order != True):
curCID = str(getCustomer(id)[0])
#print(customer[0])
curOID = str(setOrder(id)[0])
#print(myOrder[0])
open_order = True
return redirect('/')

if __name__ == '__main__':
app.debug=True
server = Server(app.wsgi_app)
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
Click==7.0
Flask==1.1.1
Flask-MySQL==1.4.0
Flask-MySQLdb==0.2.0
itsdangerous==1.1.0
Jinja2==2.10.1
livereload==2.6.1
MarkupSafe==1.1.1
mysqlclient==1.4.2.post1
PyMySQL==0.9.3
six==1.12.0
tornado==6.0.3
Werkzeug==0.15.5
5 changes: 4 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<title>SmartNinja with Jinja</title>
<link rel="stylesheet" href="/static/css/style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
</head>
<body>

Expand Down Expand Up @@ -43,7 +47,6 @@
</div>


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
Expand Down
130 changes: 73 additions & 57 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,75 +1,91 @@
{% extends "base.html" %}

{% block content %}
<h1>Sales Order</h1>
<div class="info container-fluid">
<p>Bill To</p>
<p>{{this_dict.customer}} <span class="float-right">Order#: {{this_dict.sale_order}}</span></p>
<p>{{this_dict.address}}</p>
</div>
<div class="container">
<form class="form-row" action="http://localhost:5500/" method="POST">
<div class="form-group col-5">
<label for="item-name">Item</label>
<select class="form-control" name="item-name" required>
{% for id,name in items.items() %}
<option>{{id}}-{{name}}</option>
}
{% extends "base.html" %} {% block content %}
<h1 class="my-3">Sales Order<span class="float-right">SO#{% if curOID %}{{curOID}}{%endif%}</span></h1>
<div class="info container-fluid my-5">
<p><strong>Bill To:</strong></p>
{%if customer%}
<p>{{customer[1]}}</p>
<p>{{customer[2]}}</p>
{% else %}
<select id="customerSelect" class="form-control" name="item-name" required>
{% for id,name,address in customers %}
<option value="{{id}}">{{id}}-{{name}} {{address}}
</option>
{%endfor%}
</select>
<button id="setCustomer" class="btn btn-primary my-3">Set</button>
{% endif %}
</div>
<script>
$(document).ready(function(){
$('#setCustomer').click(function(){
var id = $('#customerSelect').find(":selected").val();
$.get({url: "http://localhost:5500/setCustomer/"+id},function(){
location.reload();
});
});
});
</script>
<div class="container">
<form class="form-row" action="http://localhost:5500/" method="POST">
<div class="form-group col-5">
<label for="item-name">Item</label>
<select class="form-control" name="item-name" required>
{% for id,name in items %}
<option value="{{id}}">{{id}}-{{name}}</option>
<button>Select</button>
{%endfor%}
</select>
</div>
<div class="form-group col">
</select>
</div>
<div class="form-group col">
<label for="price">Price</label>
<input type="number" class="form-control" name="price" min="0" required>
</div>
<div class="form-group col">
</div>
<div class="form-group col">
<label for="quantity">Quantity</label>
<input type="number" class="form-control" name="quantity" step="1" min="1" required>
</div>
<button type="submit" class="btn btn-primary my-3">Add</button>
</form>
</div>
<button type="submit" class="btn btn-primary my-3">Add</button>
</form>

<table class="table" id ="myTable">
<thead class="thead-dark header">
<table class="table" id="myTable">
<thead class="thead-dark header">
<tr>
<th scope="col">Item #</th>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Total</th>
<th scope="col"></th>
<th scope="col">Item #</th>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Total</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% if result %}
{% for value,item in result.items() %}
</thead>
<tbody>
{% if result %}{% for key in result%}
<tr>
<th scope="row">{{item.id}}</th>
<td>{{item.name}}</td>
<td>${{item.price}}</td>
<td>{{item.quantity}}</td>
<td>${{item.total}}</td>
<form action="http://localhost:5500/delete/{{value}}" method="GET">
<td><input type="submit" value="Delete"></td>
</form>
</tr>
{% endfor %}
{% endif %}
<th scope="row">{{key[0]}}</th>
<td>{{key[1]}}</td>
<td>${{key[2]}}</td>
<td>{{key[3]}}</td>
<td>${{key[2]*key[3]}}</td>
<form action="http://localhost:5500/delete/{{value}}" method="GET">
<td>
<input type="submit" value="Delete">
</td>
</form>
</tr>
</tbody>
</table>
<div>
<span class="float-left">
<form action="http://localhost:5500/save/{{this_dict.sale_order}}" method="POST">
{% endfor %}{% endif %}
</tbody>
</table>
<div>
<span class="float-left">
<form action="http://localhost:5500/save/1234" method="POST">
<button type="submit" class="btn btn-primary">
Save
</button>
</form>
</span>
<span class="float-right"><strong>Total:</strong>${{total}}</span>
</div>

<span class="float-right"><strong>Total:</strong>${{total}}</span>
</div>

</div>

{% endblock content %}
{% endblock content %}

0 comments on commit c7835a9

Please sign in to comment.