diff --git a/lernplattform/classes.py b/lernplattform/classes.py
new file mode 100644
index 0000000..c89c380
--- /dev/null
+++ b/lernplattform/classes.py
@@ -0,0 +1,27 @@
+from flask import Flask, request
+from lernplattform import app
+from lernplattform.models import User
+
+class login():
+ """
+ A class for handling user login functionality.
+
+ Attributes:
+ user_name (str): The username to be checked for authentication.
+
+ Methods:
+ __init__(self, user_name=None): Initializes the login object with a given user name.
+ check(user_name): A static method that checks if the provided user name is valid.
+ login(self): Prints "Login" to indicate that the login method has been called.
+ """
+ def __init__(self, user_name=None):
+ self.user_name = user_name # Initialize the user name attribute for the login class
+
+ @staticmethod
+ def check(user_name):
+ if user_name is None:
+ return False # Return False if the user name is not provided
+ else:
+ return True # Otherwise, return True indicating a valid user name
+ def login(self):
+ print("Login") # Print "Login" to indicate that the login method has been called
diff --git a/lernplattform/models.py b/lernplattform/models.py
index b3fe581..7e5b684 100644
--- a/lernplattform/models.py
+++ b/lernplattform/models.py
@@ -1,16 +1,23 @@
from lernplattform import db
+# Define a User model that represents the user table in the database
class User(db.Model):
+ # Primary key column for the table, auto-incrementing integer
id = db.Column(db.Integer, primary_key=True)
+ # Username column with a maximum length of 20 characters, unique and not nullable
username = db.Column(db.String(20), unique=True, nullable=False)
+ # Email column with a maximum length of 120 characters, unique and not nullable
email = db.Column(db.String(120), unique=True, nullable=False)
+ # Password column with a maximum length of 60 characters, not nullable
password = db.Column(db.String(60), nullable=False)
+ # Magic method to represent the object as a string when printed or used in string context
def __repr__(self):
return f"User('{self.username}', '{self.email}')"
+# Function to initialize the database by creating all tables defined in models if they do not exist
def initialize_database():
from lernplattform import app
+ # Create an application context before running operations on the database within this function
with app.app_context():
db.create_all()
- print("Database and tables created successfully.")
\ No newline at end of file
diff --git a/lernplattform/routes.py b/lernplattform/routes.py
index 4e8a9a0..f35096b 100644
--- a/lernplattform/routes.py
+++ b/lernplattform/routes.py
@@ -1,21 +1,57 @@
-from flask import render_template, url_for, flash, redirect, jsonify
+from flask import render_template, url_for, flash, redirect, jsonify, request
from lernplattform import app
from lernplattform.models import User
+from lernplattform import db
+from lernplattform.classes import login
+
@app.route('/')
def index():
- # Render the main page with the list of users
- return render_template('index.html')
+ """
+ Renders the main page of the application.
+ This function checks if a user is logged in by checking for a 'user_name' cookie.
+ If the user is logged in, it retrieves the username from the database using the cookie value.
+ If not, it defaults to 'Gast'. The main page is then rendered with the current username.
+
+ Returns:
+ A rendered HTML template with the current username.
+ """
+ if login.check(request.cookies.get('user_name')):
+ user_name = User.query.filter_by(username=request.cookies.get('user_name')).first().username
+ else:
+ # If not logged in, default to 'Gast'
+ user_name = 'Gast'
+
+ # Render the main page with the current username
+ return render_template('index.html', user_name=user_name)
@app.route('/users', methods=['GET'])
def get_users():
+ # Query all users from the database
users = User.query.all()
- return jsonify([user.username for user in users])
+ # Create a list of dictionaries with user information
+ user_list = [
+ {
+ "id": user.id,
+ "username": user.username,
+ "email": user.email,
+ "password": user.password
+ }
+ for user in users
+ ]
+ # Return the list of users as a JSON response
+ return jsonify(user_list)
+ #return user_list[0]['username']
@app.route('/users', methods=['POST'])
def create_user():
+ # Get the JSON data from the request body
data = request.get_json()
- new_user = User(username=data['username'], email=data['email'])
+ # Create a new User object with the provided username, email, and password
+ new_user = User(username=data['username'], email=data['email'], password=data['password'])
+ # Add the new user to the database session
db.session.add(new_user)
+ # Commit the transaction to save the new user in the database
db.session.commit()
+ # Return a success message with status code 201 (Created)
return jsonify({'message': 'User created'}), 201
\ No newline at end of file
diff --git a/lernplattform/static/images/logo.png b/lernplattform/static/images/logo.png
new file mode 100644
index 0000000..44dcce2
Binary files /dev/null and b/lernplattform/static/images/logo.png differ
diff --git a/lernplattform/templates/index.html b/lernplattform/templates/index.html
index 1afb36d..312ac37 100644
--- a/lernplattform/templates/index.html
+++ b/lernplattform/templates/index.html
@@ -3,29 +3,49 @@
- Social Media Layout
+ Lernplattform EMT/Paramedic
-
+
+ {% include 'nav.html' %}
-
-
Sam Lanson
+
+
{{ user_name }}
Web Developer at Webestica
-
I'd love to change the world, but they won’t give me the source code.
+
I'd love to change the world, but they won’t give me the source code.
256 Posts2.5K Followers365 Following
+
+
-
+
-
+
+
-
Lori Ferguson
- Web Developer at Webestica • 2hr
+
Lernplattform für Rettungs- und Notfallsanitäter
+ Hier kann man lernen und üben, Wiessen festigen und sich aktiv an der Datenbank beteiligen.
+
I'm thrilled to share that I've completed a graduate certificate course in project
management with the president's honor roll.
@@ -159,16 +181,16 @@