From 3be7b50d09cfd87c35d14caa22bb036cf140cabc Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 14 May 2022 18:57:04 +0000 Subject: [PATCH] Use identity check for comparison to a singleton --- services/Auth.py | 12 +++++----- services/EventLogger.py | 6 ++--- services/HighLow.py | 50 ++++++++++++++++++++--------------------- services/User.py | 4 ++-- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/services/Auth.py b/services/Auth.py index d947a86..ad04705 100644 --- a/services/Auth.py +++ b/services/Auth.py @@ -88,7 +88,7 @@ def sign_in_with_oauth(self, provider_key, provider_name, firstname, lastname, e user = cursor.fetchone() - if user != None: + if user is not None: conn.close() return json.dumps({ "uid": user["uid"], @@ -244,7 +244,7 @@ def sign_up(self, firstname, lastname, email, password, confirmpassword, platfor hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) #Insert into the database - if platform == None: + if platform is None: cursor.execute("INSERT INTO users(uid, firstname, lastname, email, password, profileimage, streak, bio) VALUES('" + str(uid) + "', '" + firstname + "', '" + lastname + "', '" + email + "', '" + hashed_password.decode('utf-8') + "', 'user/" + str(uid) + "/profile/profile.png', 0, '');") else: cursor.execute("INSERT INTO users(uid, firstname, lastname, email, password, profileimage, streak, bio, platform) VALUES('" + str(uid) + "', '" + firstname + "', '" + lastname + "', '" + email + "', '" + hashed_password.decode('utf-8') + "', 'user/" + str(uid) + "/profile/profile.png', 0, '', " + str(platform) + ");") @@ -289,10 +289,10 @@ def sign_in(self, email, password): existingUser = cursor.fetchone() - if existingUser != None: + if existingUser is not None: #If the password is correct... - if existingUser["banned"] != True and bcrypt.checkpw(password.encode('utf-8'), existingUser["password"].encode('utf-8')): + if existingUser["banned"] is not True and bcrypt.checkpw(password.encode('utf-8'), existingUser["password"].encode('utf-8')): #The user is authenticated; create and return a token access_token = self.create_token( existingUser["uid"] ) @@ -332,7 +332,7 @@ def admin_sign_in(self, username, password): existingUser = cursor.fetchone() - if existingUser != None: + if existingUser is not None: #If the password is correct... if bcrypt.checkpw(password.encode('utf-8'), existingUser["password"].encode('utf-8')): @@ -601,7 +601,7 @@ def refresh_access(self, refresh_token): conn.commit() conn.close() - if user != None and user["banned"]: + if user is not None and user["banned"]: return "ERROR-INVALID-REFRESH-TOKEN" #Create a new token and return it diff --git a/services/EventLogger.py b/services/EventLogger.py index 0395bd0..db4b410 100644 --- a/services/EventLogger.py +++ b/services/EventLogger.py @@ -54,17 +54,17 @@ def query(self, _type=None, min_time=None, max_time=None, conditions=[], admin_p #Time constraint time_constraint_str = "" - if min_time != None: + if min_time is not None: time_constraint_str += " AND _timestamp >= TIMESTAMP('{}') ".format(min_time) - if max_time != None: + if max_time is not None: time_constraint_str += " AND _timestamp <= TIMESTAMP('{}') ".format(max_time) #Type constraint type_str = "" - if _type != None: + if _type is not None: type_str += " AND type='{}'".format(_type) #Password check diff --git a/services/HighLow.py b/services/HighLow.py index a337680..463b9d0 100644 --- a/services/HighLow.py +++ b/services/HighLow.py @@ -21,7 +21,7 @@ def __init__(self, host, username, password, database, high_low_id=None): self.database = database self.high_low_id = "" - if high_low_id != None: + if high_low_id is not None: self.high_low_id = pymysql.escape_string( bleach.clean(high_low_id) ) conn = pymysql.connect(self.host, self.username, self.password, self.database, cursorclass=pymysql.cursors.DictCursor, charset='utf8mb4') @@ -104,17 +104,17 @@ def create(self, uid, _date, high=None, low=None, high_image=None, low_image=Non #Create a High/Low ID self.high_low_id = str( uuid.uuid1() ) - if high != None: + if high is not None: self.high = pymysql.escape_string( self.sanitize_html(high) ) self.high = "{}".format(self.high) - if low != None: + if low is not None: self.low = pymysql.escape_string( self.sanitize_html(low) ) self.low = "{}".format(self.low) - if high_image != None: + if high_image is not None: fileStorage = FileStorage() upload_result = json.loads( fileStorage.upload_to_high_images(high_image) ) @@ -126,7 +126,7 @@ def create(self, uid, _date, high=None, low=None, high_image=None, low_image=Non self.high_image = "{}".format(upload_result["file"]) - if low_image != None: + if low_image is not None: fileStorage = FileStorage() upload_result = json.loads( fileStorage.upload_to_low_images(low_image) ) @@ -231,12 +231,12 @@ def get_json(self, uid=None, supports_html=False): conn = pymysql.connect(self.host, self.username, self.password, self.database, cursorclass=pymysql.cursors.DictCursor, charset='utf8mb4') cursor = conn.cursor() - if uid != None: + if uid is not None: cursor.execute( "SELECT * FROM likes WHERE uid='{}' AND highlowid='{}'".format(uid, self.high_low_id) ) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: json_object["liked"] = True cursor.execute("SELECT * FROM flags WHERE uid='{}' AND highlowid='{}'".format(uid, self.high_low_id)) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: json_object["flagged"] = True cursor.execute( """ @@ -317,7 +317,7 @@ def update_high(self, uid, text=None, image=None, isPrivate=False, supports_html conn = pymysql.connect(self.host, self.username, self.password, self.database, cursorclass=pymysql.cursors.DictCursor, charset='utf8mb4') cursor = conn.cursor() - if text != None: + if text is not None: text = pymysql.escape_string( self.sanitize_html(text) ) self.high = text text = "'{}'".format(text) @@ -326,7 +326,7 @@ def update_high(self, uid, text=None, image=None, isPrivate=False, supports_html filename = "" - if image != None: + if image is not None: fileStorage = FileStorage() @@ -361,7 +361,7 @@ def update_low(self, uid, text=None, image=None, isPrivate=False, supports_html= conn = pymysql.connect(self.host, self.username, self.password, self.database, cursorclass=pymysql.cursors.DictCursor, charset='utf8mb4') cursor = conn.cursor() - if text != None: + if text is not None: text = pymysql.escape_string( self.sanitize_html(text) ) self.low = text text = "'{}'".format(text) @@ -370,7 +370,7 @@ def update_low(self, uid, text=None, image=None, isPrivate=False, supports_html= filename = "" - if image != None: + if image is not None: fileStorage = FileStorage() @@ -437,7 +437,7 @@ def like(self, uid): #Make sure this user hasn't "liked" before cursor.execute("SELECT * FROM likes WHERE highlowid='{}' AND uid='{}';".format(self.high_low_id, uid)) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: conn.commit() conn.close() return { 'error': 'already-liked' } @@ -445,7 +445,7 @@ def like(self, uid): #Make sure the highlow does not belong to the user cursor.execute("SELECT uid FROM highlows WHERE highlowid='{}' AND uid='{}';".format(self.high_low_id, uid)) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: conn.commit() conn.close() return { 'error': 'not-allowed' } @@ -487,7 +487,7 @@ def unlike(self, uid): #Update the High/Low's total likes cursor.execute( "SELECT * FROM likes WHERE highlowid='{}' AND uid='{}';".format(self.high_low_id, uid) ) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: cursor.execute( "UPDATE highlows SET total_likes = total_likes - 1 WHERE highlowid='{}'".format(self.high_low_id) ) #Delete the entry, if it exists @@ -674,7 +674,7 @@ def unflag(self, uid, supports_html=False): cursor.execute("SELECT id FROM flags WHERE highlowid='{}' AND flagger='{}';".format(self.high_low_id, uid)) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: cursor.execute( """ UPDATE users SET times_flagged = IF(times_flagged > 0, times_flagged - 1, 0) @@ -740,7 +740,7 @@ def get_highlows_for_user(self, uid, current_user, limit, page, sortby=None, sup highlows = cursor.fetchall() - if sortby != None: + if sortby is not None: options = { @@ -750,7 +750,7 @@ def get_highlows_for_user(self, uid, current_user, limit, page, sortby=None, sup } - if options[sortby][0] == None: + if options[sortby][0] is None: highlows = sorted(highlows, reverse=options[sortby][1]) else: highlows = sorted(highlows, key=lambda a: a[ options[sortby][0] ], reverse=options[sortby][1]) @@ -814,7 +814,7 @@ def get_today_for_user(self, uid, supports_html=False): highlow = cursor.fetchone() - if highlow == None: + if highlow is None: conn.commit() conn.close() @@ -839,10 +839,10 @@ def get_today_for_user(self, uid, supports_html=False): highlow['low'] = self.html_to_plain_text(highlow['low']) cursor.execute( "SELECT * FROM likes WHERE uid='{}' AND _date='{}';".format(uid, datestr) ) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: highlow["liked"] = True cursor.execute("SELECT * FROM flags WHERE uid='{}' AND _date='{}';".format(uid, datestr)) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: highlow["flagged"] = True @@ -889,7 +889,7 @@ def get_day_for_user(self, uid, date, viewer, supports_html=False): highlow = cursor.fetchone() - if highlow == None: + if highlow is None: conn.commit() conn.close() @@ -911,10 +911,10 @@ def get_day_for_user(self, uid, date, viewer, supports_html=False): highlow['low'] = self.html_to_plain_text(highlow['low']) cursor.execute( "SELECT * FROM likes WHERE uid='{}' AND highlowid='{}'".format(viewer, highlow["highlowid"]) ) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: highlow["liked"] = True cursor.execute("SELECT * FROM flags WHERE uid='{}' AND highlowid='{}'".format(viewer, highlow["highlowid"])) - if cursor.fetchone() != None: + if cursor.fetchone() is not None: highlow["flagged"] = True @@ -972,7 +972,7 @@ def delete_comment(self, uid, commentid): conn.close() def update_comment(self, uid, commentid, message): - if message == None or message == "": + if message is None or message == "": return '{ "error": "no-message" }' diff --git a/services/User.py b/services/User.py index 9d11682..b6cf143 100644 --- a/services/User.py +++ b/services/User.py @@ -40,7 +40,7 @@ def __init__(self, uid, host, username, password, database): conn.close() #Make sure the user existed in the first place - if user == None: + if user is None: raise ValueError("user-no-exist") #Otherwise, get all the data and store it @@ -412,7 +412,7 @@ def is_friend_with(self, uid): conn.close() - if row == None: + if row is None: return '{"isFriend": false}' return '{"isFriend": true}'