Skip to content

Commit

Permalink
feat: add MultipleTargets to support multiple inputs for one field
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantgoel committed Nov 5, 2024
1 parent d3e9fbc commit 310c34a
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions streaming_form_data/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class BaseTarget:
"""
Targets determine what to do with some input once the parser is done
processing it. Any new Target should inherit from this base class and
override the `data_received` function.
Targets determine what to do with some input once the parser is done processing it.
Any new Target should inherit from this base class and override the `data_received`
method.
Attributes:
multipart_filename:
Expand Down Expand Up @@ -60,6 +60,36 @@ def set_multipart_content_type(self, content_type: str):
self.multipart_content_type = content_type


class MultipleTargets(BaseTarget):
"""
Target class that enables processing multiple inputs for one field
"""

def __init__(self, next_target: Callable):
self._next_target = next_target

self._targets = []
self._validator = None # next_target should have a validator

def on_start(self):
target = self._next_target()

self._targets.append(target)
target.start()

def on_data_received(self, chunk: bytes):
self._targets[-1].data_received(chunk)

def on_finish(self):
self._targets[-1].finish()

def set_multipart_filename(self, filename: str):
self._targets[-1].set_multipart_filename(filename)

def set_multipart_content_type(self, content_type: str):
self._targets[-1].set_multipart_content_type(content_type)


class NullTarget(BaseTarget):
"""
NullTarget ignores whatever input is passed in.
Expand Down

0 comments on commit 310c34a

Please sign in to comment.