def logOut(): """Function to log out the current users Takes users cookie and sets the 'authenticated' value to False, logging them out @return True or False -> True if unsuccessful, False if unsuccessful """ try: cookie = SimpleCookie() http_cookie_header = environ.get('HTTP_COOKIE') if http_cookie_header: cookie.load(http_cookie_header) if 'sid' in cookie: sid = cookie['sid'].value path = get_abs_paths()['data_store'] + '/sessions/sess_' + sid session_store = open(path, writeback=True) session_store['authenticated'] = False session_store.close() global _loggedIn _loggedIn = False # successfully logged out return True except IOError as e: # failed to access the session files print(e) return False
def connect(url="DEFAULT"): """ :param url: String --> path to the database to be connected to. DEFAULT to connect to database.db :return: connection --> sqlite3.connect() obj cursor --> sqlite3.cursor obj """ if url == "DEFAULT": url = get_abs_paths()['data_store'] + '/database.db' connection = sqlite3.connect(url) cursor = connection.cursor() return connection, cursor
def create_header(title, pdf): """ Function to create the pdfs header @params title - title of pdf pdf - the pdf canvas """ pdf.setTitle(title) pdf.setFillColor(HexColor("#E9EEF4")) path = pdf.beginPath() path.moveTo(0 * cm, 0 * cm) path.lineTo(0 * cm, 30 * cm) path.lineTo(25 * cm, 30 * cm) path.lineTo(25 * cm, 0 * cm) #this creates a rectangle the size of the sheet pdf.drawPath(path, True, True) pdf.drawInlineImage(get_abs_paths()['assets'] + "/images/logo.png", 100, 745) pdf.setFillColor(HexColor("#080807")) pdf.line(0, 725, 850, 725)
def tryLogIn(user_id, password): """Function to attempt to log in a user Function takes in a user_id and password and queries them against the database. If successful, a cookie is created and given to the user as proof of login. Escaped user_id and password can be taken from a form and used here. It is assumed that the user isn't already logged in. @param user_id -> unique id in DB (email) password -> Password object @return if unsuccessful -> returns None if successful -> returns the cookie the cookie must be printed to the webpage header """ try: result = select_all_with_2_conditions("users", "email", user_id, "password", str(password)) if len(result) == 0: return None cookie = SimpleCookie() sid = sha256(repr(time()).encode()).hexdigest() cookie['sid'] = sid session_store = open(get_abs_paths()['data_store'] + '/sessions/sess_' + sid, writeback=True) session_store['authenticated'] = True session_store['user_id'] = user_id session_store.close() global _loggedIn, _user_id _loggedIn, _user_id = True, user_id except Exception as e: print('Content-Type: text/html\n') print(e) exit(0) # print(cookie) ### EDIT THIS - cookie needs to be appropriately printed to http header. return cookie
def loggedIn(): """ Checks if a user is logged in Function to check if user is logged in or not. Should be run on every page which requires a user to be logged in to use. @return authenticated -> boolean showing if already logged in or not user_id -> string of this user's user_id, empty string if unsuccessful """ if _loggedIn: return _loggedIn, _user_id authenticated = False user_id = "" cookie = SimpleCookie() http_cookie_header = environ.get('HTTP_COOKIE') if not http_cookie_header: sid = sha256(repr(time()).encode()).hexdigest() cookie['sid'] = sid else: cookie.load(http_cookie_header) if 'sid' not in cookie: sid = sha256(repr(time()).encode()).hexdigest() cookie['sid'] = sid else: sid = cookie['sid'].value #print(get_abs_paths()['python'] + 'sessions/sess_' + sid) session_store = open(get_abs_paths()['data_store'] + '/sessions/sess_' + sid, writeback=True) authenticated = session_store.get("authenticated") user_id = session_store.get("user_id") #authenticated, user_id = False, "" return authenticated, user_id
import csv from python.databases.databaseQueries import select_fullness_of_locations, get_count_of_product_expiring_soon from python.path_stuff import get_abs_paths product_history_csv_path = get_abs_paths( )["data_store"] + "/product_history.csv" path_to_created_graphs = get_abs_paths()["data_store"] + "/created_graphs" def _draw_bar_chart(title, x_label, y_label, x_items, item_counts, png_name): import matplotlib.pyplot as plt """ A function to draw and save bar charts as files Arguments: title -- String - The title you wish to have displayed above the graph x_label -- String - The label that will be displayed under the x-axis y_label -- String - The label that will be displayed beside the y-axis x_items -- List of strings - Each string is the name of the type of data, i.e. label under each bar item_counts -- List of floats - Each number represents the amount of items that are there, i.e. how high each bar will go png_name -- String - The name you wish to save the bar chart as """ index = [i for i in range(0, len(x_items), 1)] plt.bar(index, item_counts) plt.xlabel(x_label) plt.ylabel(y_label) plt.xticks(index, x_items) plt.title(title)
def setup(): new_database() try: os.makedirs(get_abs_paths()['data_store'] + '/sessions') except FileExistsError: pass
def generateStockInfo(): generatePDF(get_abs_paths()['data_store'] + "/StockDates.pdf", "StockDates", "dates") return '/data_store/StockDates.pdf'