#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras #=================================================================== # using flask restful from flask import Flask, request, jsonify, session from flask_restful import Resource, Api from json import dumps import json from flask_cors import CORS import mysql.connector # ================================================================== # ================================================================== # creating the flask app app = Flask(__name__) CORS(app) # creating an API object api = Api(app) # Initialize the database Connection mydb = mysql.connector.connect( host = "127.0.0.1",#"q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com", user = "root",#"zsgmj50h7zgz9ioq", password = "rootP",#"omk5l1hrwsgvlcez", database = "PARKING"#"g0s9cnmdkziq6fsp" ) myCursor = mydb.cursor() # ================================== # Define our functions. # Define a function that gets the parking status # for all parking codes. def getParkings(): parks = [] myCursor.execute("SELECT * FROM PARKING") myRes = myCursor.fetchall() for res in myRes: if res[1] == 1: parks.append({"no": res[0], "status": True}) else: parks.append({"no": res[0], "status": False}) return parks def isMember(username, password): myCursor.execute("SELECT * FROM USERS") myRes = myCursor.fetchall() isValid = False for res in myRes: if res[1] == username and res[2] == password: isValid = True break return isValid # ================================================================== # making a class for a particular resource # the get, post methods correspond to get and post requests # they are automatically mapped by flask_restful. # other methods include put, delete, etc. class Parking(Resource): def get(self): parks = getParkings() return parks, 200 class ParkingStatus(Resource): def get(self): return """