From 1ebe5b1df72a37e6b9e3ee235de169f359ad5642 Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 29 Jan 2025 15:35:36 +0530 Subject: [PATCH] Set up db and fixed view page --- main.py => app.py | 79 ++++++++++++++++++++--------------------- models.py | 21 +++++++++++ templates/create.html | 28 +++++++-------- templates/update.html | 26 +++++++------- templates/viewList.html | 24 ++++++------- 5 files changed, 99 insertions(+), 79 deletions(-) rename main.py => app.py (51%) diff --git a/main.py b/app.py similarity index 51% rename from main.py rename to app.py index 68ab553..cf03205 100644 --- a/main.py +++ b/app.py @@ -4,13 +4,12 @@ from models import * import csv app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///inventory.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://assetadmin:1234@localhost/asset_test_db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) -@app.before_first_request -def create_table(): +with app.app_context(): db.create_all() @@ -25,16 +24,16 @@ def create(): return render_template('create.html') if request.method == 'POST': - sku = request.form['sku'] - name = request.form['name'] - description = request.form['description'] - price = request.form['price'] + assettag = request.form['assettag'] + hostname = request.form['hostname'] + warrantyfrom = request.form['warrantyfrom'] + status = request.form['status'] try: - qty = int(request.form['qty']) + staffnum = int(request.form['staffnum']) except ValueError: - return render_template('create.html', exc='qty') + return render_template('create.html', exc='staffnum') - item = Item(sku=sku, name=name, description=description, price=price, qty=qty) + item = Asset(assettag=assettag, hostname=hostname, warrantyfrom=warrantyfrom, status=status, staffnum=staffnum) try: db.session.add(item) @@ -42,7 +41,7 @@ def create(): except exc.IntegrityError: return render_template('create.html', exc='integrity') except exc.StatementError: - return render_template('create.html', exc='price') + return render_template('create.html', exc='status') return redirect('/view') @@ -52,63 +51,63 @@ def view_list(): if request.method == 'POST': outfile = open('inventory_export.csv', 'w', newline='') outcsv = csv.writer(outfile) - records = db.session.query(Item).all() - outcsv.writerow([column.name for column in Item.__mapper__.columns]) - [outcsv.writerow([getattr(curr, column.name) for column in Item.__mapper__.columns]) for curr in records] + records = db.session.query(Asset).all() + outcsv.writerow([column.hostname for column in Asset.__mapper__.columns]) + [outcsv.writerow([getattr(curr, column.hostname) for column in Asset.__mapper__.columns]) for curr in records] outfile.close() return send_file('inventory_export.csv', as_attachment=True) - items = Item.query.all() - return render_template('viewlist.html', items=items) + items = Asset.query.all() + return render_template('viewList.html', items=items) -@app.route('/view//') -def view_item(sku): - item = Item.query.filter_by(sku=sku).first() +@app.route('/view//') +def view_item(assettag): + item = Asset.query.filter_by(assettag=assettag).first() if item: return render_template('viewitem.html', item=item) - return f"Item {sku} is not found" + return f"Asset {assettag} is not found" -@app.route('/update//', methods=['GET', 'POST']) -def update(sku): - item = Item.query.filter_by(sku=sku).first() +@app.route('/update//', methods=['GET', 'POST']) +def update(assettag): + item = Asset.query.filter_by(assettag=assettag).first() if request.method == 'POST': if item: - sku = request.form['sku'] - name = request.form['name'] - description = request.form['description'] + assettag = request.form['assettag'] + hostname = request.form['hostname'] + warrantyfrom = request.form['warrantyfrom'] try: - price = float(request.form['price']) + status = float(request.form['status']) except ValueError: - return render_template('update.html', item=item, exc='price') + return render_template('update.html', item=item, exc='status') try: - qty = int(request.form['qty']) + staffnum = int(request.form['staffnum']) except ValueError: - return render_template('update.html', item=item, exc='qty') + return render_template('update.html', item=item, exc='staffnum') try: - setattr(item, 'sku', sku) - setattr(item, 'name', name) - setattr(item, 'description', description) - setattr(item, 'price', price) - setattr(item, 'qty', qty) + setattr(item, 'assettag', assettag) + setattr(item, 'hostname', hostname) + setattr(item, 'warrantyfrom', warrantyfrom) + setattr(item, 'status', status) + setattr(item, 'staffnum', staffnum) db.session.commit() except exc.IntegrityError: return render_template('update.html', item=item, exc='integrity') except (exc.StatementError, exc.InvalidRequestError) as e: - return render_template('update.html', item=item, exc='price') + return render_template('update.html', item=item, exc='status') return redirect(f'/view/') - return f"Item {sku} is not found" + return f"Asset {assettag} is not found" return render_template('update.html', item=item) -@app.route('/delete//', methods=['GET', 'POST']) -def delete(sku): - item = Item.query.filter_by(sku=sku).first() +@app.route('/delete//', methods=['GET', 'POST']) +def delete(assettag): + item = Asset.query.filter_by(assettag=assettag).first() if request.method == 'POST': if item: db.session.delete(item) diff --git a/models.py b/models.py index b6f7699..406c099 100644 --- a/models.py +++ b/models.py @@ -2,7 +2,27 @@ from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() +class Asset(db.Model): + __tablename__ = "asset_test" # Table in MySQL + assettag = db.Column(db.String(50), primary_key=True) # Primary key + hostname = db.Column(db.String(50)) # VARCHAR(50) + warrantyfrom = db.Column(db.Date) # DATE + status = db.Column(db.Enum('Active', 'Inactive')) # ENUM('Active', 'Inactive') + #staffnum = db.Column(db.Integer, unsigned=True) # INT(8) UNSIGNED + staffnum = db.Column(db.Integer) # INT(8) UNSIGNED + + def __init__(self, assettag, hostname=None, warrantyfrom=None, status=None, staffnum=None): + self.assettag = assettag + self.hostname = hostname + self.warrantyfrom = warrantyfrom + self.status = status + self.staffnum = staffnum + + def __repr__(self): + return f"" + +""" Original code class Item(db.Model): __tablename__ = "Item" @@ -22,3 +42,4 @@ class Item(db.Model): def __repr__(self): return f"{self.name}:{self.sku}" +""" diff --git a/templates/create.html b/templates/create.html index b27e1fa..58e40e2 100644 --- a/templates/create.html +++ b/templates/create.html @@ -20,37 +20,37 @@

