upload.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """Test states for upload-related tests."""
  2. from pathlib import Path
  3. from typing import ClassVar
  4. import reflex as rx
  5. from reflex.state import BaseState, State
  6. class UploadBaseState(BaseState):
  7. """The base state for uploading a file."""
  8. class UploadState(BaseState):
  9. """The base state for uploading a file."""
  10. async def handle_upload1(self, files: list[rx.UploadFile]):
  11. """Handle the upload of a file.
  12. Args:
  13. files: The uploaded files.
  14. """
  15. pass
  16. class SubUploadState(UploadBaseState):
  17. """The test substate."""
  18. img: str
  19. async def handle_upload(self, files: list[rx.UploadFile]):
  20. """Handle the upload of a file.
  21. Args:
  22. files: The uploaded files.
  23. """
  24. pass
  25. class FileUploadState(State):
  26. """The base state for uploading a file."""
  27. img_list: list[str]
  28. _tmp_path: ClassVar[Path]
  29. async def handle_upload2(self, files):
  30. """Handle the upload of a file.
  31. Args:
  32. files: The uploaded files.
  33. """
  34. pass
  35. async def multi_handle_upload(self, files: list[rx.UploadFile]):
  36. """Handle the upload of a file.
  37. Args:
  38. files: The uploaded files.
  39. """
  40. for file in files:
  41. upload_data = await file.read()
  42. assert file.name is not None
  43. outfile = self._tmp_path / file.name
  44. # Save the file.
  45. outfile.write_bytes(upload_data)
  46. # Update the img var.
  47. self.img_list.append(file.name)
  48. @rx.event(background=True)
  49. async def bg_upload(self, files: list[rx.UploadFile]):
  50. """Background task cannot be upload handler.
  51. Args:
  52. files: The uploaded files.
  53. """
  54. pass
  55. class FileStateBase1(State):
  56. """The base state for a child FileUploadState."""
  57. pass
  58. class ChildFileUploadState(FileStateBase1):
  59. """The child state for uploading a file."""
  60. img_list: list[str]
  61. _tmp_path: ClassVar[Path]
  62. async def handle_upload2(self, files):
  63. """Handle the upload of a file.
  64. Args:
  65. files: The uploaded files.
  66. """
  67. pass
  68. async def multi_handle_upload(self, files: list[rx.UploadFile]):
  69. """Handle the upload of a file.
  70. Args:
  71. files: The uploaded files.
  72. """
  73. for file in files:
  74. upload_data = await file.read()
  75. assert file.name is not None
  76. outfile = self._tmp_path / file.name
  77. # Save the file.
  78. outfile.write_bytes(upload_data)
  79. # Update the img var.
  80. self.img_list.append(file.name)
  81. @rx.event(background=True)
  82. async def bg_upload(self, files: list[rx.UploadFile]):
  83. """Background task cannot be upload handler.
  84. Args:
  85. files: The uploaded files.
  86. """
  87. pass
  88. class FileStateBase2(FileStateBase1):
  89. """The parent state for a grandchild FileUploadState."""
  90. pass
  91. class GrandChildFileUploadState(FileStateBase2):
  92. """The child state for uploading a file."""
  93. img_list: list[str]
  94. _tmp_path: ClassVar[Path]
  95. async def handle_upload2(self, files):
  96. """Handle the upload of a file.
  97. Args:
  98. files: The uploaded files.
  99. """
  100. pass
  101. async def multi_handle_upload(self, files: list[rx.UploadFile]):
  102. """Handle the upload of a file.
  103. Args:
  104. files: The uploaded files.
  105. """
  106. for file in files:
  107. upload_data = await file.read()
  108. assert file.name is not None
  109. outfile = self._tmp_path / file.name
  110. # Save the file.
  111. outfile.write_bytes(upload_data)
  112. # Update the img var.
  113. self.img_list.append(file.name)
  114. @rx.event(background=True)
  115. async def bg_upload(self, files: list[rx.UploadFile]):
  116. """Background task cannot be upload handler.
  117. Args:
  118. files: The uploaded files.
  119. """
  120. pass