Skip to content

Commit e88e944

Browse files
📝 Add docstrings to blatt10
Docstrings generation was requested by @GalacticCodeGambit. * #105 (comment) The following files were modified: * `project/backend/Datenbank.py` * `project/backend/LazyCookVerwaltung.py`
1 parent a4ff779 commit e88e944

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

project/backend/Datenbank.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def get_connection(self):
2020
return con
2121

2222
def __create_tables_if_not_exist(self):
23+
"""
24+
Create the required SQLite tables for the application's schema if they do not already exist.
25+
26+
Executes CREATE TABLE IF NOT EXISTS statements for all application tables and commits the transaction on success. On error, prints the exception message and rolls back the transaction.
27+
"""
2328
try:
2429
self.db.execute("""
2530
CREATE TABLE IF NOT EXISTS Zutat (
@@ -89,6 +94,17 @@ def __create_tables_if_not_exist(self):
8994
self.con.rollback()
9095

9196
def addNutzer(self, email, salt, passwort):
97+
"""
98+
Register a new account by inserting a row into the Konto table if the email is not already registered.
99+
100+
Parameters:
101+
email (str): The account email address to register.
102+
salt (str): The salt value associated with the account password.
103+
passwort (str): The account password (expected to be already hashed or prepared).
104+
105+
Returns:
106+
str: "Registrierung erfolgreich" if a new Konto row was inserted, or "Email schon in einem Konto registriert" if the email already exists.
107+
"""
92108
con = self.get_connection()
93109

94110
db = con.cursor()
@@ -117,6 +133,15 @@ def addNutzer(self, email, salt, passwort):
117133
return "Registrierung erfolgreich"
118134

119135
def anmeldenNutzer(self, email):
136+
"""
137+
Retrieve stored password hash and salt for the account with the given email.
138+
139+
Parameters:
140+
email (str): Account email to look up.
141+
142+
Returns:
143+
sqlite3.Row | None: A row containing `passwort` and `salt` for the matching account, or `None` if no account with the email exists.
144+
"""
120145
con = self.get_connection()
121146

122147
db = con.cursor()
@@ -131,12 +156,38 @@ def anmeldenNutzer(self, email):
131156
return row
132157

133158
def speichernInDB(self, daten: Dict[str, Any]):
159+
"""
160+
Persist the provided data dictionary into this instance's database.
161+
162+
Parameters:
163+
daten (Dict[str, Any]): A mapping of column/field names to values representing the record(s) to persist. The exact expected keys and behavior depend on calling context and table targeted by the implementation.
164+
"""
134165
pass
135166

136167
def holeDaten(self, tabelle: str) -> list[Dict[str, Any]]:
168+
"""
169+
Retrieve all rows from the specified table as a list of dictionaries.
170+
171+
Parameters:
172+
tabelle (str): Name of the database table to read from.
173+
174+
Returns:
175+
list[Dict[str, Any]]: A list where each item is a mapping of column names to their values for a single row; returns an empty list if the table has no rows or does not exist.
176+
"""
137177
pass
138178

139179
def entferneTextSyntax(self, text):
180+
"""
181+
Remove common bracket and punctuation characters from a string.
182+
183+
This function returns the input string with all parentheses `()`, square brackets `[]`, single quotes `'`, and commas `,` removed.
184+
185+
Parameters:
186+
text (str): Input string to sanitize.
187+
188+
Returns:
189+
str: The sanitized string with the specified characters removed.
190+
"""
140191
text = (
141192
text.replace("(", "")
142193
.replace(")", "")
@@ -148,6 +199,11 @@ def entferneTextSyntax(self, text):
148199
return text
149200

150201
def close(self):
202+
"""
203+
Close the instance's open SQLite connection.
204+
205+
After calling this method the database connection is closed and the instance should not be used for further database operations.
206+
"""
151207
self.con.close()
152208

153209
"""Alles Nachfolgende sind Testfunktionen
@@ -230,4 +286,3 @@ def test_print_tables(self):
230286
except Exception as e:
231287
print(f"Fehler beim Testen der Tabellen: {e}")
232288

233-

project/backend/LazyCookVerwaltung.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ def hash_password(password: str) -> tuple[str, str]:
3737
return base64.b64encode(salt).decode(), base64.b64encode(key).decode()
3838

3939
def verify_password(password: str, salt_b64: str, key_b64: str) -> bool:
40+
"""
41+
Validate a plaintext password against a stored salt and derived key.
42+
43+
Parameters:
44+
password (str): The plaintext password to verify.
45+
salt_b64 (str): Base64-encoded salt used when the stored key was derived.
46+
key_b64 (str): Base64-encoded derived key (password hash) to compare against.
47+
48+
Returns:
49+
bool: `True` if the password, when derived with the provided salt, matches the stored key; `False` otherwise.
50+
"""
4051
salt = base64.b64decode(salt_b64)
4152
key_original = base64.b64decode(key_b64)
4253

@@ -53,6 +64,19 @@ def verify_password(password: str, salt_b64: str, key_b64: str) -> bool:
5364
@app.post("/api/login")
5465
def anmelden(user: UserSignUpIn):
5566

67+
"""
68+
Authenticate a user by email and password.
69+
70+
Parameters:
71+
user (UserSignUpIn): Object containing `email` and `password` for authentication.
72+
73+
Returns:
74+
dict: A dictionary with a single `"message"` key describing the outcome:
75+
- "Anmeldung erfolgreich" if authentication succeeds.
76+
- "Falsches Passwort" if the password is incorrect.
77+
- "Für diese Email ist kein Konto hinterlegt" if no account exists for the provided email.
78+
- "Anmeldung fehlgeschlagen" if an unexpected error occurs.
79+
"""
5680
try:
5781
row = datenbank.anmeldenNutzer(user.email)
5882

@@ -110,4 +134,3 @@ def zeigeRezepteAn(self):
110134

111135

112136

113-

0 commit comments

Comments
 (0)