From 9e40c297b4a4fccdab9a2fc16189553cd90fac87 Mon Sep 17 00:00:00 2001 From: guimoz Date: Tue, 31 Jan 2017 14:17:50 +0100 Subject: [PATCH] =?UTF-8?q?migration=20=C3=A0=20mysql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- import_bdd.py | 15 +++++--- roulette.py | 103 +++++++++++++++----------------------------------- 2 files changed, 40 insertions(+), 78 deletions(-) diff --git a/import_bdd.py b/import_bdd.py index 1879fbf..772d6a7 100644 --- a/import_bdd.py +++ b/import_bdd.py @@ -1,14 +1,16 @@ -import sqlite3 +import MySQLdb from users.models import User from machines.models import Machine,Interface -SQLITE_FILENAME = '/var/www/re2o/players.db' +def connect_sql(): + return MySQLdb.connect(host="roulette.rez", # your host, usually localhost + user="hydra-user", # your username + passwd="q5kBLchS6ooAZDSxMeJG2gdf4gmJcfS5", # your password + db="roulette") # name of the data base -def connect_sqlite(): - return sqlite3.connect(SQLITE_FILENAME) # Connexion à la base SQLite locale -con = connect_sqlite() +con = connect_sql() cur = con.cursor() # cur.execute('''create table players (id,prenom,nom, etat)''') # cur.execute('''create table machines (id,uid_user,ip)''') @@ -24,9 +26,10 @@ for user in User.objects.filter(school=1): if user.has_access() and (user.is_adherent() or user.end_adhesion()): if user.uid_number not in players: cur.execute("""insert into players values (?,?,?,?)""",(user.uid_number, user.name, user.surname, 0)) + print(user.surname+' '+user.name) for m in Machine.objects.filter(user= user): for i in Interface.objects.filter(machine = m): if i.ipv4.ipv4 not in machines: cur.execute("""insert into machines values (?,?,?) """,(i.id, user.uid_number, i.ipv4.ipv4)) -con.commit() + con.close() diff --git a/roulette.py b/roulette.py index 8c6794e..1103f49 100644 --- a/roulette.py +++ b/roulette.py @@ -21,11 +21,6 @@ ASSHOLES = [] SQLITE_FILENAME = '/var/roulette/players.db' SQLITE_SCHEMA = 'schema.sql' -MYSQL_HOST = 'mysql.rez' -MYSQL_USER = 'rezo_admin_ro' -MYSQL_PASSWORD = 'rezopaspipo' -MYSQL_DB = 'rezo_admin' - BAN_DURATION = 30. * 60. IMMUNITY_FILE = '/var/roulette/immunity' @@ -46,51 +41,15 @@ locale.setlocale(locale.LC_ALL, 'fr_FR.utf8') def connect_sqlite(): return sqlite3.connect(SQLITE_FILENAME) +def connect_sql(): + return MySQLdb.connect(host="localhost", # your host, usually localhost + user="root", # your username + passwd="F5XUaZmkWaWp3GDnT73WuEGZqAN5hMF3", # your password + db="roulette") # name of the data base + def init_db(): # Initialisation de la base SQLite - with closing(connect_sqlite()) as con_sqlite: - with app.open_resource('schema.sql') as f: - con_sqlite.cursor().executescript(f.read().decode("utf-8")) - con_sqlite.commit() - - # Connexion à la base SQLite locale - con_sqlite = connect_sqlite() - cur_sqlite = con_sqlite.cursor() - - # Connexion à la base MySQL sur babel - con_mysql = mdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB, \ - charset='utf8', use_unicode=True) - cur_mysql = con_mysql.cursor(mdb.cursors.DictCursor) - - # Remplissage de la table players à partir de la table utilisateurs - cur_mysql.execute("""select id,prenom,nom from utilisateurs - where etat='STATE_ACTIVE' and ecole_id=1 and id<>1 - and typeUtilisateur='membre'""") - rows = cur_mysql.fetchall() - print('players :') - for row in rows: - if row['prenom'] + ' ' + row['nom'] not in IMMUNITY: - print(row) - cur_sqlite.execute("""insert into players values (?,?,?,?)""", \ - ((row["id"]), row["prenom"], row["nom"], 0)) - - # Remplissage de la table ip à partir de la table equipements - cur_mysql.execute("""select equipements.id,utilisateurs.id,equipements.ip - from utilisateurs - inner join equipements on utilisateurs.id=equipements.utilisateur_id - where utilisateurs.ecole_id=1 and utilisateurs.id<>1 - and utilisateurs.etat='STATE_ACTIVE' and equipements.etat='STATE_ACTIVE' - and utilisateurs.typeUtilisateur='membre'""") - rows = cur_mysql.fetchall() - print('machines :') - for row in rows: - print(row) - cur_sqlite.execute("""insert into machines values (?,?,?)""", \ - (row["id"], row["utilisateurs.id"], row["ip"])) - - con_sqlite.commit() - cur_sqlite.close() - cur_mysql.close() + pass def duration_format(seconds): hours = seconds // 3600 @@ -110,8 +69,8 @@ def get_ip(): return request.remote_addr def get_player(player_id): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select id,firstname,name,ban_end from players where id=(?)""", [player_id]) @@ -122,8 +81,8 @@ def get_player(player_id): return {'id': row[0], 'firstname': row[1], 'name': row[2], 'ban_end': row[3]} def get_player_from_ip(ip): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select players.id,players.firstname,players.name, machines.id,machines.ip,players.ban_end @@ -143,8 +102,8 @@ def get_player_from_ip(ip): return user def get_player_from_full_name(firstname, name): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select players.id,players.firstname,players.name, machines.id,machines.ip,players.ban_end @@ -163,8 +122,8 @@ def get_player_from_full_name(firstname, name): return user def is_banned(user_id): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select ban_end from players where id=(?)""", [user_id]) @@ -190,8 +149,8 @@ def playable_required(f): return decorated_function def get_players_not_banned(): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select id,firstname,name from players where (?) > ban_end """, [time()]) @@ -238,16 +197,16 @@ def ban(player_id, target_id, success): banned_player = success and target or player - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select id,ban_end from players where id=(?)""", [banned_player['id']]) con.commit() con.close() - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() ban_end = cur.fetchone()[0] ban_end = time() + BAN_DURATION @@ -258,8 +217,8 @@ def ban(player_id, target_id, success): con.commit() con.close() - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""insert into bans (player_id,target_id,success,time) values (?,?,?,?)""", [player['id'], target['id'], \ success and 1 or 0, time()]) @@ -268,8 +227,8 @@ def ban(player_id, target_id, success): def unban(player_id): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""update players set ban_end=(?) where id=(?)""", [time() - BAN_DURATION, player_id]) @@ -278,8 +237,8 @@ def unban(player_id): con.close() def get_bans(player_id): - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() # Bannissements concernant le joueur : cur.execute("""select player_id,target_id,success,time from bans @@ -313,8 +272,8 @@ def banned(): def statistiques(): #Calcul des statistiques - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select firstname,name,ban_end from players""") @@ -348,8 +307,8 @@ def banned_ip(): if get_ip() not in ['10.7.0.254']: abort(403) - con = connect_sqlite() - cur = con.cursor() + con = connect_sql() + cur = db.cursor() cur.execute("""select machines.ip from players inner join machines on players.id=machines.player_id