- - + +

- - + +

- - + +

- - + +

- - + +

{% if exc == 'integrity' %} - Item with such SKU already exists + Item with the same assettag already exists {% endif %} - {% if exc == 'price' %} + {% if exc == 'status' %} Data input error. Price must have a numeric value {% endif %} - {% if exc == 'qty' %} - Data input error. Quantity must be an integer + {% if exc == 'staffnum' %} + Data input error. Staff number must be an integer {% endif %}

diff --git a/templates/update.html b/templates/update.html index 7c71235..37f59a7 100644 --- a/templates/update.html +++ b/templates/update.html @@ -20,24 +20,24 @@

- - + +

- - + +

- - + +

- - + +

- - + +

@@ -48,11 +48,11 @@ {% if exc == 'integrity' %} Item with such SKU already exists {% endif %} - {% if exc == 'price' %} + {% if exc == 'status' %} Data input error. Price must have a numeric value {% endif %} - {% if exc == 'qty' %} - Data input error. Quantity must be an integer + {% if exc == 'staffnum' %} + Data input error. Staff number must be an integer {% endif %}

diff --git a/templates/viewList.html b/templates/viewList.html index da29226..869951c 100644 --- a/templates/viewList.html +++ b/templates/viewList.html @@ -14,36 +14,36 @@ - - - - - + + + + + {% for item in items %}
SKUNameDescriptionPriceQuantityAsset TagHost NameWarranty FromStatusStaff No.
- {{item.sku}} + {{item.assettag}} - {{item.name}} + {{item.hostname}} - {{item.description}} + {{item.warrantyfrom}} - ${{item.price}} + ${{item.status}} - {{item.qty}} + {{item.staffnum}} - + -
+