LU06.L06 - Todo-Liste mit Blueprints

Alle Routes wurde ausgelagert.

main.py
from flask import Flask
from flask_login import LoginManager
 
from todoBlueprint import todo_blueprint
from userBlueprint import user_blueprint
 
app = Flask(__name__)
app.secret_key = 'supersecretkey'
 
login_manager = LoginManager()
login_manager.init_app(app)
 
@login_manager.user_loader
def load_user(user_id):
    from userDao import UserDao
    user_dao = UserDao('todo_example.db')
    return user_dao.get_user_by_id(int(user_id))
 
 
app.register_blueprint(todo_blueprint)
app.register_blueprint(user_blueprint)
 
 
def generate_testdata():
 
    from todoItem import TodoItem
    from user import User
    from todoDao import TodoDao
    from userDao import UserDao
 
    todo_dao = TodoDao('todo_example.db')
    user_dao = UserDao('todo_example.db')
 
    # Generate user
    user_dao.create_user_table()
    user_dao.add_user(User(1, 'admin', 'admin@example', 'admin'))
 
    # Generate todo items
    todo_dao.create_table()
    todo_dao.add_item(TodoItem(1, 'Buy milk', False))
    todo_dao.add_item(TodoItem(2, 'Buy eggs', False))
    todo_dao.add_item(TodoItem(3, 'Buy bread', False))
    todo_dao.add_item(TodoItem(4, 'Buy butter', False))
 
    todo_dao.close()
    user_dao.close()
 
 
if __name__ == '__main__':
    generate_testdata()
    app.run(debug=True)
todoBlueprint.py
from flask import Blueprint
from flask_login import login_required
from flask import jsonify, request
from todoDao import TodoDao
from todoItem import TodoItem
 
todo_blueprint = Blueprint('todo_blueprint', __name__)
todo_dao = TodoDao('todo_example.db')
 
 
@todo_blueprint.route('/todos', methods=['GET'])
def get_all_todos():
    items = todo_dao.get_all_items()
    return jsonify([item.__dict__ for item in items]), 200
 
 
@todo_blueprint.route('/todos/<int:item_id>', methods=['GET'])
def get_todo(item_id):
    item = todo_dao.get_item(item_id)
    if item:
        return jsonify(item.__dict__), 200
    else:
        return jsonify({"message": "Item not found"}), 404
 
 
@todo_blueprint.route('/todos', methods=['POST'])
@login_required
def add_todo():
    data = request.get_json()
    new_item = TodoItem(None, data['title'], data['is_completed'])
    todo_dao.add_item(new_item)
    return jsonify({"message": "Todo item created"}), 201
 
 
@todo_blueprint.route('/todos/<int:item_id>', methods=['PUT'])
@login_required
def update_todo(item_id):
    data = request.get_json()
    updated_item = TodoItem(item_id, data['title'], data['is_completed'])
    if todo_dao.update_item(updated_item):
        return jsonify({"message": "Item updated"}), 200
    else:
        return jsonify({"message": "Item not found or not updated"}), 404
 
 
@todo_blueprint.route('/todos/<int:item_id>', methods=['DELETE'])
@login_required
def delete_todo(item_id):
    if todo_dao.delete_item(item_id):
        return jsonify({"message": "Item deleted"}), 200
    else:
        return jsonify({"message": "Item not found or not deleted"}), 404
userBlueprint.py
from flask import Blueprint, request, jsonify
from flask_login import login_required, login_user, logout_user
from userDao import UserDao
 
user_blueprint = Blueprint('user_blueprint', __name__)
user_dao = UserDao('todo_example.db')
 
 
@user_blueprint.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    user = user_dao.get_user_by_username(data['username'])
    if user and user.password == data['password']:
        login_user(user)
        return jsonify({'success': True}), 200
    return jsonify({'error': 'Invalid username or password'}), 401
 
 
@user_blueprint.route('/logout')
@login_required
def logout():
    logout_user()
    return jsonify({'success': True}), 200
  • modul/m323/learningunits/lu06/loesungen/blueprints.txt
  • Zuletzt geändert: 2024/03/28 14:07
  • von 127.0.0.1