upload.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """Test states for upload-related tests."""
  2. from pathlib import Path
  3. from typing import ClassVar, List
  4. import reflex as rx
  5. class UploadState(rx.State):
  6. """The base state for uploading a file."""
  7. async def handle_upload1(self, files: List[rx.UploadFile]):
  8. """Handle the upload of a file.
  9. Args:
  10. files: The uploaded files.
  11. """
  12. pass
  13. class BaseState(rx.State):
  14. """The test base state."""
  15. pass
  16. class SubUploadState(BaseState):
  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(rx.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. for file in files:
  35. upload_data = await file.read()
  36. outfile = f"{self._tmp_path}/{file.filename}"
  37. # Save the file.
  38. with open(outfile, "wb") as file_object:
  39. file_object.write(upload_data)
  40. # Update the img var.
  41. self.img_list.append(file.filename)
  42. async def multi_handle_upload(self, files: List[rx.UploadFile]):
  43. """Handle the upload of a file.
  44. Args:
  45. files: The uploaded files.
  46. """
  47. for file in files:
  48. upload_data = await file.read()
  49. outfile = f"{self._tmp_path}/{file.filename}"
  50. # Save the file.
  51. with open(outfile, "wb") as file_object:
  52. file_object.write(upload_data)
  53. # Update the img var.
  54. assert file.filename is not None
  55. self.img_list.append(file.filename)
  56. class FileStateBase1(rx.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. for file in files:
  69. upload_data = await file.read()
  70. outfile = f"{self._tmp_path}/{file.filename}"
  71. # Save the file.
  72. with open(outfile, "wb") as file_object:
  73. file_object.write(upload_data)
  74. # Update the img var.
  75. self.img_list.append(file.filename)
  76. async def multi_handle_upload(self, files: List[rx.UploadFile]):
  77. """Handle the upload of a file.
  78. Args:
  79. files: The uploaded files.
  80. """
  81. for file in files:
  82. upload_data = await file.read()
  83. outfile = f"{self._tmp_path}/{file.filename}"
  84. # Save the file.
  85. with open(outfile, "wb") as file_object:
  86. file_object.write(upload_data)
  87. # Update the img var.
  88. assert file.filename is not None
  89. self.img_list.append(file.filename)
  90. class FileStateBase2(FileStateBase1):
  91. """The parent state for a grandchild FileUploadState."""
  92. pass
  93. class GrandChildFileUploadState(FileStateBase2):
  94. """The child state for uploading a file."""
  95. img_list: List[str]
  96. _tmp_path: ClassVar[Path]
  97. async def handle_upload2(self, files):
  98. """Handle the upload of a file.
  99. Args:
  100. files: The uploaded files.
  101. """
  102. for file in files:
  103. upload_data = await file.read()
  104. outfile = f"{self._tmp_path}/{file.filename}"
  105. # Save the file.
  106. with open(outfile, "wb") as file_object:
  107. file_object.write(upload_data)
  108. # Update the img var.
  109. self.img_list.append(file.filename)
  110. async def multi_handle_upload(self, files: List[rx.UploadFile]):
  111. """Handle the upload of a file.
  112. Args:
  113. files: The uploaded files.
  114. """
  115. for file in files:
  116. upload_data = await file.read()
  117. outfile = f"{self._tmp_path}/{file.filename}"
  118. # Save the file.
  119. with open(outfile, "wb") as file_object:
  120. file_object.write(upload_data)
  121. # Update the img var.
  122. assert file.filename is not None
  123. self.img_list.append(file.filename)