Ejemplo n.º 1
0
 def _build(cls, model_class, *args, **kwargs):
     block = model_class()
     return blocks.StructValue(
         block,
         [(name,
           (kwargs[name] if name in kwargs else child_block.get_default()))
          for name, child_block in block.child_blocks.items()],
     )
Ejemplo n.º 2
0
    def bulk_to_python(self, values):
        """Support bulk page retrieval to reduce database queries."""
        page_ids = set(chain(*map(self.get_referenced_page_ids, values)))
        pages = Page.objects.in_bulk(page_ids)

        for value in values:
            self.replace_referenced_page_ids_with_pages(value, pages)

        return [blocks.StructValue(self, value) for value in values]
Ejemplo n.º 3
0
    def bulk_to_python(self, values):
        """Support bulk retrieval of Contacts to reduce database queries."""
        contact_model = self.child_blocks['contact'].target_model
        contacts_by_id = contact_model.objects.in_bulk(value['contact']
                                                       for value in values)

        for value in values:
            value['contact'] = contacts_by_id.get(value['contact'])

        return [blocks.StructValue(self, value) for value in values]
Ejemplo n.º 4
0
    def clean(self, value):
        """
        Gets value in the form of a string
        Converts it to a list of name/value tuples
        Returns something in the structure of a StructBlock

        """
        cleaned_data = []
        if value and isinstance(json.loads(value), dict):
            for name, value in json.loads(
                    value).items():  # child is a BoundBlock instance
                cleaned_data.append((name, value))
        return blocks.StructValue(self, cleaned_data)
Ejemplo n.º 5
0
    def bulk_to_python(self, values):
        contact_expandable_values = list(
            itertools.chain(*(value['expandables'] for value in values)))

        contacts = self.child_blocks['expandables'].child_block \
            .bulk_to_python(contact_expandable_values)

        index = 0
        for value in values:
            value_count = len(value['expandables'])
            value['expandables'] = contacts[index:index + value_count]
            index += value_count

        return [blocks.StructValue(self, value) for value in values]