Skip to content

Commit c7fb73f

Browse files
authored
Add conditional checks when resolving streamfield type (#29)
1 parent e8dd9a1 commit c7fb73f

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

grapple/actions.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,17 @@ def convert_to_underscore(name):
278278

279279

280280
def streamfield_resolver(self, instance, info, **kwargs):
281-
field_name = convert_to_underscore(info.field_name)
282-
block = instance.block.child_blocks[field_name]
283-
value = instance.value[field_name]
281+
value = None
282+
if hasattr(instance, "block"):
283+
field_name = convert_to_underscore(info.field_name)
284+
block = instance.block.child_blocks[field_name]
285+
value = instance.value[field_name]
284286

285-
if issubclass(type(block), ImageChooserBlock) and isinstance(value, int):
286-
return block.to_python(value)
287+
if not block or not value:
288+
return None
289+
290+
if issubclass(type(block), ImageChooserBlock) and isinstance(value, int):
291+
return block.to_python(value)
287292

288293
return value
289294

grapple/types/streamfield.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ def resolve_type(cls, instance, info):
3939
If block has a custom Graphene Node type in registry then use it,
4040
otherwise use generic block type.
4141
"""
42-
mdl = type(instance.block)
43-
if mdl in registry.streamfield_blocks:
44-
return registry.streamfield_blocks[mdl]
42+
if hasattr(instance, "block"):
43+
mdl = type(instance.block)
44+
if mdl in registry.streamfield_blocks:
45+
return registry.streamfield_blocks[mdl]
4546

46-
for block_class in inspect.getmro(mdl):
47-
if block_class in registry.streamfield_blocks:
48-
return registry.streamfield_blocks[block_class]
47+
for block_class in inspect.getmro(mdl):
48+
if block_class in registry.streamfield_blocks:
49+
return registry.streamfield_blocks[block_class]
4950

5051
return registry.streamfield_blocks["generic-block"]
5152

0 commit comments

Comments
 (0)