6
0

Collection.py 1.4 KB

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