123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- from contextlib import closing
- from typing import Iterator
- from pycs import db
- from pycs.database.base import NamedBaseModel
- class Collection(NamedBaseModel):
- # table columns
- project_id = db.Column(
- db.Integer, db.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)
- reference = db.Column(
- db.String, nullable=False)
- description = db.Column(
- db.String)
- position = db.Column(
- db.Integer, nullable=False)
- autoselect = db.Column(
- db.Boolean, nullable=False)
- # contraints
- __table_args__ = (
- db.UniqueConstraint('project_id', 'reference'),
- )
- # relationships to other models
- files = db.relationship("File", backref="collection", lazy="dynamic")
- serialize_rules = ('-files',)
- def count_files(self) -> int:
- return self.files.count()
- # def files_it(self, offset: int = 0, limit: int = -1) -> Iterator[File]:
- # # self.files.filter
- # files = File.query.filter_by(project_id=self.project_id, collection_id=self.id)
- # raise NotImplementedError
- def get_files(self, offset: int = 0, limit: int = -1):
- """
- get an iterator of files associated with this project
- :param offset: file offset
- :param limit: file limit
- :return: iterator of files
- """
- from pycs.database.File import File
- return self.files.order_by(File.id).offset(offset).limit(limit)
|