6
0

Collection.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from contextlib import closing
  2. from typing import Iterator
  3. from pycs import db
  4. from pycs.database.base import NamedBaseModel
  5. class Collection(NamedBaseModel):
  6. # table columns
  7. project_id = db.Column(
  8. db.Integer, db.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)
  9. reference = db.Column(
  10. db.String, nullable=False)
  11. description = db.Column(
  12. db.String)
  13. position = db.Column(
  14. db.Integer, nullable=False)
  15. autoselect = db.Column(
  16. db.Boolean, nullable=False)
  17. # contraints
  18. __table_args__ = (
  19. db.UniqueConstraint('project_id', 'reference'),
  20. )
  21. # relationships to other models
  22. files = db.relationship("File", backref="collection", lazy="dynamic")
  23. serialize_rules = ('-files',)
  24. def count_files(self) -> int:
  25. return self.files.count()
  26. # def files_it(self, offset: int = 0, limit: int = -1) -> Iterator[File]:
  27. # # self.files.filter
  28. # files = File.query.filter_by(project_id=self.project_id, collection_id=self.id)
  29. # raise NotImplementedError
  30. def get_files(self, offset: int = 0, limit: int = -1):
  31. """
  32. get an iterator of files associated with this project
  33. :param offset: file offset
  34. :param limit: file limit
  35. :return: iterator of files
  36. """
  37. from pycs.database.File import File
  38. return self.files.order_by(File.id).offset(offset).limit(limit)