Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Bookshop_5001CEM/add_stock.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59 lines (54 sloc)
3.68 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from main import app | |
from flask import Flask,redirect,url_for,render_template,abort,request,abort,make_response,session,flash | |
import sqlite3 | |
from werkzeug.utils import secure_filename | |
import os | |
UPLOAD_FOLDER = '/home/codio/workspace/bookshop/static/images' | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif'] | |
@app.route('/stock_level') | |
def stock_level(): | |
con = sqlite3.connect("prime.db") | |
con.row_factory = sqlite3.Row | |
cur = con.cursor() | |
cur.execute("SELECT * from stock") | |
rows = cur.fetchall(); | |
return render_template("stock_level.html",rows = rows) | |
@app.route('/add_stock', methods=['GET', 'POST']) | |
def add_stock(): | |
'''this is a page with a form where we can add or update a book in our stock''' | |
if request.method == 'POST': | |
if request.form['Submit'] == 'SUBMIT': | |
file = request.files["file"] #here is the code that let me upload an image into files https://stackoverflow.com/questions/19898283/folder-and-files-upload-with-flask | |
if file: # if the user had given us a file | |
if allowed_extension(file.filename): # we check if it has one of the allowed extensions | |
# code for removing non ascii characters https://www.kite.com/python/answers/how-to-remove-non-ascii-characters-in-python | |
filename = secure_filename(file.filename) #we check if a filename is secure | |
encoded_filename = filename.encode("ascii", "ignore") # we replace all of the non-ascii characters with ascii ones by encoding a string | |
decoded_filename = encoded_filename.decode() #and later decode it | |
filename = decoded_filename | |
filename = filename.replace(" ","_") #in the name of the file we replace spaces with underscore | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) # we save it to our folder for images | |
con = sqlite3.connect('prime.db') | |
con.row_factory = lambda cursor, row: row[0] | |
cur = con.cursor() | |
cur.execute("SELECT ISBN FROM stock WHERE ISBN=?",(request.form['ISBN'],)) #we check if there is already an entry in our db with the same isbn13 number to know if we have to add an entry or update one | |
row = cur.fetchall() | |
if len(row) != 0: #if this is true it means that the fetchall function did found a record with same isbn13 number, and will replace the current record with the new one | |
cur.execute("DELETE FROM stock WHERE ISBN=?",(request.form['ISBN'],)) # it updates an entry by deleting the older one | |
# we add values that we provided in a form into our database | |
cur.execute("INSERT INTO stock VALUES(?,?,?,?,?,?,?,?,?)",(request.form['name'],request.form['author'],request.form['date'],request.form['ISBN'],request.form['description'],filename,request.form['tradep'],request.form['retailp'],request.form['quantity'])) | |
con.commit() | |
con.close() | |
return redirect(url_for('stock_level')) | |
elif request.form['Submit'] == 'GO BACK': | |
return redirect(url_for('stock_levels')) | |
else: | |
return render_template('add_stock.html') | |
def allowed_extension(filename): | |
'''the code below checks if file extension is valid -- based on https://flask.palletsprojects.com/en/2.0.x/patterns/fileuploads/''' | |
if '.' not in filename: #if there is no dot in our filename | |
return False | |
if filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS: #if text after the dot is in ALLOWED_EXTENSIONS | |
return True | |
return False |