123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- from __future__ import annotations
- from typing import List
- from pycs import db
- from pycs.database.base import NamedBaseModel
- class Collection(NamedBaseModel):
- """ DB Model for collections """
- # 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_only = NamedBaseModel.serialize_only + (
- "project_id",
- "reference",
- "description",
- "position",
- "autoselect",
- )
- 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
- """
- # pylint: disable=import-outside-toplevel
- from pycs.database.File import File
- return self.files.order_by(File.id).offset(offset).limit(limit)
- @staticmethod
- def update_autoselect(collections: List[Collection]) -> List[Collection]:
- """ disable autoselect if there are no elements in the collection """
- found = False
- for collection in collections:
- if not collection.autoselect:
- continue
- if found:
- collection.autoselect = False
- elif collection.files.count() == 0:
- collection.autoselect = False
- found = True
- return collections
|