Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a999187e78 | |||
| 6b45f7afcb | |||
|
|
11924af113 | ||
|
|
3b5d5a194f | ||
| 715dd6e2b9 | |||
| d0960bb6ca | |||
|
|
23b18ff30c | ||
|
|
318e5353e7 | ||
|
|
29b7b39184 | ||
|
|
f94f7a99cd | ||
|
|
ae963051cb | ||
|
|
2ecf163f28 | ||
|
|
8afcccf72f | ||
|
|
4e1f910048 | ||
| 899f941a7c | |||
| 5a11434c83 |
@ -27,6 +27,8 @@ from app.routes.user_routes import user_bp
|
|||||||
from app.routes.login_form import login_bp
|
from app.routes.login_form import login_bp
|
||||||
from app.routes.dashboard import dashboard_bp
|
from app.routes.dashboard import dashboard_bp
|
||||||
from app.routes.invoice_routes import invoice_bp
|
from app.routes.invoice_routes import invoice_bp
|
||||||
|
from app.routes.tag_routes import tag_bp
|
||||||
|
from app.routes.doc_tag_routes import doc_tag_bp
|
||||||
from werkzeug.security import generate_password_hash
|
from werkzeug.security import generate_password_hash
|
||||||
|
|
||||||
def create_default_user():
|
def create_default_user():
|
||||||
@ -79,5 +81,6 @@ def create_app():
|
|||||||
app.register_blueprint(invoice_bp, url_prefix="/invoices")
|
app.register_blueprint(invoice_bp, url_prefix="/invoices")
|
||||||
app.register_blueprint(login_bp, url_prefix="/login_bp")
|
app.register_blueprint(login_bp, url_prefix="/login_bp")
|
||||||
app.register_blueprint(dashboard_bp, url_prefix="/dashboard")
|
app.register_blueprint(dashboard_bp, url_prefix="/dashboard")
|
||||||
|
app.register_blueprint(tag_bp, url_prefix="/tags")
|
||||||
|
app.register_blueprint(doc_tag_bp, url_prefix="/doc-tags")
|
||||||
return app
|
return app
|
||||||
|
|||||||
11
app/models/model_doc_tag.py
Normal file
11
app/models/model_doc_tag.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from app.database import db
|
||||||
|
from sqlalchemy import Column, ForeignKey
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
class DocTag(db.Model):
|
||||||
|
__tablename__ = "doc_tags"
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
documento_id = Column(UUID(as_uuid=True), ForeignKey("documento.id"), nullable=False)
|
||||||
|
tag_id = Column(UUID(as_uuid=True), ForeignKey("tag.id"), nullable=False)
|
||||||
10
app/models/model_tag.py
Normal file
10
app/models/model_tag.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from app.database import db
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
class Tag(db.Model):
|
||||||
|
__tablename__ = "tag"
|
||||||
|
|
||||||
|
id = db.Column(db.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
nome = db.Column(db.String, unique=True, nullable=False)
|
||||||
16
app/models/ocr_results.py
Normal file
16
app/models/ocr_results.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from sqlalchemy import Column, String, Boolean, ForeignKey, DateTime
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class OCRResult(Base):
|
||||||
|
__tablename__ = "ocr_result" # Corrigido
|
||||||
|
|
||||||
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
documento_id = Column(UUID(as_uuid=True), ForeignKey("documento.id"), unique=True, nullable=False)
|
||||||
|
caminho_texto = Column(String, nullable=True)
|
||||||
|
indexado = Column(Boolean, default=False)
|
||||||
|
criado_em = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
|
||||||
39
app/routes/doc_tag_routes.py
Normal file
39
app/routes/doc_tag_routes.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from app.models.model_doc_tag import DocTag
|
||||||
|
from app.database import db
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
# Criação do Blueprint
|
||||||
|
doc_tag_bp = Blueprint("doc_tag_routes", __name__)
|
||||||
|
|
||||||
|
@doc_tag_bp.route("/", methods=["POST"], endpoint="create_doc_tag")
|
||||||
|
def create_doc_tag():
|
||||||
|
try:
|
||||||
|
documento_id = request.json.get("documento_id")
|
||||||
|
tag_id = request.json.get("tag_id")
|
||||||
|
|
||||||
|
# Verifica se os IDs são UUID válidos
|
||||||
|
try:
|
||||||
|
documento_id = UUID(documento_id)
|
||||||
|
tag_id = UUID(tag_id)
|
||||||
|
except ValueError:
|
||||||
|
return jsonify({"error": "IDs devem ser UUID válidos"}), 400
|
||||||
|
|
||||||
|
new_doc_tag = DocTag(documento_id=documento_id, tag_id=tag_id)
|
||||||
|
db.session.add(new_doc_tag)
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify({"id": new_doc_tag.id, "documento_id": str(new_doc_tag.documento_id), "tag_id": str(new_doc_tag.tag_id)}), 201
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"error": "Erro ao criar DocTag", "details": str(e)}), 500
|
||||||
|
|
||||||
|
@doc_tag_bp.route("/", methods=["GET"], endpoint="list_doc_tags")
|
||||||
|
def list_doc_tags():
|
||||||
|
try:
|
||||||
|
doc_tags = DocTag.query.all()
|
||||||
|
return jsonify([{ "id": str(dt.id), "documento_id": str(dt.documento_id), "tag_id": str(dt.tag_id) } for dt in doc_tags]), 200
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
return jsonify({"error": "Erro ao listar DocTags", "details": str(e)}), 500
|
||||||
19
app/routes/ocr_results_routes.py
Normal file
19
app/routes/ocr_results_routes.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from models.ocr_result import OCRResult
|
||||||
|
from models.base import db
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
ocr_routes = Blueprint("ocr_routes", __name__, url_prefix="/ocr")
|
||||||
|
|
||||||
|
@ocr_routes.route("/", methods=["POST"])
|
||||||
|
def create_ocr():
|
||||||
|
documento_id = request.json.get("documento_id")
|
||||||
|
new_ocr = OCRResult(documento_id=documento_id)
|
||||||
|
db.session.add(new_ocr)
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify({"id": new_ocr.id, "documento_id": new_ocr.documento_id}), 201
|
||||||
|
|
||||||
|
@ocr_routes.route("/", methods=["GET"])
|
||||||
|
def list_ocr():
|
||||||
|
ocrs = OCRResult.query.all()
|
||||||
|
return jsonify([{"id": ocr.id, "documento_id": ocr.documento_id} for ocr in ocrs]), 200
|
||||||
55
app/routes/tag_routes.py
Normal file
55
app/routes/tag_routes.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from app.models.model_tag import Tag
|
||||||
|
from app.database import db
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
tag_bp = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
||||||
|
|
||||||
|
@tag_bp.route("/", methods=["POST"], endpoint="create_tag")
|
||||||
|
def create_tag():
|
||||||
|
try:
|
||||||
|
nome = request.json.get("nome")
|
||||||
|
if not nome:
|
||||||
|
return jsonify({"error": "Nome é obrigatório"}), 400
|
||||||
|
|
||||||
|
existing_tag = Tag.query.filter_by(nome=nome).first()
|
||||||
|
if existing_tag:
|
||||||
|
return jsonify({"error": "Nome já está em uso"}), 409
|
||||||
|
|
||||||
|
new_tag = Tag(nome=nome)
|
||||||
|
db.session.add(new_tag)
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify({"id": new_tag.id, "nome": new_tag.nome}), 201
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"error": "Erro ao criar tag", "details": str(e)}), 500
|
||||||
|
|
||||||
|
@tag_bp.route("/", methods=["GET"], endpoint="list_tags")
|
||||||
|
def list_tags():
|
||||||
|
try:
|
||||||
|
tags = Tag.query.all()
|
||||||
|
if not tags:
|
||||||
|
return jsonify({"message": "Nenhuma tag encontrada"}), 404
|
||||||
|
|
||||||
|
return jsonify([{"id": tag.id, "nome": tag.nome} for tag in tags]), 200
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
return jsonify({"error": "Erro ao listar tags", "details": str(e)}), 500
|
||||||
|
|
||||||
|
@tag_bp.route("/<uuid:tag_id>", methods=["DELETE"], endpoint="delete_tag")
|
||||||
|
def delete_tag(tag_id):
|
||||||
|
try:
|
||||||
|
tag = Tag.query.get(tag_id)
|
||||||
|
if not tag:
|
||||||
|
return jsonify({"error": "Tag não encontrada"}), 404
|
||||||
|
|
||||||
|
db.session.delete(tag)
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify({"message": "Tag deletada com sucesso"}), 200
|
||||||
|
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({"error": "Erro ao deletar tag", "details": str(e)}), 500
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user