class RevealSubmitBlockSerializer(SubmitBlockSerializer): response = SettingsStreamFieldSerializer() action = SettingsStreamFieldSerializer() class Meta: fields = [ 'name', 'text', 'response', 'action', ]
class LinkCategorySerializer(serializers.Serializer): block_name = 'linkcategory' name = serializers.CharField(max_length=50) icon = serializers.CharField() stream = SettingsStreamFieldSerializer() @staticmethod def block_definition(local_blocks): return LinkCategorySerializer.block_name, blocks.StructBlock( local_blocks=[ ('name', blocks.TextBlock(max_length=50, required=True, help_text=_('Name of the category'))), ('stream', StreamBlock(local_blocks, label=_('A nested content of a category'))), ('icon', blocks.ChoiceBlock(choices=[('none', _('None'))] + icons, default=['none'], label=_('The icon'))), ]) class Meta: fields = ['name', 'stream', 'icon']
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data, *args, **kwargs) serializer.is_valid(raise_exception=True) data = serializer.validated_data form: FormBuilder = self.queryset.get(name=data['name']) action = form.find_action(action=data['action']) permission_classes = security_choices[ form.security]['permission_classes'] if any([ not perm().has_permission(request, self) for perm in permission_classes ]): raise PermissionDenied() context = {'request': request, 'form': form, 'data': data['data']} context['validated_data'] = validate_form_data(**context) def match_action(other): return action['id'] == other['id'] fields = SettingsStreamFieldSerializer( serializers={ 'form_submit': RevealSubmitBlockSerializer }).to_representation(form.stream) actions = list(filter(match_action, fields)) assert len(actions) == 1, ( 'More actions with same id found. Should not happen') result = handle(actions[0], **context) return HttpResponse(json.dumps({'stream': result}))
class CustomMenuSerializer(serializers.ModelSerializer): stream = SettingsStreamFieldSerializer() class Meta: model = CustomMenu fields = [ 'name', 'stream', ]
class ContainerSerializer(serializers.Serializer): block_name = 'container' stream = SettingsStreamFieldSerializer() @staticmethod def block_definition(local_blocks): return ContainerSerializer.block_name, ContainerBlock(local_blocks=local_blocks, icon='doc-full', label=_('Column')) class Meta: fields = ('stream',)
class GetFormBuilderSerializer(serializers.ModelSerializer): block_name = 'form' stream = SettingsStreamFieldSerializer() @staticmethod def block_definition(): return GetFormBuilderSerializer.block_name, SnippetChooserBlock(target_model='generic_forms.FormBuilder', label=_('form'), icon='form') class Meta: model = FormBuilder fields = ['name', 'display_name', 'description', 'security', 'stream']
class GroupBlockSerializer(serializers.Serializer): block_name = 'form_group' name = serializers.CharField(max_length=80) row = serializers.BooleanField() required = serializers.ChoiceField(choices=visibility_choices) stream = SettingsStreamFieldSerializer() class Meta: fields = ('name', 'row', 'required', 'stream',) @staticmethod def block_definition(): return GroupBlockSerializer.block_name, GroupBlock(stream_blocks=[ InputBlockSerializer.block_definition() ]) def validate(self, attrs): pass
class OpenDialogSerializer(serializers.Serializer): type = 'form_open_dialog' title = serializers.CharField(max_length=40) text = SettingsStreamFieldSerializer() @staticmethod def block_definition() -> tuple: return OpenDialogSerializer.type, blocks.StructBlock(local_blocks=[( 'title', blocks.TextBlock( required=True, help_text=_('title'), max_length=40) ), ('text', blocks.StreamBlock([RichTextSerializer.block_definition()]))]) class Meta: fields = [ 'title', 'text', ]
def create_extra(self, instance): ret = {} if 'stream' in self.child_extra: ret['stream'] = SettingsStreamFieldSerializer().to_representation( instance.stream) return ret