|
|
@ -7,6 +7,7 @@ from json import dumps |
|
|
|
import json |
|
|
|
from flask_cors import CORS |
|
|
|
import mysql.connector |
|
|
|
import os |
|
|
|
|
|
|
|
# ================================================================== |
|
|
|
# ================================================================== |
|
|
@ -15,6 +16,9 @@ import mysql.connector |
|
|
|
app = Flask(__name__) |
|
|
|
CORS(app) |
|
|
|
|
|
|
|
# create the secret key for session |
|
|
|
app.secret_key = os.urandom(24) |
|
|
|
|
|
|
|
# creating an API object |
|
|
|
api = Api(app) |
|
|
|
|
|
|
@ -33,17 +37,17 @@ myCursor = mydb.cursor() |
|
|
|
# Define a function that gets the parking status |
|
|
|
# for all parking codes. |
|
|
|
def getParkings(): |
|
|
|
parks = [] |
|
|
|
|
|
|
|
myCursor.execute("SELECT * FROM PARKING") |
|
|
|
myRes = myCursor.fetchall() |
|
|
|
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 |
|
|
|
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") |
|
|
@ -57,6 +61,12 @@ def isMember(username, password): |
|
|
|
|
|
|
|
return isValid |
|
|
|
|
|
|
|
def isAuthenticated(): |
|
|
|
if 'device_id' in session: |
|
|
|
return True |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
# ================================================================== |
|
|
|
# making a class for a particular resource |
|
|
@ -64,64 +74,95 @@ def isMember(username, password): |
|
|
|
# they are automatically mapped by flask_restful. |
|
|
|
# other methods include put, delete, etc. |
|
|
|
class Parking(Resource): |
|
|
|
def get(self): |
|
|
|
parks = getParkings() |
|
|
|
return parks, 200 |
|
|
|
def get(self): |
|
|
|
try: |
|
|
|
parks = getParkings() |
|
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
return parks, 200 |
|
|
|
|
|
|
|
class ParkingStatus(Resource): |
|
|
|
def get(self): |
|
|
|
return """<html> |
|
|
|
<head><title>ERROR</title></head> |
|
|
|
<body><h1>Not get at '/parkingStatus'.</h1></body> |
|
|
|
</html>""" |
|
|
|
def post(self): |
|
|
|
# Gets the data into as a JSON Object from HTTP request. |
|
|
|
data = json.loads(request.data) |
|
|
|
|
|
|
|
# SQL get all Parking places status. |
|
|
|
parks = getParkings() |
|
|
|
|
|
|
|
currentParking = {} |
|
|
|
for park in parks: |
|
|
|
if park['no'] == data['no']: |
|
|
|
currentParking = park |
|
|
|
break; |
|
|
|
|
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
try: |
|
|
|
if currentParking['status'] != data['status']: |
|
|
|
toUpdate = True |
|
|
|
thereIs = True |
|
|
|
except IndexError: |
|
|
|
# handle Index Error |
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
except KeyError: |
|
|
|
# handle the KeyError |
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
|
|
|
|
if not thereIs: |
|
|
|
# Make a new insert entry for a new Parking Code. |
|
|
|
values = (int(data['no']), int(data['status'])) |
|
|
|
myCursor.execute("INSERT INTO PARKING (PARKING_CODE, PARKING_STATUS) VALUES (%s, %s)", values) |
|
|
|
mydb.commit() |
|
|
|
parks = getParkings() |
|
|
|
elif toUpdate: |
|
|
|
# Make an Update status for Parking Code that availability changed. |
|
|
|
values = (int(data['status']), int(data['no'])) |
|
|
|
myCursor.execute("UPDATE PARKING SET PARKING_STATUS=%s WHERE PARKING_CODE=%s", values) |
|
|
|
mydb.commit() |
|
|
|
parks = getParkings() |
|
|
|
|
|
|
|
return currentParking, 201 |
|
|
|
def get(self): |
|
|
|
return """<html> |
|
|
|
<head><title>ERROR</title></head> |
|
|
|
<body><h1>Not get at '/parkingStatus'.</h1></body> |
|
|
|
</html>""" |
|
|
|
def post(self): |
|
|
|
if isAuthenticated(): |
|
|
|
# Gets the data into as a JSON Object from HTTP request. |
|
|
|
data = json.loads(request.data) |
|
|
|
|
|
|
|
try: |
|
|
|
# SQL get all Parking places status. |
|
|
|
parks = getParkings() |
|
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
currentParking = {} |
|
|
|
for park in parks: |
|
|
|
if park['no'] == data['no']: |
|
|
|
currentParking = park |
|
|
|
break; |
|
|
|
|
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
try: |
|
|
|
if currentParking['status'] != data['status']: |
|
|
|
toUpdate = True |
|
|
|
thereIs = True |
|
|
|
except IndexError: |
|
|
|
# handle Index Error |
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
except KeyError: |
|
|
|
# handle the KeyError |
|
|
|
thereIs = False |
|
|
|
toUpdate = False |
|
|
|
|
|
|
|
try: |
|
|
|
if not thereIs: |
|
|
|
# Make a new insert entry for a new Parking Code. |
|
|
|
values = (int(data['no']), int(data['status'])) |
|
|
|
myCursor.execute("INSERT INTO PARKING (PARKING_CODE, PARKING_STATUS) VALUES (%s, %s)", values) |
|
|
|
mydb.commit() |
|
|
|
parks = getParkings() |
|
|
|
elif toUpdate: |
|
|
|
# Make an Update status for Parking Code that availability changed. |
|
|
|
values = (int(data['status']), int(data['no'])) |
|
|
|
myCursor.execute("UPDATE PARKING SET PARKING_STATUS=%s WHERE PARKING_CODE=%s", values) |
|
|
|
mydb.commit() |
|
|
|
parks = getParkings() |
|
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
return currentParking, 201 |
|
|
|
else: |
|
|
|
return "Error! You aren't authenticated. [POST] /authenticate first.", 403 |
|
|
|
|
|
|
|
class Authenticate(Resource): |
|
|
|
def post(self): |
|
|
|
try: |
|
|
|
#Get the credencial from body of request. |
|
|
|
data = json.loads(request.data) |
|
|
|
|
|
|
|
if data['username'] != None and data['password'] != None and data['device'] != None: |
|
|
|
isValid = isMember(data['username'], data['password']) |
|
|
|
|
|
|
|
if isValid: |
|
|
|
session['device_id'] = data['device'] |
|
|
|
else: |
|
|
|
return "Not Authenticatiove device", 403 |
|
|
|
else: |
|
|
|
return "Error authentication", 403 |
|
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
# ================================================================== |
|
|
|
# adding the defined resources along with their corresponding urls to REST APIs |
|
|
|
api.add_resource(Parking, '/') |
|
|
|
api.add_resource(ParkingStatus, '/parkingStatus') |
|
|
|
api.add_resource(Authenticate, '/authenticate') |
|
|
|
|
|
|
|
# ================================================================== |
|
|
|
# driver function |
|
|
|