-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.py
52 lines (49 loc) · 1.66 KB
/
delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sqlite3
import os
import getpass
import hashlib
from view import fetch
def delete(username):
conn = sqlite3.connect("userdecks.db")
conn2 = sqlite3.connect("users.db")
cursor = conn.cursor()
cursor2 = conn2.cursor()
print()
decks = fetch(cursor, username)
i = 1
index = {}
for deck in decks:
fname = deck[0].lower().replace(" ", "_")
try:
open(f"./decks/{fname}.deck").close()
except:
cursor.execute(f"DELETE FROM userdecks WHERE deck = '{deck[0]}'")
else:
print(f"{i}. {deck[0]}")
index[i] = deck[0]
i += 1
try:
deck_number = int(input("Enter deck number to delete: "))
except:
print("Deck number must be an integer!")
else:
try:
deck = index[deck_number]
except:
print("Invalid deck number!")
else:
password = getpass.getpass(
"Enter your password to confirm deletion: ")
cursor2.execute(
f"SELECT * FROM users WHERE username = '{username}'")
details = cursor2.fetchone()
password = hashlib.sha512(
bytes(password, "utf-8")).hexdigest()
if password == details[1]:
cursor.execute(f"DELETE FROM userdecks WHERE deck = '{deck}'")
os.remove(f"./decks/{deck.lower().replace(' ', '_')}.deck")
print(f"Successfully deleted deck {deck}.\n")
else:
print("Could not delete deck because incorrect password was provided.")
conn.commit()
conn.close()