Create database models, views for Task, views for User and main file app.py

This commit is contained in:
Marcin Ramotowski 2023-02-28 00:10:37 +01:00
commit 348eab2f6f
4 changed files with 166 additions and 0 deletions

19
app.py Normal file
View File

@ -0,0 +1,19 @@
from flask import Flask
from models import db
from user_views import user_bp
from task_views import task_bp
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.register_blueprint(user_bp)
app.register_blueprint(task_bp)
db.init_app(app)
with app.app_context():
db.create_all()

33
models.py Normal file
View File

@ -0,0 +1,33 @@
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=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)
# is_superuser = db.Column(db.Boolean, default=False)
def to_dict(self):
return {"Id_użytkownika": self.id, "nazwa_użytkownika": self.username}
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
done = db.Column(db.Boolean, default=False)
due_date = db.Column(db.Date)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def to_dict(self):
return {
"id": self.id,
"title": self.title,
"description": self.description,
"due_date": self.due_date,
"done": self.done,
"user_id": self.user_id
}

66
task_views.py Normal file
View File

@ -0,0 +1,66 @@
from flask import Blueprint, jsonify, request, abort
from models import Task, db
from datetime import datetime
task_bp = Blueprint('task_bp', __name__)
@task_bp.route('/tasks', methods=['GET'])
def get_all_tasks():
tasks = Task.query.all()
return jsonify([task.to_dict() for task in tasks])
@task_bp.route('/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = Task.query.get_or_404(task_id)
return jsonify(task.to_dict())
@task_bp.route('/tasks/user/<int:user_id>', methods=['GET'])
def get_tasks_by_user(user_id):
tasks = Task.query.filter_by(user_id=user_id).all()
tasks = [task.to_dict() for task in tasks]
return jsonify(tasks)
@task_bp.route('/tasks', methods=['POST'])
def create_task():
data = request.get_json()
due_date = datetime.strptime(data['due_date'], '%d-%m-%Y')
task = Task(title=data['title'], description=data['description'], due_date=due_date,
done=data['done'], user_id=data['user_id'])
db.session.add(task)
db.session.commit()
return jsonify(task.to_dict())
@task_bp.route('/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = Task.query.get_or_404(task_id)
request_title = request.json.get('title')
request_description = request.json.get('description')
request_due_date = datetime.strptime(request.json.get('due_date'), '%d-%m-%Y')
request_done = request.json.get('done')
if all((task.title, task.description, task.due_date)) and task.done is not None:
task.title = request_title
task.description = request_description
task.due_date = request_due_date
task.done = request_done
db.session.commit()
return jsonify(task.to_dict())
else:
return abort(400, {'error': 'Niepełne dane zadania.'})
@task_bp.route('/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = Task.query.get_or_404(task_id)
db.session.delete(task)
db.session.commit()
return jsonify({})

48
user_views.py Normal file
View File

@ -0,0 +1,48 @@
from flask import Blueprint, jsonify, request, abort
from models import User, db
user_bp = Blueprint('user_bp', __name__)
@user_bp.route('/users', methods=['GET'])
def get_all_users():
users = User.query.all()
return jsonify([user.to_dict() for user in users])
@user_bp.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
user = User.query.get_or_404(user_id)
return jsonify(user.to_dict())
@user_bp.route('/users', methods=['POST'])
def create_user():
data = request.get_json()
user = User(username=data['username'], email=data['email'], password=data['password'])
db.session.add(user)
db.session.commit()
return jsonify(user.to_dict()), 201
@user_bp.route('/users/<int:user_id>', methods=['PUT'])
def edit_user(user_id):
request_data = request.get_json()
user_to_update = User.query.get_or_404(user_id)
request_username = request_data.get('username')
request_email = request_data.get('email')
if request_username and request_email:
user_to_update.username = request_username
user_to_update.email = request_email
db.session.commit()
return jsonify(user_to_update.to_dict())
else:
return abort(400, {'error': 'Niepełne dane użytkownika.'})
@user_bp.route('/users/<int:user_id>', methods=['DELETE'])
def remove_user(user_id):
user_to_delete = User.query.get_or_404(user_id)
db.session.delete(user_to_delete)
db.session.commit()
return jsonify({})