112 lines
4.5 KiB
Python
112 lines
4.5 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Table, Text
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
import datetime
|
|
|
|
Base = declarative_base()
|
|
|
|
# Association table for document tags
|
|
document_tags = Table('document_tags', Base.metadata,
|
|
Column('document_id', Integer, ForeignKey('documents.id')),
|
|
Column('tag_id', Integer, ForeignKey('tags.id'))
|
|
)
|
|
|
|
# Association table for document collections
|
|
document_collections = Table('document_collections', Base.metadata,
|
|
Column('document_id', Integer, ForeignKey('documents.id')),
|
|
Column('collection_id', Integer, ForeignKey('collections.id'))
|
|
)
|
|
|
|
class Document(Base):
|
|
__tablename__ = 'documents'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
file_path = Column(String(500), nullable=False)
|
|
file_name = Column(String(255), nullable=False)
|
|
marca = Column(String(100), nullable=False)
|
|
modelo = Column(String(100), nullable=False)
|
|
ano = Column(Integer, nullable=False)
|
|
cilindrada = Column(String(50))
|
|
codigo_motor = Column(String(100))
|
|
tipo_documento = Column(String(50), nullable=False) # Elétrico/Mecânico
|
|
variante = Column(String(50), nullable=False) # Esquema, Esquema OEM, etc.
|
|
observacoes = Column(Text)
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
folder_id = Column(Integer, ForeignKey('folders.id'))
|
|
|
|
folder = relationship("Folder", back_populates="documents")
|
|
versions = relationship("DocumentVersion", back_populates="document", cascade="all, delete-orphan")
|
|
tags = relationship("Tag", secondary=document_tags, back_populates="documents")
|
|
comments = relationship("Comment", back_populates="document", cascade="all, delete-orphan")
|
|
collections = relationship("Collection", secondary=document_collections, back_populates="documents")
|
|
|
|
class DocumentVersion(Base):
|
|
__tablename__ = 'document_versions'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
document_id = Column(Integer, ForeignKey('documents.id'))
|
|
version_number = Column(Integer, nullable=False)
|
|
file_path = Column(String(500), nullable=False)
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
changes = Column(Text)
|
|
|
|
document = relationship("Document", back_populates="versions")
|
|
|
|
class Folder(Base):
|
|
__tablename__ = 'folders'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String(100), nullable=False)
|
|
parent_id = Column(Integer, ForeignKey('folders.id'))
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
parent = relationship("Folder", remote_side=[id], back_populates="children")
|
|
children = relationship("Folder", back_populates="parent")
|
|
documents = relationship("Document", back_populates="folder")
|
|
|
|
class Tag(Base):
|
|
__tablename__ = 'tags'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String(50), nullable=False, unique=True)
|
|
color = Column(String(7), default="#808080") # Hex color code
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
documents = relationship("Document", secondary=document_tags, back_populates="tags")
|
|
|
|
class Comment(Base):
|
|
__tablename__ = 'comments'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
document_id = Column(Integer, ForeignKey('documents.id'))
|
|
text = Column(Text, nullable=False)
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
updated_at = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
|
|
page_number = Column(Integer) # For PDF comments
|
|
x_coord = Column(Integer) # For position-specific comments
|
|
y_coord = Column(Integer)
|
|
|
|
document = relationship("Document", back_populates="comments")
|
|
|
|
class Collection(Base):
|
|
__tablename__ = 'collections'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String(100), nullable=False, unique=True)
|
|
description = Column(Text)
|
|
color = Column(String(7), default="#808080") # Hex color code
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
documents = relationship("Document", secondary=document_collections, back_populates="collections")
|
|
|
|
class Statistics(Base):
|
|
__tablename__ = 'statistics'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
date = Column(DateTime, nullable=False)
|
|
total_documents = Column(Integer, default=0)
|
|
total_folders = Column(Integer, default=0)
|
|
documents_by_type = Column(Text) # JSON string
|
|
documents_by_brand = Column(Text) # JSON string
|
|
storage_used = Column(Integer, default=0) # in bytes
|