forked from maya-a-iuga/Web-App-DevOps-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
127 lines (103 loc) · 4.06 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from azure.identity import ManagedIdentityCredential
from azure.keyvault.secrets import SecretClient
from flask import Flask, render_template, request, redirect, url_for
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
import pyodbc
import os
# Initialise Flask App
app = Flask(__name__)
# Azure Key Vault host
key_vault_url = "https://devopskeystore.vault.azure.net/"
#Set Key Vault Client to Use Managed Identity
credential = ManagedIdentityCredential()
secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
#Access the secret in Key Vault
driver= '{ODBC Driver 18 for SQL Server}'
server_secret = secret_client.get_secret("DB-Server")
database_secret = secret_client.get_secret("DB-Name")
username_secret = secret_client.get_secret("DB-User")
password_secret = secret_client.get_secret("DB-Pass")
#Accesses the Secrets value
server = server_secret.value
database = database_secret.value
username = username_secret.value
password = password_secret.value
# Create the connection string
connection_string=f'Driver={driver};\
Server=tcp:{server},1433;\
Database={database};\
Uid={username};\
Pwd={password};\
Encrypt=yes;\
TrustServerCertificate=no;\
Connection Timeout=30;'
# Create the engine to connect to the database
engine = create_engine("mssql+pyodbc:///?odbc_connect={}".format(connection_string))
engine.connect()
# Create the Session
Session = sessionmaker(bind=engine)
# Define the Order data model
Base = declarative_base()
class Order(Base):
__tablename__ = 'orders'
date_uuid = Column('date_uuid', String, primary_key=True)
user_id = Column('User ID', String, primary_key=True)
card_number = Column('Card Number', String)
store_code = Column('Store Code', String)
product_code = Column('product_code', String)
product_quantity = Column('Product Quantity', Integer)
order_date = Column('Order Date', DateTime)
shipping_date = Column('Shipping Date', DateTime)
# define routes
# route to display orders
@app.route('/')
def display_orders():
page = int(request.args.get('page', 1))
rows_per_page = 25
# Calculate the start and end indices for the current page
start_index = (page - 1) * rows_per_page
end_index = start_index + rows_per_page
# Create a session to interact with the database
session = Session()
# Fetch a subset of data for the current page
current_page_orders = session.query(Order).order_by(Order.user_id, Order.date_uuid).slice(start_index, end_index).all()
# Calculate the total number of pages
total_rows = session.query(Order).count()
total_pages = (total_rows + rows_per_page - 1) // rows_per_page
# Close the session
session.close()
return render_template('orders.html', orders=current_page_orders, page=page, total_pages=total_pages)
# route to add orders
@app.route('/add_order', methods=['POST'])
def add_order():
date_uuid = request.form.get('date_uuid')
user_id = request.form.get('user_id')
card_number = request.form.get('card_number')
store_code = request.form.get('store_code')
product_code = request.form.get('product_code')
product_quantity = request.form.get('product_quantity')
order_date = request.form.get('order_date')
shipping_date = request.form.get('shipping_date')
# Create a session to interact with the database
session = Session()
# Create a new order object using the form data
new_order = Order(
date_uuid=date_uuid,
user_id=user_id,
card_number=card_number,
store_code=store_code,
product_code=product_code,
product_quantity=product_quantity,
order_date=order_date,
shipping_date=shipping_date
)
# Add the new order to the session and commit to the database
session.add(new_order)
session.commit()
return redirect(url_for('display_orders'))
# run the app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)