6
0

File.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Project import Project
  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=True)
  35. def set_collection(self, id: T.Optional[int]):
  36. """
  37. set this file's collection
  38. :param id: new collection id
  39. :return:
  40. """
  41. self.collection_id = id
  42. self.commit()
  43. def set_collection_by_reference(self, collection_reference: T.Optional[str]):
  44. """
  45. set this file's collection
  46. :param collection_reference: collection reference
  47. :return:
  48. """
  49. if self.collection_reference is None:
  50. self.set_collection(None)
  51. collection = Collection.query.filter_by(reference=collection_reference).one()
  52. self.collection = collection
  53. self.commit()
  54. def _get_another_file(self, *query) -> T.Optional[File]:
  55. """
  56. get the first file matching the query ordered by descending id
  57. :return: another file or None
  58. """
  59. return File.query.filter(*query)\
  60. .order_by(File.id.desc())\
  61. .first()
  62. def next(self) -> T.Optional[File]:
  63. """
  64. get the successor of this file
  65. :return: another file or None
  66. """
  67. query = File.id > self.id, Project.id == self.project_id
  68. return self._get_another_file(*query)
  69. def previous(self) -> T.Optional[File]:
  70. """
  71. get the predecessor of this file
  72. :return: another file or None
  73. """
  74. query = File.id < self.id, Project.id == self.project_id
  75. return self._get_another_file(*query)
  76. def next_in_collection(self) -> T.Optional[File]:
  77. """
  78. get the predecessor of this file
  79. :return: another file or None
  80. """
  81. query = File.id > self.id, Project.id == self.project_id, Collection.id == self.collection_id
  82. return self._get_another_file(*query)
  83. def previous_in_collection(self) -> T.Optional[File]:
  84. """
  85. get the predecessor of this file
  86. :return: another file or None
  87. """
  88. query = File.id < self.id, Project.id == self.project_id, Collection.id == self.collection_id
  89. return self._get_another_file(*query)
  90. def result(self, id: int) -> T.Optional[Result]:
  91. return self.results.get(id)
  92. def create_result(self, origin, result_type, label, data: T.Optional[dict] = None):
  93. data = data if data is None else json.dumps(data)
  94. result = Result.new(commit=True,
  95. file=self,
  96. origin=origin,
  97. type=result_type,
  98. label=label,
  99. data=data)
  100. return result
  101. def remove_results(self, origin='pipeline'):
  102. results = Result.query.filter(Result.file == self, Result.origin == origin)
  103. results.remove()
  104. return results