示例#1
0
def create_user(username, password, first_name, last_name, email):
    conn = _database.connection()

    with conn.cursor() as cursor:
        sql = "INSERT INTO users (username, email, first_name, last_name, password) VALUE (%s, %s, %s, %s, MD5(%s));"
        cursor.execute(sql, (username, email, first_name, last_name, password))
        result = cursor.fetchone()
        conn.close()

    return result
示例#2
0
def search(query='', limit=3):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM elements_search WHERE name LIKE %s LIMIT %s;"
            cursor.execute(sql, ('%' + query + '%', limit))
            result = cursor.fetchall()

    finally:
        conn.close()

    return result
示例#3
0
def user_exist(username, password):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT COUNT(*) AS count, id FROM users WHERE username = %s AND password = MD5(%s)"
            cursor.execute(sql, (username, password))
            result = cursor.fetchone()

    finally:
        conn.close()

    return result
示例#4
0
def getRandomElements(nb):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM elements WHERE picture IS NOT NULL ORDER BY RAND() LIMIT %s;"
            cursor.execute(sql, (nb))
            result = cursor.fetchall()

    finally:
        conn.close()

    return result
示例#5
0
def getLastReview(nb):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM elements_evaluate ORDER BY created DESC LIMIT %s;"
            cursor.execute(sql, (nb))
            result = cursor.fetchall()

    finally:
        conn.close()

    return result
示例#6
0
def get_user(id):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT * FROM users WHERE id = %s"
            cursor.execute(sql, (id))
            result = cursor.fetchone()
            conn.close()
    except:
        abort(404)

    return result
示例#7
0
文件: game.py 项目: Escobaj/db
def rate_game(id, user_id, rate):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = " INSERT INTO evaluate_game (`users_id`, `games_id`, `comment`, `rate`) VALUE (%s, %s, NULL, %s) \
			        ON DUPLICATE KEY UPDATE `rate` = %s;"
			cursor.execute(sql, (user_id, id, rate, rate))
			cursor.fetchone()
			result = True
	except Exception as e:
		result = False
	finally:
		cursor.close()
		conn.close()
	
	return result
