def gainXp(arguments): errorCode = None username = arguments[0] reason = arguments[1] amount = arguments[2] #currentXP = executeSql("SELECT xp FROM `users` WHERE username = '******'", # username)[0]['xp'] currentXp = executeMDb('users', 'find', {'username': username}) newXp = currentXp + amount #print(currentXP[0]['xp']) #if executeSql("UPDATE users SET xp='{}' WHERE username='******'", # (newXP, username)) == (): if executeMDb('users', 'update', { 'username': username, '$set': { 'xp': newXp } }) != 1: log.writeLog(username, 'info', 'Gained {} xp. Reason: {}'.format(amount, reason)) errorCode = 1 else: errorCode = 0 return errorCode
def calculateLinkRating(arguments): link = arguments if isinstance(arguments, tuple): link = arguments[0] #print(link) errorCode = None #1: ok; 2: url invalid; 3: error while updating allLinkRating = 0 allLinkRatingCount = 0 #result = databaseConnection.executeSql( # "SELECT `link`, `rating` FROM `inputs` WHERE `link`='{}'", link) result = databaseConnection.executeMDb('inputs', 'find', {'link': link}, 'all')['dbReturn'] #print(result[0]) #print(result[1]) #print(result[2]) for row in result: #print(float(row['rated'])) allLinkRating = allLinkRating + float(row['rated']) allLinkRatingCount = allLinkRatingCount + 1 if allLinkRating == None or allLinkRating == 0 or allLinkRating == '' or allLinkRatingCount == None or allLinkRatingCount == 0 or allLinkRatingCount == '': rating = 0 else: rating = allLinkRating / allLinkRatingCount #main rating for this link isInDbCode = check.IfisInRatingsDb(link) #print('isindb: ' + str(isInDbCode)) if isInDbCode == 1: #updates link's entry #print(rating) #print(allLinkRating) #databaseConnection.executeSql( # "UPDATE ratings SET rating='{}', allLinkRating='{}', allLinkRatingCount='{}' WHERE link='{}'", # (rating, allLinkRating, allLinkRatingCount, link)) if databaseConnection.executeMDb('ratings', 'update', [{ 'link': link }, { '$set': { 'rating': rating, 'allLinkRating': allLinkRating, 'allLinkRatingCount': allLinkRatingCount } }])['errorCode'] != 1: errorCode = 3 return {'errorCode': errorCode} elif isInDbCode == 2: #inserts new entry for new link databaseConnection.executeMDb( 'ratings', 'insert', { 'link': link, 'rating': rating, 'allLinkRating': allLinkRating, 'allLinkRatingCount': allLinkRatingCount }) #databaseConnection.executeSql( # "INSERT INTO ratings (link,rating) VALUES ('{}', '{}')", # (link, rating)) errorCode = 1 return {'errorCode': errorCode}
def registerUser(username, password): errorCode = 0 if userExists(username) != 1: executeMDb('users', 'insert', { 'username': username, 'password': password, 'role': 'user', 'xp': 0 }) errorCode = 1 else: errorCode = 2 #executeSql("INSERT INTO `users` (`username`,`password`, `role`, `xp`) VALUES ('{}','{}','{}','{}')",(username, password, 'user', '0')) return errorCode
def getLinkRating(link): errorCode = None rating = -1 if check.IfisInRatingsDb(link): rating = databaseConnection.executeMDb('ratings', 'find', { 'link': link })['dbReturn'][0]['rating'] errorCode = 1 else: errorCode = 0 return {'errorCode': errorCode, 'rating': rating}
def userExists(username): errorCode = None #0: unknown; 1:is in db; 2: isnt in db try: fetchedResults = executeMDb('users', 'find', {'username': username})['dbReturn'][0] #print('result: ' + str(fetchedResults)) if not fetchedResults or fetchedResults == None or fetchedResults == '': errorCode = 2 else: errorCode = 1 except IndexError: errorCode = 2 except: #print("Unexpected error:", str(sys.exc_info()[0])) errorCode = 0 return errorCode
def getTopLinkRatings(): errorCode = None links = [] howMany = 5 if check.DbIsNotEmpty('ratings'): result = databaseConnection.executeMDb('ratings', 'find', {}, 'all', ['allLinkRating', -1]) #print('res: ' + str(result)) #result = databaseConnection.executeSql( # "SELECT * FROM `ratings` ORDER BY allLinkRating DESC", '', 3) i = 0 for row in result['dbReturn']: links.append(row['link']) i = i + 1 if i >= howMany: break errorCode = 1 return {'errorCode': errorCode, 'links': links}
def UsernameAndPasswordValidity(username, password): #ConnectionToDb() errorCode = None # 0: unknown; 1:username+pw correct; 2: no user with this name; 3: pw wrong fetchedResult = None #fetchedResults = executeSql("SELECT * FROM `users` WHERE `username`='{}'",username) try: fetchedResult = executeMDb('users', 'find', {'username': username})['dbReturn'][0] except IndexError: errorCode = 2 return errorCode except: errorCode = 0 return errorCode if fetchedResult['password'] == password: errorCode = 1 else: errorCode = 3 return errorCode
def IfisInRatingsDb(link): #ConnectionToDb() errorCode = None #0: unknown; 1:is in db; 2: isnt in db try: fetchedResults = executeMDb('ratings', 'find', {'link': link})['dbReturn'][0] #print('result: ' + str(fetchedResults)) if not fetchedResults or fetchedResults == None or fetchedResults == '': errorCode = 2 else: errorCode = 1 except IndexError: errorCode = 2 except: print("Unexpected error:", str(sys.exc_info()[0])) errorCode = 0 #fetchedResults = executeSql("SELECT * FROM `ratings` WHERE `link`='{}'", # link) return errorCode
def getUseridFromUsername(username): return executeMDb('users', 'find', {'username': username})['dbReturn'][0]['userid']
def getUsernameFromId(userid): return executeMDb('users', 'find', {'id': userid})['dbReturn'][0]['username']
def getRawUserdataFromDb(username): return executeMDb('users', 'find', {'username': username})['dbReturn'][0]
def addRating(arguments): #funNum: 1 # 1: ok | 2: error in calculate # 3: invalid arg type 4: invalid rating # 5: invalid link | 6: error in addrating # 7: error while updating errorCode = None username = arguments[0] link = arguments[1] rated = arguments[2] allLinkRating = 0 allLinkRatingCount = 0 rating = 0 #convert rating try: rated = int(rated) except: errorCode = 3 return {'errorCode': errorCode} if not check.RatingValidity(rated) == 1: errorCode = 4 return {'errorCode': errorCode} if not check.UrlValidity(link) == 1: errorCode = 5 return {'errorCode': errorCode} #userid = databaseConnection.executeSql( #get user id # "SELECT `id` FROM `users` WHERE `username`='{}'", username)[0]['id'] #addRating if databaseConnection.executeMDb('inputs', 'insert', { 'username': username, 'link': link, 'rated': rated })['errorCode'] != 1: errorCode = 6 return {'errorCode': errorCode} #databaseConnection.executeSql( #add new input to db # "INSERT INTO `inputs` ( `username`,`link`,`rating`) VALUES ('{}', '{}', '{}')", # (username, link, rated)) #calculateRating isInDbCode = check.IfisInRatingsDb(link) #print('isindb: ' + str(isInDbCode)) if isInDbCode == 1: #updates link's entry #print('updating link\'s entry') #allLinkRatingAndCount = databaseConnection.executeSql( # "SELECT `allLinkRating`,`allLinkRatingCount` FROM `ratings` WHERE `link`='{}'", # link) allLinkRatingAndCount = databaseConnection.executeMDb( 'ratings', 'find', { 'link': link })['dbReturn'] #print(allLinkRatingAndCount[0]['allLinkRating']) allLinkRating = allLinkRatingAndCount[0]['allLinkRating'] allLinkRatingCount = allLinkRatingAndCount[0]['allLinkRatingCount'] #print(allLinkRating) if allLinkRating == None or allLinkRatingCount == None: #if link exists but has no alllinkrating/count allLinkRating = 0 allLinkRatingCount = 0 result = databaseConnection.executeMDb( 'inputs', 'find', {'link': link}, 'all')['dbReturn'] #result = databaseConnection.executeSql( # "SELECT `link`, `rating` FROM `inputs` WHERE `link`='{}'", # link) #CONTINUE HERE RERERERERREEREEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE (check if ratingcount is proper) #print(allLinkRatingCount) for row in result: allLinkRating = allLinkRating + float(row['rating']) allLinkRatingCount = allLinkRatingCount + 1 rating = allLinkRating / allLinkRatingCount #main rating for this link if databaseConnection.executeMDb( 'ratings', 'insert', { 'link': link, 'rating': rating, 'allLinkRating': allLinkRating, 'allLinkRatingCount': allLinkRatingCount })['errorCode'] != 1: errorCode = 7 return {'errorCode': errorCode} #databaseConnection.executeSql( # "INSERT INTO ratings (link,rating,allLinkRating,allLinkRatingCount) VALUES ('{}', '{}', '{}', '{}')", # (link, rating, allLinkRating, allLinkRatingCount)) else: #link exists and alllinkrating (+count) need to be updated #print('alllinkr ' + str(allLinkRating)) newAllLinkRating = allLinkRating + rated newAllLinkRatingCount = allLinkRatingCount + 1 #print('newalllinkr' + str(newAllLinkRating)) #raise debugMe('debug') if allLinkRating == None or allLinkRating == 0 or allLinkRating == '' or allLinkRatingCount == None or allLinkRatingCount == 0 or allLinkRatingCount == '': #print('ree somtingwong') calculateLinkRating(link) else: #print('allgoodmyboy') rating = (newAllLinkRating) / (newAllLinkRatingCount) #print('rating: ' + str(rating)) if databaseConnection.executeMDb( 'ratings', 'update', [{ 'link': link }, { '$set': { 'rating': rating, 'allLinkRating': newAllLinkRating, 'allLinkRatingCount': newAllLinkRatingCount } }])['errorCode'] != 1: errorCode = 7 #return {'errorCode': errorCode} #databaseConnection.executeSql( # "UPDATE ratings SET `rating`='{}', `allLinkRating`='{}', `allLinkRatingCount`='{}' WHERE link='{}'", # (rating, newAllLinkRating, newAllLinkRatingCount, link)) #print('yay me is done :)') errorCode = 1 elif isInDbCode == 2: # recalculate from all input and insert new entry for new link #print('recalculating link from all input') result = databaseConnection.executeMDb( 'inputs', 'find', {'link': link}, 'all')['dbReturn'] #print(type(result)) #result = databaseConnection.executeSql( # "SELECT `link`, `rating` FROM `inputs` WHERE `link`='{}'", link) for row in result: allLinkRating = allLinkRating + float(row['rated']) allLinkRatingCount = allLinkRatingCount + 1 rating = allLinkRating / allLinkRatingCount #main rating for this link if databaseConnection.executeMDb( 'ratings', 'insert', { 'link': link, 'rating': rating, 'allLinkRating': allLinkRating, 'allLinkRatingCount': allLinkRatingCount })['errorCode'] != 1: errorCode = 5 #return {'errorCode': errorCode} #databaseConnection.executeMDb('ratings','insert',{}) #databaseConnection.executeSql( # "INSERT INTO ratings (link,rating,allLinkRating,allLinkRatingCount) VALUES ('{}', '{}', '{}', '{}')", # (link, rating, allLinkRating, allLinkRatingCount)) errorCode = 1 else: #print('error while checking if is in ratingdb') errorCode = 0 #print('now returning ' + str(errorCode)) return {'errorCode': errorCode}
def DbIsNotEmpty(table): result = executeMDb(table, 'find', {})['dbReturn'][0] return result != None and result != ''