@ -20,18 +20,36 @@ 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 )
# 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 ( )
# Initialize the database Connection (Classified)
class mySqlConnect :
def __init__ ( self ) :
self . con = con = 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"
)
self . cur = self . con . cursor ( ) ;
def __del__ ( self ) :
self . cur . close ( )
self . con . close ( )
@property
def cursor ( self ) :
return self . cur
@property
def connection ( self ) :
return self . con
# ==================================
# ==================================
# Define our functions.
# Define our functions.
@ -40,6 +58,8 @@ myCursor = mydb.cursor()
def getParkings ( ) :
def getParkings ( ) :
parks = [ ]
parks = [ ]
sql = mySqlConnect ( )
myCursor = sql . cur
myCursor . execute ( " SELECT * FROM PARKING " )
myCursor . execute ( " SELECT * FROM PARKING " )
myRes = myCursor . fetchall ( )
myRes = myCursor . fetchall ( )
@ -53,6 +73,9 @@ def getParkings():
# Define a function that get if a user with exiting credencials
# Define a function that get if a user with exiting credencials
# username and password is authenticated.
# username and password is authenticated.
def isMember ( username , password ) :
def isMember ( username , password ) :
mysql = mySqlConnect ( )
myCursor = mysql . cur
myCursor . execute ( " SELECT * FROM USERS " )
myCursor . execute ( " SELECT * FROM USERS " )
myRes = myCursor . fetchall ( )
myRes = myCursor . fetchall ( )
@ -75,6 +98,31 @@ 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
@ -89,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
@ -132,20 +180,28 @@ class ParkingStatus(Resource):
toUpdate = False
toUpdate = False
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 :
@ -173,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=============================