upload.py 4.1 KB

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