6
0

File.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from __future__ import annotations
  2. import json
  3. import typing as T
  4. from contextlib import closing
  5. from datetime import datetime
  6. from pycs import db
  7. from pycs.database.Result import Result
  8. from pycs.database.Label import Label
  9. from pycs.database.Collection import Collection
  10. from pycs.database.base import NamedBaseModel
  11. class File(NamedBaseModel):
  12. # table columns
  13. uuid = db.Column(db.String, nullable=False)
  14. extension = db.Column(db.String, nullable=False)
  15. type = db.Column(db.String, nullable=False)
  16. size = db.Column(db.String, nullable=False)
  17. created = db.Column(db.DateTime, default=datetime.utcnow,
  18. index=True, nullable=False)
  19. path = db.Column(db.String, nullable=False)
  20. frames = db.Column(db.Integer)
  21. fps = db.Column(db.Float)
  22. project_id = db.Column(
  23. db.Integer,
  24. db.ForeignKey("project.id", ondelete="CASCADE"),
  25. nullable=False)
  26. collection_id = db.Column(
  27. db.Integer,
  28. db.ForeignKey("collection.id", ondelete="SET NULL"))
  29. # contraints
  30. __table_args__ = (
  31. db.UniqueConstraint('project_id', 'path'),
  32. )
  33. # relationships to other models
  34. results = db.relationship("Result", backref="file", lazy="dynamic")
  35. serialize_only = (
  36. "id", "name", "uuid",
  37. "extension", "type", "size",
  38. "created", "path", "frames",
  39. "fps", "project_id",
  40. "collection_id",
  41. )
  42. def set_collection(self, id: T.Optional[int]):
  43. """
  44. set this file's collection
  45. :param id: new collection id
  46. :return:
  47. """
  48. self.collection_id = id
  49. self.commit()
  50. def set_collection_by_reference(self, collection_reference: T.Optional[str]):
  51. """
  52. set this file's collection
  53. :param collection_reference: collection reference
  54. :return:
  55. """
  56. if self.collection_reference is None:
  57. self.set_collection(None)
  58. collection = Collection.query.filter_by(reference=collection_reference).one()
  59. self.collection = collection
  60. self.commit()
  61. def _get_another_file(self, *query) -> T.Optional[File]:
  62. """
  63. get the first file matching the query ordered by descending id
  64. :return: another file or None
  65. """
  66. # return self.project.files.filter(*query)\
  67. return File.query.filter_by(project_id=self.project_id)\
  68. .filter(*query)\
  69. .order_by(File.id.desc())\
  70. .first()
  71. def next(self) -> T.Optional[File]:
  72. """
  73. get the successor of this file
  74. :return: another file or None
  75. """
  76. return self._get_another_file(File.id > self.id)
  77. def previous(self) -> T.Optional[File]:
  78. """
  79. get the predecessor of this file
  80. :return: another file or None
  81. """
  82. return self._get_another_file(File.id < self.id)
  83. def next_in_collection(self) -> T.Optional[File]:
  84. """
  85. get the predecessor of this file
  86. :return: another file or None
  87. """
  88. return self._get_another_file(File.id > self.id, Collection.id == self.collection_id)
  89. def previous_in_collection(self) -> T.Optional[File]:
  90. """
  91. get the predecessor of this file
  92. :return: another file or None
  93. """
  94. return self._get_another_file(File.id < self.id, Collection.id == self.collection_id)
  95. def result(self, id: int) -> T.Optional[Result]:
  96. return self.results.get(id)
  97. def create_result(self, origin, result_type, label, data: T.Optional[dict] = None):
  98. data = data if data is None else json.dumps(data)
  99. result = Result.new(file=self,
  100. origin=origin,
  101. type=result_type,
  102. data=data)
  103. if label is not None:
  104. assert isinstance(label, (int, Label)), f"Wrong label type: {type(label)}"
  105. if isinstance(label, Label):
  106. label = label.id
  107. result.label_id = label
  108. self.commit()
  109. return result
  110. def remove_results(self, origin='pipeline'):
  111. results = Result.query.filter(Result.file == self, Result.origin == origin)
  112. results.delete()
  113. return results