funktionierendes Grundprojekt
This commit is contained in:
parent
1231787e08
commit
e7e70daec0
37
app.py
37
app.py
@ -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)
|
9
lernplattform/__init__.py
Normal file
9
lernplattform/__init__.py
Normal file
@ -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
|
6
lernplattform/app.py
Normal file
6
lernplattform/app.py
Normal file
@ -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)
|
16
lernplattform/models.py
Normal file
16
lernplattform/models.py
Normal file
@ -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.")
|
21
lernplattform/routes.py
Normal file
21
lernplattform/routes.py
Normal file
@ -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
|
15
lernplattform/setup.py
Normal file
15
lernplattform/setup.py
Normal file
@ -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'
|
||||
)
|
4
lernplattform/wsgi.py
Normal file
4
lernplattform/wsgi.py
Normal file
@ -0,0 +1,4 @@
|
||||
from lernplattform import app
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
16
models.py
16
models.py
@ -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)
|
Loading…
Reference in New Issue
Block a user