From e7e70daec0c0a0f0b3770814e5b6af4779fd7d9b Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Mon, 5 Aug 2024 20:43:29 +0200 Subject: [PATCH] funktionierendes Grundprojekt --- app.py | 37 ------------------- lernplattform/__init__.py | 9 +++++ lernplattform/app.py | 6 +++ lernplattform/models.py | 16 ++++++++ lernplattform/routes.py | 21 +++++++++++ lernplattform/setup.py | 15 ++++++++ .../templates}/index.html | 0 lernplattform/wsgi.py | 4 ++ models.py | 16 -------- 9 files changed, 71 insertions(+), 53 deletions(-) delete mode 100644 app.py create mode 100644 lernplattform/__init__.py create mode 100644 lernplattform/app.py create mode 100644 lernplattform/models.py create mode 100644 lernplattform/routes.py create mode 100644 lernplattform/setup.py rename {templates => lernplattform/templates}/index.html (100%) create mode 100644 lernplattform/wsgi.py delete mode 100644 models.py diff --git a/app.py b/app.py deleted file mode 100644 index 740f5dd..0000000 --- a/app.py +++ /dev/null @@ -1,37 +0,0 @@ -from flask import Flask, render_template, request, jsonify -from flask_sqlalchemy import SQLAlchemy - -# Importing db from models to ensure it exists and is initialized properly -from models import db - -# Function to initialize the database with the app context -def initialize_db(app): - # Push the application context to make it available within this function - app.app_context().push() - # Initialize the SQLAlchemy database with the Flask app configuration - db.init_app(app) - # Create all tables defined in the models (if they don't already exist) - db.create_all() - # Commit any changes to the database session - db.session.commit() - -# Function to create and configure the Flask application -def create_app(): - # Create a new Flask application instance with the name of the current module (__name__) - app = Flask(__name__) - - # Configure the SQLAlchemy database URI for SQLite, using 'learn.db' as the file location - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///learn.db' - # Disable tracking modifications of objects in the session to improve performance - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - - # Call the initialize_db function with the newly created Flask app instance - initialize_db(app) - - return app - -# Entry point for running the Flask application when this script is executed directly -if __name__ == '__main__': - # Create and run the Flask application instance with debugging enabled - app = create_app() - app.run(debug=True) \ No newline at end of file diff --git a/lernplattform/__init__.py b/lernplattform/__init__.py new file mode 100644 index 0000000..efea60a --- /dev/null +++ b/lernplattform/__init__.py @@ -0,0 +1,9 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///learn.db' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db = SQLAlchemy(app) + +import lernplattform.routes \ No newline at end of file diff --git a/lernplattform/app.py b/lernplattform/app.py new file mode 100644 index 0000000..51c714b --- /dev/null +++ b/lernplattform/app.py @@ -0,0 +1,6 @@ +from lernplattform import app +from lernplattform.models import initialize_database + +if __name__ == '__main__': + initialize_database() # Initialisiere die Datenbank beim Start der Anwendung + app.run(debug=True) \ No newline at end of file diff --git a/lernplattform/models.py b/lernplattform/models.py new file mode 100644 index 0000000..b3fe581 --- /dev/null +++ b/lernplattform/models.py @@ -0,0 +1,16 @@ +from lernplattform import db + +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(20), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=False) + password = db.Column(db.String(60), nullable=False) + + def __repr__(self): + return f"User('{self.username}', '{self.email}')" + +def initialize_database(): + from lernplattform import app + 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 new file mode 100644 index 0000000..4e8a9a0 --- /dev/null +++ b/lernplattform/routes.py @@ -0,0 +1,21 @@ +from flask import render_template, url_for, flash, redirect, jsonify +from lernplattform import app +from lernplattform.models import User + +@app.route('/') +def index(): + # Render the main page with the list of users + return render_template('index.html') + +@app.route('/users', methods=['GET']) +def get_users(): + users = User.query.all() + return jsonify([user.username for user in users]) + +@app.route('/users', methods=['POST']) +def create_user(): + data = request.get_json() + new_user = User(username=data['username'], email=data['email']) + db.session.add(new_user) + db.session.commit() + return jsonify({'message': 'User created'}), 201 \ No newline at end of file diff --git a/lernplattform/setup.py b/lernplattform/setup.py new file mode 100644 index 0000000..c5b4d9d --- /dev/null +++ b/lernplattform/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup, find_packages + +VERSION = '0.0.1' + +setup( + name='lernplattform', + version=VERSION, + license='MIT', + description='Eine Plattform zum lernen nach dem Leitner System', + author='Manuel Weiser', + author_email='manuel.weiser@me.com', + url='https://gitlab.fire-devils.org/ManuelW/Lerndatenbank', + packages=find_packages(exclude=('tests', 'docs', instance, env, _idee, __pycache__, templates)), + python_requires='>=3.10' +) \ No newline at end of file diff --git a/templates/index.html b/lernplattform/templates/index.html similarity index 100% rename from templates/index.html rename to lernplattform/templates/index.html diff --git a/lernplattform/wsgi.py b/lernplattform/wsgi.py new file mode 100644 index 0000000..6f51249 --- /dev/null +++ b/lernplattform/wsgi.py @@ -0,0 +1,4 @@ +from lernplattform import app + +if __name__ == "__main__": + app.run() \ No newline at end of file diff --git a/models.py b/models.py deleted file mode 100644 index 5cba329..0000000 --- a/models.py +++ /dev/null @@ -1,16 +0,0 @@ -# Import necessary modules -from flask_sqlalchemy import SQLAlchemy - -# Create an instance of SQLAlchemy and assign it to db -db = SQLAlchemy() - -# Define a User model class that inherits from db.Model -class table_user(db.Model): - # Define the id column as an integer primary key - id = db.Column(db.Integer, primary_key=True) - # Define the user_name column as a string with max length 80 and must be unique and not nullable - user_name = db.Column(db.String(80), unique=True, nullable=False) - # Define the user_password column as a string with max length 120 and must be unique and not nullable - user_password = db.Column(db.String(120), unique=True, nullable=False) - # Define the user_email column as a string with max length 120 and must be unique and not nullable - user_email = db.Column(db.String(120), unique=True, nullable=False)