roulette/import_bdd.py

36 lines
1.4 KiB
Python
Raw Normal View History

2017-01-31 13:17:50 +00:00
import MySQLdb
2017-01-31 11:06:02 +00:00
from users.models import User
2017-01-30 21:27:00 +00:00
from machines.models import Machine,Interface
2017-01-31 13:17:50 +00:00
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
2017-01-30 21:27:00 +00:00
# Connexion à la base SQLite locale
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 11:06:02 +00:00
cur = con.cursor()
# cur.execute('''create table players (id,prenom,nom, etat)''')
# cur.execute('''create table machines (id,uid_user,ip)''')
cur.execute("""select id from players""")
players = cur.fetchall()
players = [player[0] for player in players]
cur.execute("""select ip from machines""")
machines = cur.fetchall()
machines = [machine[0] for machine in machines]
2017-01-30 21:27:00 +00:00
for user in User.objects.filter(school=1):
2017-01-31 11:06:02 +00:00
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))
2017-01-31 13:17:50 +00:00
print(user.surname+' '+user.name)
2017-01-30 21:27:00 +00:00
for m in Machine.objects.filter(user= user):
for i in Interface.objects.filter(machine = m):
2017-01-31 11:06:02 +00:00
if i.ipv4.ipv4 not in machines:
cur.execute("""insert into machines values (?,?,?) """,(i.id, user.uid_number, i.ipv4.ipv4))
2017-01-31 13:17:50 +00:00
2017-01-31 11:06:02 +00:00
con.close()