# Path: jetstream/blocks.py
# class FeatureCustomizedStreamBlock(blocks.StreamBlock):
#     """
#     Identical to StreamBlock, except that we override the constructor to make it save self._base_blocks and
#     self._dependencies, instead of self.base_blocks and self.dependencies. This lets us replace them with @properties.
#     """
# 
#     def __init__(self, local_blocks=None, **kwargs):
#         self._constructor_kwargs = kwargs
# 
#         # Note, this is calling BaseStreamBlock's super __init__, not FeatureCustomizedStreamBlock's. We don't want
#         # BaseStreamBlock.__init__() to run, because it tries to assign to self.child_blocks, which it can't do because
#         # we've overridden it with an @property. But we DO want Block.__init__() to run.
#         super(BaseStreamBlock, self).__init__(**kwargs)
# 
#         # create a local (shallow) copy of base_blocks so that it can be supplemented by local_blocks
#         self._child_blocks = self.base_blocks.copy()
#         if local_blocks:
#             for name, block in local_blocks:
#                 block.set_name(name)
#                 self._child_blocks[name] = block
# 
#         self._dependencies = self._child_blocks.values()
# 
#     @property
#     def child_blocks(self):
#         request = get_current_request()
#         # Protect against crashing in case this ever runs outside of a request cycle.
#         if request is None:
#             return self._child_blocks
# 
#         # Protect against crashing of the site has no features (like the initial site built by Wagtail migration)
#         # noinspection PyUnresolvedReferences
#         try:
#             features = Site.find_for_request(request).features
#             return OrderedDict([
#                 item for item in self._child_blocks.items() if features.feature_is_enabled(item[0])
#             ])
#         except Site.features.RelatedObjectDoesNotExist:
#             return self._child_blocks
# 
#     @property
#     def dependencies(self):
#         request = get_current_request()
#         # Protect against crashing in case this ever runs outside of a request cycle.
#         if request is None:
#             return self._child_blocks
# 
#         # Protect against crashing of the site has no features (like the initial site built by Wagtail migration)
#         # noinspection PyUnresolvedReferences
#         try:
#             features = Site.find_for_request(request).features
#             return [block for block in self._dependencies if features.feature_is_enabled(block.name)]
#         except Site.features.RelatedObjectDoesNotExist:
#             return self._child_blocks


from wagtail.core.blocks import Block
from wagtail.core.fields import StreamField
from jetstream.blocks import FeatureCustomizedStreamBlock



class FeatureCustomizedStreamField(StreamField):

    def __init__(self, block_types, **kwargs):
        super(StreamField, self).__init__(**kwargs)
        if isinstance(block_types, Block):
            self.stream_block = block_types
        elif isinstance(block_types, type):
            self.stream_block = block_types(required=not self.blank)
        else:

# Path: jetstream/blocks.py
# class FeatureCustomizedStreamBlock(blocks.StreamBlock):
#     """
#     Identical to StreamBlock, except that we override the constructor to make it save self._base_blocks and
#     self._dependencies, instead of self.base_blocks and self.dependencies. This lets us replace them with @properties.
#     """
# 
#     def __init__(self, local_blocks=None, **kwargs):
#         self._constructor_kwargs = kwargs
# 
#         # Note, this is calling BaseStreamBlock's super __init__, not FeatureCustomizedStreamBlock's. We don't want
#         # BaseStreamBlock.__init__() to run, because it tries to assign to self.child_blocks, which it can't do because
#         # we've overridden it with an @property. But we DO want Block.__init__() to run.
#         super(BaseStreamBlock, self).__init__(**kwargs)
# 
#         # create a local (shallow) copy of base_blocks so that it can be supplemented by local_blocks
#         self._child_blocks = self.base_blocks.copy()
#         if local_blocks:
#             for name, block in local_blocks:
#                 block.set_name(name)
#                 self._child_blocks[name] = block
# 
#         self._dependencies = self._child_blocks.values()
# 
#     @property
#     def child_blocks(self):
#         request = get_current_request()
#         # Protect against crashing in case this ever runs outside of a request cycle.
#         if request is None:
#             return self._child_blocks
# 
#         # Protect against crashing of the site has no features (like the initial site built by Wagtail migration)
#         # noinspection PyUnresolvedReferences
#         try:
#             features = Site.find_for_request(request).features
#             return OrderedDict([
#                 item for item in self._child_blocks.items() if features.feature_is_enabled(item[0])
#             ])
#         except Site.features.RelatedObjectDoesNotExist:
#             return self._child_blocks
# 
#     @property
#     def dependencies(self):
#         request = get_current_request()
#         # Protect against crashing in case this ever runs outside of a request cycle.
#         if request is None:
#             return self._child_blocks
# 
#         # Protect against crashing of the site has no features (like the initial site built by Wagtail migration)
#         # noinspection PyUnresolvedReferences
#         try:
#             features = Site.find_for_request(request).features
#             return [block for block in self._dependencies if features.feature_is_enabled(block.name)]
#         except Site.features.RelatedObjectDoesNotExist:
#             return self._child_blocks


# Path: jetstream/fields.py
from wagtail.core.blocks import Block
from wagtail.core.fields import StreamField
from jetstream.blocks import FeatureCustomizedStreamBlock




class FeatureCustomizedStreamField(StreamField):

    def __init__(self, block_types, **kwargs):
        super(StreamField, self).__init__(**kwargs)
        if isinstance(block_types, Block):
            self.stream_block = block_types
        elif isinstance(block_types, type):
            self.stream_block = block_types(required=not self.blank)
        else:

            self.stream_block = FeatureCustomizedStreamBlock(block_types, required=not self.blank)