Python wagtail.core.fields.StreamField() Examples
The following are 10
code examples of wagtail.core.fields.StreamField().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
wagtail.core.fields
, or try the search function
.
Example #1
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_lazy_load(self): """ Getting a single item should lazily load the StreamField, only accessing the database once the StreamField is accessed """ with self.assertNumQueries(1): # Get the instance. The StreamField should *not* load the image yet instance = StreamModel.objects.get(pk=self.with_image.pk) with self.assertNumQueries(0): # Access the body. The StreamField should still not get the image. body = instance.body with self.assertNumQueries(1): # Access the image item from the stream. The image is fetched now body[0].value with self.assertNumQueries(0): # Everything has been fetched now, no further database queries. self.assertEqual(body[0].value, self.image) self.assertEqual(body[1].value, 'foo')
Example #2
Source File: models.py From securethenews with GNU Affero General Public License v3.0 | 5 votes |
def editor_css(): # Make 'heading' StreamField blocks look like h2 in RichTextBlocks in the # Wagtail Admin. return ( ''' <style> .fieldname-heading input { color: #666; font-family: Roboto Slab, Georgia, serif; font-size: 2.4em; font-weight: bold; } </style> ''' )
Example #3
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_lazy_load_no_images(self): """ Getting a single item whose StreamField never accesses the database should behave as expected. """ with self.assertNumQueries(1): # Get the instance, nothing else instance = StreamModel.objects.get(pk=self.no_image.pk) with self.assertNumQueries(0): # Access the body. The StreamField has no images, so nothing should # happen body = instance.body self.assertEqual(body[0].value, 'foo')
Example #4
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_lazy_load_queryset(self): """ Ensure that lazy loading StreamField works when gotten as part of a queryset list """ with self.assertNumQueries(1): instances = StreamModel.objects.filter( pk__in=[self.with_image.pk, self.no_image.pk]) instances_lookup = {instance.pk: instance for instance in instances} with self.assertNumQueries(1): instances_lookup[self.with_image.pk].body[0] with self.assertNumQueries(0): instances_lookup[self.no_image.pk].body[0]
Example #5
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_lazy_load_queryset_bulk(self): """ Ensure that lazy loading StreamField works when gotten as part of a queryset list """ file_obj = get_test_image_file() image_1 = Image.objects.create(title='Test image 1', file=file_obj) image_3 = Image.objects.create(title='Test image 3', file=file_obj) with_image = StreamModel.objects.create(body=json.dumps([ {'type': 'image', 'value': image_1.pk}, {'type': 'image', 'value': None}, {'type': 'image', 'value': image_3.pk}, {'type': 'text', 'value': 'foo'}])) with self.assertNumQueries(1): instance = StreamModel.objects.get(pk=with_image.pk) # Prefetch all image blocks with self.assertNumQueries(1): instance.body[0] # 1. Further image block access should not execute any db lookups # 2. The blank block '1' should be None. # 3. The values should be in the original order. with self.assertNumQueries(0): assert instance.body[0].value.title == 'Test image 1' assert instance.body[1].value is None assert instance.body[2].value.title == 'Test image 3'
Example #6
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_lazy_load_get_prep_value(self): """ Saving a lazy StreamField that hasn't had its data accessed should not cause extra database queries by loading and then re-saving block values. Instead the initial JSON stream data should be written back for any blocks that have not been accessed. """ with self.assertNumQueries(1): instance = StreamModel.objects.get(pk=self.with_image.pk) # Expect a single UPDATE to update the model, without any additional # SELECT related to the image block that has not been accessed. with self.assertNumQueries(1): instance.save()
Example #7
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_can_read_non_json_content(self): """StreamField columns should handle non-JSON database content gracefully""" self.assertIsInstance(self.nonjson_body.body, StreamValue) # the main list-like content of the StreamValue should be blank self.assertFalse(self.nonjson_body.body) # the unparsed text content should be available in raw_text self.assertEqual(self.nonjson_body.body.raw_text, "<h1>hello world</h1>")
Example #8
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_non_blank_field_is_required(self): field = StreamField([('paragraph', blocks.CharBlock())], blank=False) self.assertTrue(field.stream_block.required)
Example #9
Source File: test_streamfield.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_blank_field_is_not_required(self): field = StreamField([('paragraph', blocks.CharBlock())], blank=True) self.assertFalse(field.stream_block.required)
Example #10
Source File: actions.py From wagtail-graphql with MIT License | 5 votes |
def _add_streamfields(cls: wagtailPage, node: str, dict_params: dict, app: str, prefix: str) -> None: from .types.streamfield import ( block_handler, stream_field_handler, ) for field in cls._meta.fields: if isinstance(field, StreamField): field_name = field.name stream_field_name = f"{node}{string.capwords(field_name, sep='_').replace('_', '')}" blocks = field.stream_block.child_blocks handlers = dict( (name, block_handler(block, app, prefix)) for name, block in blocks.items() ) f, resolve = stream_field_handler( stream_field_name, field_name, handlers ) dict_params.update({ field.name: f, "resolve_" + field.name: resolve })