Exemplo n.º 1
0
from flask import Flask, request, jsonify
from app.model import User, Book, Car
from app.serializer import UserSchema, BookSchema, CarSchema, UpdateUserSchema, UpdateBookSchema, UpdateCarSchema
from app_init.app_factory import create_app
from flask_jwt_extended import create_access_token, get_jwt_identity, jwt_required, jwt_refresh_token_required, create_refresh_token
from marshmallow import ValidationError
from http import HTTPStatus
import os, requests

settings_name = os.getenv("settings")
app = create_app(settings_name)

# LOGIN


@app.route('/users/login', methods=["POST"])
def get_login():
    user = request.get_json()
    user_data: User = User.query.filter_by(email=user.get("email")).first()
    if user_data:
        if user_data.check_password(user.get("password")):
            access_token = create_access_token(identity=user_data.email)
            refresh_token = create_refresh_token(identity=user_data.email)
            user_schema = UserSchema().dump(user_data)
            user_schema["access_token"] = access_token
            user_schema["refresh_token"] = refresh_token
            return jsonify(user_schema), HTTPStatus.OK
    return jsonify(msg="Incorrect email or password"), HTTPStatus.BAD_REQUEST


# REFRESH TOKEN
Exemplo n.º 2
0
from flask import Flask, request,jsonify,send_file, send_from_directory
from http import HTTPStatus
from marshmallow.exceptions import ValidationError
from app.model import User, UserBooks
from app.serializer import UpdateSchema, UserSchema
from app_init.app_factory import create_app
import requests

    

app = create_app()

#CREATE USER

@app.route('/users', methods=["POST"])
def register():
    user_info = request.get_json()
    user = User.query.filter_by(email = user_info.get("email")).first()
    if user:
        return jsonify(msg="User exsist"),HTTPStatus.BAD_REQUEST
    try:    
        user = UserSchema().load(user_info)
        response = requests.get(f"http://127.0.0.1:5002/books/{user_info.get('book_id')}")
        if user:  
            user.set_password()
            user = user.save_db()   
            if response.status_code == 200:
                user_books = UserBooks()
                user_books.user_id = user.id
                user_books.book_id = response.json().get('id')
                user_books.save_db()