Collection.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from contextlib import closing
  2. from typing import List
  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 = False if row[6] == 0 else True
  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=0, limit=-1) -> List[File]:
  43. """
  44. get a list of files associated with this collection
  45. :param offset: file offset
  46. :param limit: file limit
  47. :return: list 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. return list(map(
  56. lambda row: File(self.database, row),
  57. cursor.fetchall()
  58. ))