This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/documents/models.py

67 lines
2.4 KiB
Python
Raw Permalink Normal View History

2021-07-21 06:35:18 +00:00
from flask import g
2021-08-10 10:45:03 +00:00
from citext import CIText
from sortedcontainers import SortedSet
from sqlalchemy import BigInteger, Column, Sequence, Unicode, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declared_attr
2021-07-21 06:35:18 +00:00
from sqlalchemy.dialects.postgresql import UUID
2021-08-10 10:45:03 +00:00
from sqlalchemy.orm import backref
from teal.db import CASCADE_OWN, URL
2021-07-21 06:35:18 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.models import Thing, STR_SM_SIZE
2021-08-10 10:45:03 +00:00
_sorted_documents = {
'order_by': lambda: Document.created,
'collection_class': SortedSet
}
2021-07-21 06:35:18 +00:00
class Document(Thing):
"""This represent a generic document."""
id = Column(BigInteger, Sequence('device_seq'), primary_key=True)
id.comment = """The identifier of the device for this database. Used only
internally for software; users should not use this.
"""
2021-08-10 10:45:03 +00:00
document_type = Column(Unicode(STR_SM_SIZE), nullable=False)
2021-07-21 06:35:18 +00:00
date = Column(db.DateTime, nullable=True)
date.comment = """The date of document, some documents need to have one date
"""
2021-08-04 16:15:15 +00:00
id_document = Column(CIText(), nullable=True)
2021-07-21 06:35:18 +00:00
id_document.comment = """The id of one document like invoice so they can be linked."""
owner_id = db.Column(UUID(as_uuid=True),
db.ForeignKey(User.id),
nullable=False,
default=lambda: g.user.id)
owner = db.relationship(User, primaryjoin=owner_id == User.id)
file_name = Column(db.CIText(), nullable=False)
file_name.comment = """This is the name of the file when user up the document."""
file_hash = Column(db.CIText(), nullable=False)
file_hash.comment = """This is the hash of the file produced from frontend."""
2021-08-06 09:08:58 +00:00
url = db.Column(URL(), nullable=True)
2021-07-21 06:35:18 +00:00
url.comment = """This is the url where resides the document."""
def __str__(self) -> str:
return '{0.file_name}'.format(self)
class JoinedTableMixin:
# noinspection PyMethodParameters
@declared_attr
def id(cls):
return Column(BigInteger, ForeignKey(Document.id), primary_key=True)
class DataWipeDocument(JoinedTableMixin, Document):
"""This represent a generic document."""
2021-08-04 16:15:15 +00:00
software = Column(CIText(), nullable=True)
software.comment = """Which software is used"""
2021-08-06 09:08:58 +00:00
success = Column(Boolean, default=False)
success.comment = """If the erase was success"""
def __str__(self) -> str:
return '{0.file_name}'.format(self)