1
0

upload.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. assert file.filename is not None
  44. outfile = self._tmp_path / file.filename
  45. # Save the file.
  46. outfile.write_bytes(upload_data)
  47. # Update the img var.
  48. self.img_list.append(file.filename)
  49. @rx.event(background=True)
  50. async def bg_upload(self, files: List[rx.UploadFile]):
  51. """Background task cannot be upload handler.
  52. Args:
  53. files: The uploaded files.
  54. """
  55. pass
  56. class FileStateBase1(State):
  57. """The base state for a child FileUploadState."""
  58. pass
  59. class ChildFileUploadState(FileStateBase1):
  60. """The child state for uploading a file."""
  61. img_list: List[str]
  62. _tmp_path: ClassVar[Path]
  63. async def handle_upload2(self, files):
  64. """Handle the upload of a file.
  65. Args:
  66. files: The uploaded files.
  67. """
  68. pass
  69. async def multi_handle_upload(self, files: List[rx.UploadFile]):
  70. """Handle the upload of a file.
  71. Args:
  72. files: The uploaded files.
  73. """
  74. for file in files:
  75. upload_data = await file.read()
  76. assert file.filename is not None
  77. outfile = self._tmp_path / file.filename
  78. # Save the file.
  79. outfile.write_bytes(upload_data)
  80. # Update the img var.
  81. self.img_list.append(file.filename)
  82. @rx.event(background=True)
  83. async def bg_upload(self, files: List[rx.UploadFile]):
  84. """Background task cannot be upload handler.
  85. Args:
  86. files: The uploaded files.
  87. """
  88. pass
  89. class FileStateBase2(FileStateBase1):
  90. """The parent state for a grandchild FileUploadState."""
  91. pass
  92. class GrandChildFileUploadState(FileStateBase2):
  93. """The child state for uploading a file."""
  94. img_list: List[str]
  95. _tmp_path: ClassVar[Path]
  96. async def handle_upload2(self, files):
  97. """Handle the upload of a file.
  98. Args:
  99. files: The uploaded files.
  100. """
  101. pass
  102. async def multi_handle_upload(self, files: List[rx.UploadFile]):
  103. """Handle the upload of a file.
  104. Args:
  105. files: The uploaded files.
  106. """
  107. for file in files:
  108. upload_data = await file.read()
  109. assert file.filename is not None
  110. outfile = self._tmp_path / file.filename
  111. # Save the file.
  112. outfile.write_bytes(upload_data)
  113. # Update the img var.
  114. self.img_list.append(file.filename)
  115. @rx.event(background=True)
  116. async def bg_upload(self, files: List[rx.UploadFile]):
  117. """Background task cannot be upload handler.
  118. Args:
  119. files: The uploaded files.
  120. """
  121. pass