6
0

Collection.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from contextlib import closing
  2. from typing import Iterator
  3. from pycs.database.File import File
  4. class Collection:
  5. """
  6. database class for collections
  7. """
  8. def __init__(self, database, row):
  9. self.database = database
  10. self.identifier = row[0]
  11. self.project_id = row[1]
  12. self.reference = row[2]
  13. self.name = row[3]
  14. self.description = row[4]
  15. self.position = row[5]
  16. self.autoselect = row[6] > 0
  17. def set_name(self, name: str):
  18. """
  19. set this collection's name
  20. :param name: new name
  21. :return:
  22. """
  23. with closing(self.database.con.cursor()) as cursor:
  24. cursor.execute('UPDATE collections SET name = ? WHERE id = ?', (name, self.identifier))
  25. self.name = name
  26. def remove(self) -> None:
  27. """
  28. remove this collection from the database
  29. :return:
  30. """
  31. with closing(self.database.con.cursor()) as cursor:
  32. cursor.execute('DELETE FROM collections WHERE id = ?', [self.identifier])
  33. def count_files(self) -> int:
  34. """
  35. count files associated with this project
  36. :return: count
  37. """
  38. with closing(self.database.con.cursor()) as cursor:
  39. cursor.execute('SELECT COUNT(*) FROM files WHERE project = ? AND collection = ?',
  40. (self.project_id, self.identifier))
  41. return cursor.fetchone()[0]
  42. def files(self, offset: int = 0, limit: int = -1) -> Iterator[File]:
  43. """
  44. get an iterator of files associated with this collection
  45. :param offset: file offset
  46. :param limit: file limit
  47. :return: iterator of files
  48. """
  49. with closing(self.database.con.cursor()) as cursor:
  50. cursor.execute('''
  51. SELECT * FROM files
  52. WHERE project = ? AND collection = ?
  53. ORDER BY id ASC LIMIT ? OFFSET ?
  54. ''', (self.project_id, self.identifier, limit, offset))
  55. for row in cursor:
  56. yield File(self.database, row)