__init__.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from wsgidav.compat import to_bytes
  2. from tracim.lib.content import ContentApi
  3. from tracim.model import new_revision
  4. from tracim.model.data import ActionDescription, ContentType, Content
  5. from wsgidav import util
  6. import transaction
  7. class HistoryType(object):
  8. Deleted = 'deleted'
  9. Archived = 'archived'
  10. Standard = 'standard'
  11. All = 'all'
  12. class FileStream(object):
  13. def __init__(self, file_name: str, content: Content, content_api: ContentApi, new_file: bool):
  14. self._buffer = []
  15. self._file_name = file_name if file_name != '' else self._content.file_name
  16. self._content = content
  17. self._api = content_api
  18. def beginWrite(self, contentType) -> FileStream:
  19. return self
  20. def endWrite(self, withErrors: bool):
  21. pass
  22. def write(self, s: str):
  23. self._buffer.append(s)
  24. def close(self):
  25. item_content = b''
  26. for part in self._buffer:
  27. item_content += part
  28. if new_file:
  29. file = self._api.create(
  30. content_type=ContentType.File,
  31. workspace=self._content.workspace,
  32. parent=self._content
  33. )
  34. self._api.update_file_data(
  35. file,
  36. self._file_name,
  37. util.guessMimeType(self._file_name),
  38. item_content
  39. )
  40. self._api.save(file, ActionDescription.CREATION)
  41. else:
  42. with new_revision(self._content):
  43. self._api.update_file_data(
  44. self._content,
  45. self._file_name,
  46. util.guessMimeType(self._content.file_name),
  47. item_content)
  48. self._api.save(self._content, ActionDescription.EDITION)
  49. transaction.commit()