Commit 7e0483e8 authored by Jonathan Schaeffer's avatar Jonathan Schaeffer

Trying to better delete accounts

parent c0330ce6
......@@ -139,6 +139,9 @@ def cleanup():
Clean old temporary logins and passwords in both databases.
"""
logging.info("Cleaning up expired temporary accounts")
nb_expired_accounts = 0
old_users_entries = 0
old_users = ""
try:
conn = psycopg2.connect(dbname= application.config['AUTHDBNAME'],
port = application.config['AUTHDBPORT'],
......@@ -147,6 +150,10 @@ def cleanup():
password = application.config['AUTHDBPASSWORD'])
cur = conn.cursor()
logging.debug("Connected to users database")
cur.execute("select user_index,login from users where expires_at < now();")
nb_expired_accounts = cur.rowcount
old_users_entries = cur.fetchall()[:1000]
old_users = ','.join( str(u[0]) for u in old_users_entries )
except Exception as e:
logging.error("Unable to connect to database %s as %s@%s:%s"%(application.config['AUTHDBNAME'],
application.config['AUTHDBUSER'],
......@@ -154,10 +161,6 @@ def cleanup():
application.config['AUTHDBPORT']))
raise e
cur.execute("select user_index,login from users where expires_at < now();")
nb_expired_accounts = cur.rowcount
old_users_entries = cur.fetchall()[:1000]
old_users = ','.join( str(u[0]) for u in old_users_entries )
logging.debug("%d users to delete"%(len(old_users_entries)))
if len(old_users_entries) > 0 :
try :
......@@ -177,19 +180,19 @@ def cleanup():
password = application.config['PRIVILEGEDBPASSWORD'])
cur = conn.cursor()
logging.debug("Connected to privlieges database")
old_users = ','.join(str(u[1]) for u in old_users_entries)
logging.debug("Deleting from privileges database: %s", old_users)
cur.execute("""
delete from eida_temp_users where name in (%s);
""", (AsIs(old_users),))
conn.commit()
conn.close()
except Exception as e:
logging.error("Unable to connect to database %s as %s@%s:%s"%(application.config['PRIVILEGEDBNAME'],
application.config['PRIVILEGEDBUSER'],
application.config['PRIVILEGEDBHOST'],
application.config['PRIVILEGEDBPORT']))
raise e
logging.debug("Deleting from privileges database")
old_users = ','.join(str(u[1]) for u in old_users_entries)
cur.execute("""
delete from eida_temp_users where name in (%s);
""", (old_users,))
conn.commit()
conn.close()
logging.info("Deleted %d over %d expired accounts"%(len(old_users_entries), nb_expired_accounts))
return Response("Deleted %d expired accounts."%(len(old_users_entries)), status=200)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment