|
| 1 | +import logging |
| 2 | +from flask import Flask, jsonify |
| 3 | +from flask_sqlalchemy import SQLAlchemy |
| 4 | + |
| 5 | +app = Flask(__name__) |
| 6 | +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 7 | +app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost/postgres' |
| 8 | +app.debug = True |
| 9 | +db = SQLAlchemy(app) |
| 10 | + |
| 11 | +class users(db.Model): |
| 12 | + __tablename__ = 'usuario' |
| 13 | + id_usuario = db.Column(db.String(), primary_key=True) |
| 14 | + fk_id_categoria_usuario = db.Column(db.String(), nullable=False) |
| 15 | + login = db.Column(db.String(), nullable=False) |
| 16 | + senha = db.Column(db.String(), nullable=False) |
| 17 | + |
| 18 | + def __init__(self, id_usuario, fk_id_categoria_usuario, login, senha): |
| 19 | + self.id_usuario = id_usuario |
| 20 | + self.fk_id_categoria_usuario = fk_id_categoria_usuario |
| 21 | + self.login = login |
| 22 | + self.senha = senha |
| 23 | + |
| 24 | + |
| 25 | +@app.route('/test', methods=['GET']) |
| 26 | +def test(): |
| 27 | + return { |
| 28 | + 'test': 'test1' |
| 29 | + } |
| 30 | + |
| 31 | +# A simple test for the db connection |
| 32 | +@app.route('/test_db_connection', methods=['GET']) |
| 33 | +def test_db_test_db_connection(): |
| 34 | + all_users = users.query.all() |
| 35 | + output = [] |
| 36 | + for usuario in all_users: |
| 37 | + current_user = {} |
| 38 | + current_user['id_usuario'] = usuario.id_usuario |
| 39 | + current_user['fk_id_categoria_usuario'] = usuario.fk_id_categoria_usuario |
| 40 | + current_user['login'] = usuario.login |
| 41 | + current_user['senha'] = usuario.senha |
| 42 | + output.append(current_user) |
| 43 | + return jsonify(output) |
| 44 | + |
0 commit comments