Add support for db export

This commit is contained in:
Arti Zirk 2020-10-01 21:53:39 +03:00
parent 472c762de2
commit a9abf1eba0
2 changed files with 26 additions and 5 deletions

View File

@ -21,6 +21,10 @@ if __name__ == "__main__":
from .db import import_ookean from .db import import_ookean
import_ookean() import_ookean()
sys.exit(1) sys.exit(1)
elif cmd == "export_db":
from .db import export_db
export_db()
sys.exit(1)
else: else:
try: try:
port = int(sys.argv[1]) port = int(sys.argv[1])

View File

@ -193,11 +193,18 @@ class DB:
for user in unmatched: for user in unmatched:
print(f"User {user}") print(f"User {user}")
def export_db(self):
users = {}
def export_db(self, json_file): cur = self.db.execute("select id, distinguished_name, user, full_name, email, disabled, admin from users")
for user in self.list_users(): for user in cur.fetchall():
pass users[user["full_name"]] = dict(user)
del users[user["full_name"]]["id"]
cards_cur = self.db.execute(
"select card_uid, name, created, disabled from keycards where user_id = ?",
(user["id"],)
)
users[user["full_name"]]["keycards"] = [dict(keycard) for keycard in cards_cur.fetchall()]
return users
def initdb(): def initdb():
@ -222,5 +229,15 @@ def import_ookean():
db.import_ookean("../contrib/ookean_cards.json") db.import_ookean("../contrib/ookean_cards.json")
def export_db():
dbfile = "kdoorweb.sqlite"
db = DB(dbfile)
import sys
exports = db.export_db()
json.dump(exports, sys.stdout, indent=2)
sys.stdout.write("\n")
if __name__ == "__main__": if __name__ == "__main__":
initdb() initdb()