示例#8
0
文件: game.py 项目: Escobaj/db
def get_user_review(id, user_id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT comment, rate \
  					  FROM evaluate_game \
    					WHERE users_id = %s AND games_id = %s;"
			cursor.execute(sql, (user_id, id))
			result = cursor.fetchone()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	finally:
		conn.close()
	
	return result
示例#9
0
def delete_rate(movie_id, user_id):
    conn = _database.connection()
    result = 0

    try:
        with conn.cursor() as cursor:
            sql = "UPDATE evaluate_movie SET rate = NULL WHERE movies_id = %s AND users_id = %s;"
            cursor.execute(sql, (movie_id, user_id))
            result = cursor.fetchall()
            cursor.close()

    except Exception as e:
        flash('The database is unavailable', 'error')

    finally:
        conn.close()

    return result
示例#10
0
文件: game.py 项目: Escobaj/db
def get_genres(id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name \
					  FROM genres \
					    JOIN genre_game gm ON genres.id = gm.genres_id \
					      WHERE games_id = %s;"
			cursor.execute(sql, id)
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	finally:
		conn.close()
	
	return result
示例#11
0
def get_nb_comments(identifiant):
    conn = _database.connection()
    result = 0

    try:
        with conn.cursor() as cursor:
            sql = "SELECT count(*) as total FROM evaluate_movie WHERE comment IS NOT NULL AND movies_id = %s;"
            cursor.execute(sql, identifiant)
            result = cursor.fetchone()['total']
            cursor.close()

    except Exception as e:
        flash('The database is unavailable', 'error')

    finally:
        conn.close()

    return result
示例#12
0
def get_character(id):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT fullname, picture \
					  FROM characters \
					    WHERE id = %s;"

            cursor.execute(sql, (id))
            result = cursor.fetchone()
    except Exception as e:
        result = ()
        flash('The database is unavailable', 'error')
    finally:
        conn.close()

    return result
示例#13
0
def get_total_movie_number():
    conn = _database.connection()
    result = 0

    try:
        with conn.cursor() as cursor:
            sql = "SELECT COUNT(*) AS total FROM movies"
            cursor.execute(sql)
            result = cursor.fetchone()['total']
            cursor.close()

    except Exception as e:
        flash('The database is unavailable', 'error')
        flash(e, 'error')

    finally:
        conn.close()

    return result
示例#14
0
文件: game.py 项目: Escobaj/db
def get_casting(id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT characters_id as id, fullname, name AS role, picture \
					  FROM character_game \
					    JOIN characters c ON character_game.characters_id = c.id \
					    JOIN roles r ON character_game.roles_id = r.id \
					      WHERE games_id = %s;"
			cursor.execute(sql, id)
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	finally:
		conn.close()
	
	return result
示例#15
0
文件: game.py 项目: Escobaj/db
def get_reviews(id, nombre, page):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT u.id AS id, username, comment \
					  FROM evaluate_game \
					    JOIN games m ON evaluate_game.games_id = m.id \
					    JOIN users u ON evaluate_game.users_id = u.id \
					      WHERE games_id = %s LIMIT %s, %s;"
			cursor.execute(sql, (id, int(page) * int(nombre), int(nombre)))
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	finally:
		conn.close()
	
	return result
示例#16
0
文件: game.py 项目: Escobaj/db
def show_game(id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT *, avg(rate) AS moyenne \
					  FROM games \
					     LEFT JOIN evaluate_game em ON games.id = em.games_id \
					      WHERE id = %s \
					        GROUP BY games_id;"
			cursor.execute(sql, id)
			result = cursor.fetchone()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
	
	return result
示例#17
0
文件: game.py 项目: Escobaj/db
def get_most_interested_games(nombre, page=0):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name, picture, COUNT(*) AS nombre, release_year, description \
					  FROM games \
					    JOIN wish_list_games wlm ON games.id = wlm.games_id \
					      GROUP BY id \
					        ORDER BY COUNT(*) DESC \
					          LIMIT %s, %s;"
			cursor.execute(sql, (page * nombre, nombre))
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
	
	return result
示例#18
0
文件: game.py 项目: Escobaj/db
def get_top_rated_games(nombre, page=0):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name, ROUND(AVG(rate), 1) AS moyenne, picture, release_year, description \
					  FROM evaluate_game \
					    JOIN games m ON evaluate_game.games_id = m.id \
					      GROUP BY id \
					        ORDER BY moyenne DESC \
					          LIMIT %s, %s;"
			cursor.execute(sql, (int(page) * int(nombre), int(nombre)))
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash('The database is unavailable', 'error')
		flash(e, 'error')
	finally:
		conn.close()
	
	return result
示例#19
0
文件: game.py 项目: Escobaj/db
def get_user_wishlist(game_id, user_id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT COUNT(*) AS total \
  						FROM wish_list_games \
    						WHERE users_id = %s AND games_id = %s;"
			cursor.execute(sql, (user_id, game_id))
			row = cursor.fetchone()
			if row['total'] == 1:
				result = True
			else:
				result = False
			cursor.close()
	
	except Exception as e:
		result = False
	finally:
		conn.close()
	
	return result
示例#20
0
文件: game.py 项目: Escobaj/db
def wishlist_game(game_id, user_id):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "INSERT INTO wish_list_games (users_id, games_id) VALUES (%s, %s)"
			cursor.execute(sql, (user_id, game_id))
			cursor.close()
			flash('This game have been added to your wishlist', 'success')
	
	except Exception as e:
		try:
			with conn.cursor() as cursor:
				sql = "DELETE FROM wish_list_games WHERE users_id = %s AND games_id = %s;"
				cursor.execute(sql, (user_id, game_id))
				cursor.close()
				flash('This game have been removed from your wishlist', 'success')
		except Exception:
			flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
示例#21
0
文件: game.py 项目: Escobaj/db
def get_most_commented_games(nombre, page=0):
	conn = _database.connection()
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name, picture, release_year, description \
					  FROM evaluate_game \
					    JOIN games m ON evaluate_game.games_id = m.id \
					      WHERE comment IS NOT NULL \
					        GROUP BY id \
					          ORDER BY COUNT(*) DESC \
					            LIMIT %s, %s;"
			cursor.execute(sql, (page * nombre, nombre))
			result = cursor.fetchall()
	except Exception as e:
		result = ()
		flash(e, 'error')
		flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
	
	return result
示例#22
0
文件: game.py 项目: Escobaj/db
def get_all_comment(id):
	conn = _database.connection()
	result = 0
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name, comment, picture, release_year, 'game' AS type FROM evaluate_game \
					JOIN games m ON evaluate_game.games_id = m.id WHERE users_id = %s AND comment IS NOT NULL AND comment != '' \
				   UNION SELECT id, name, comment, picture, release_year, 'game' AS type FROM evaluate_game \
				    JOIN games m ON evaluate_game.games_id = m.id WHERE users_id = %s AND comment IS NOT NULL AND comment != '' \
				   UNION SELECT id, name, comment, picture, `release`, 'serie' AS type FROM evaluate_serie \
				    JOIN series m ON evaluate_serie.series_id = m.id WHERE users_id = %s AND comment IS NOT NULL AND comment != ''"
			cursor.execute(sql, (id, id, id))
			result = cursor.fetchall()
			cursor.close()
	
	except Exception as e:
		flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
	
	return result
示例#23
0
文件: game.py 项目: Escobaj/db
def get_all_wishlisted(id):
	conn = _database.connection()
	result = 0
	
	try:
		with conn.cursor() as cursor:
			sql = "SELECT id, name, release_year, 'game' AS type, picture FROM wish_list_games \
					JOIN games m ON wish_list_games.games_id = m.id WHERE users_id = %s \
				   UNION SELECT id, name, `release`, 'serie' AS type, picture FROM wish_list_series \
				   JOIN series s ON wish_list_series.series_id = s.id WHERE users_id = %s \
				   UNION SELECT id, name, release_year, 'game' AS type, picture FROM wish_list_games \
				   JOIN games g ON wish_list_games.games_id = g.id WHERE users_id = %s;"
			cursor.execute(sql, (id, id, id))
			result = cursor.fetchall()
			cursor.close()
	
	except Exception as e:
		flash('The database is unavailable', 'error')
	
	finally:
		conn.close()
	
	return result
示例#24
0
def get_all_elements(id):
    conn = _database.connection()

    try:
        with conn.cursor() as cursor:
            sql = "SELECT id, name, 'serie' AS type, picture \
					FROM series \
					  JOIN character_serie cs ON series.id = cs.series_id \
					WHERE characters_id = %s \
					UNION \
					SELECT id, name, 'movie' AS type, picture \
					FROM movies \
					  JOIN character_movie m ON movies.id = m.movies_id \
					WHERE characters_id = %s;"

            cursor.execute(sql, (id, id))
            result = cursor.fetchall()
    except Exception as e:
        result = ()
        flash('The database is unavailable', 'error')
    finally:
        conn.close()

    return result