@ -20,6 +20,9 @@ CORS(app)
# Session list
# Session list
sessions = [ ]
sessions = [ ]
# Chart values
chart = [ ]
# creating an API object
# creating an API object
api = Api ( app )
api = Api ( app )
@ -44,6 +47,10 @@ class mySqlConnect:
def cursor ( self ) :
def cursor ( self ) :
return self . cur
return self . cur
@property
def connection ( self ) :
return self . con
# ==================================
# ==================================
# Define our functions.
# Define our functions.
# Define a function that gets the parking status
# Define a function that gets the parking status
@ -91,6 +98,32 @@ def isAuthenticated(data):
except KeyError as e :
except KeyError as e :
return False
return False
# For the chart Data.
def updateChart ( ) :
parks = getParkings ( )
all_parks = len ( parks )
full = 0
for p in parks :
if p [ ' status ' ] == False :
full + = 1
j = 1
if len ( chart ) < 16 :
chart . append ( full )
print ( chart )
elif len ( chart ) == 16 :
for i in chart :
chart [ j ] = i
j + = 1
if j == 16 :
break
chart [ 0 ] = full
return True
# ==================================================================
# ==================================================================
# making a class for a particular resource
# making a class for a particular resource
# the get, post methods correspond to get and post requests
# the get, post methods correspond to get and post requests
@ -104,7 +137,7 @@ class Parking(Resource):
try :
try :
parks = getParkings ( )
parks = getParkings ( )
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
mydb . reconnect ( attempts = 1 , delay = 0 )
print ( " An error " )
return parks , 200
return parks , 200
@ -146,23 +179,29 @@ class ParkingStatus(Resource):
thereIs = False
thereIs = False
toUpdate = False
toUpdate = False
mysql = mySqlConnect ( )
myCursor = mysql . cur
try :
try :
mysql = mySqlConnect ( )
myCursor = mysql . cur
con = mysql . con
if not thereIs :
if not thereIs :
# Make a new insert entry for a new Parking Code.
# Make a new insert entry for a new Parking Code.
values = ( int ( data [ ' no ' ] ) , int ( data [ ' status ' ] ) )
values = ( int ( data [ ' no ' ] ) , int ( data [ ' status ' ] ) )
myCursor . execute ( " INSERT INTO PARKING (PARKING_CODE, PARKING_STATUS) VALUES ( %s , %s ) " , values )
myCursor . execute ( " INSERT INTO PARKING (PARKING_CODE, PARKING_STATUS) VALUES ( %s , %s ) " , values )
mydb . commit ( )
con . commit ( )
parks = getParkings ( )
parks = getParkings ( )
updateChart ( )
elif toUpdate :
elif toUpdate :
# Make an Update status for Parking Code that availability changed.
# Make an Update status for Parking Code that availability changed.
values = ( int ( data [ ' status ' ] ) , int ( data [ ' no ' ] ) )
values = ( int ( data [ ' status ' ] ) , int ( data [ ' no ' ] ) )
myCursor . execute ( " UPDATE PARKING SET PARKING_STATUS= %s WHERE PARKING_CODE= %s " , values )
myCursor . execute ( " UPDATE PARKING SET PARKING_STATUS= %s WHERE PARKING_CODE= %s " , values )
mydb . commit ( )
con . commit ( )
parks = getParkings ( )
parks = getParkings ( )
updateChart ( )
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
mydb . reconnect ( attempts = 1 , delay = 0 )
print ( " An error " )
return currentParking , 201
return currentParking , 201
else :
else :
@ -190,13 +229,27 @@ class Authenticate(Resource):
else :
else :
return " Error authentication " , 403
return " Error authentication " , 403
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
except ( mysql . connector . errors . DatabaseError , mysql . connector . errors . InterfaceError ) as e :
mydb . reconnect ( attempts = 1 , delay = 0 )
print ( " An error " )
# Chart
class Chart ( Resource ) :
def get ( self ) :
result = dict ( )
j = 1
for i in chart :
result [ j ] = i
j + = 1
print ( result )
return result , 200
# ==================================================================
# ==================================================================
# matches the defined resources to their corresponding urls to REST APIs
# matches the defined resources to their corresponding urls to REST APIs
api . add_resource ( Parking , ' / ' )
api . add_resource ( Parking , ' / ' )
api . add_resource ( ParkingStatus , ' /parkingStatus ' )
api . add_resource ( ParkingStatus , ' /parkingStatus ' )
api . add_resource ( Authenticate , ' /authenticate ' )
api . add_resource ( Authenticate , ' /authenticate ' )
api . add_resource ( Chart , ' /chart ' )
# ==================================================================
# ==================================================================
# ===========================MAIN CLASS=============================
# ===========================MAIN CLASS=============================