def get_template_by_show_version(self, request, *args, **kwargs): req_data = copy.deepcopy(self.kwargs) req_data["project_id"] = request.project.project_id serializer = GetShowVersionSLZ(data=req_data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data self.can_view_template(request, validated_data["template"]) serializer = serializers.GetFormTemplateSLZ(validated_data) return Response(serializer.data)
def get_template_by_show_version(self, request, project_id, template_id, show_version_id): serializer = GetShowVersionSLZ(data=self.kwargs) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data template = validated_data["template"] self.can_view_template(request, template) with_file_content = request.query_params.get("with_file_content") with_file_content = False if with_file_content == "false" else True serializer = serializers.GetTemplateFilesSLZ(validated_data, context={"with_file_content": with_file_content}) return Response(serializer.data)
class CreateTemplateReleaseSLZ(InstanceEntitySLZ): show_version = GetShowVersionSLZ() namespace_id = serializers.CharField() namespace_variables = serializers.JSONField(required=False) is_start = serializers.BooleanField(default=True) def validate(self, data): show_version_slz_data = data["show_version"] show_version_obj = show_version_slz_data["show_version"] ventity = models.VersionedEntity.objects.get( id=show_version_obj.real_version_id) data["version_id"] = ventity.id instance_resources_id_map = ventity.instance_resources_id_map req_instance_resources = data.get("instance_entity") data["instance_entity"] = generate_instance_entity( req_instance_resources, instance_resources_id_map) template = show_version_slz_data["template"] data.update({ "template_id": template.id, "template_name": template.name, "template": template, "project_id": show_version_slz_data["project_id"], }) instantiated_ns_name = get_instantiated_ns( self.context["access_token"], data["template_id"], data["namespace_id"], data["project_id"], req_instance_resources, ) if instantiated_ns_name: raise ValidationError( f"namespace {instantiated_ns_name} has been instantiated") data.update({ "namespaces": data["namespace_id"], "ns_list": [data["namespace_id"]], "show_version_id": show_version_slz_data["show_version_id"], "show_version_name": show_version_obj.name, "variable_info": { data["namespace_id"]: data.get("namespace_variables", {}) }, }) return data
class TemplateReleaseSLZ(serializers.Serializer): show_version = GetShowVersionSLZ() namespace_id = serializers.IntegerField() template_files = serializers.ListField(child=PreviewTemplateFileSLZ(), allow_empty=False) is_preview = serializers.BooleanField() template_variables = serializers.JSONField(default={}) def to_internal_value(self, data): data = super().to_internal_value(data) template_files = [] for res_file in data["template_files"]: res_file_ids = [f["id"] for f in res_file["files"]] res_file = res2files.get_resource_file(res_file["resource_name"], res_file_ids, "id", "name", "content") template_files.append(res_file) data["template_files"] = template_files return data
class TemplateReleaseSLZ(serializers.Serializer): show_version = GetShowVersionSLZ() namespace_id = serializers.IntegerField() template_files = serializers.ListField(child=PreviewTemplateFileSLZ(), allow_empty=False) is_preview = serializers.BooleanField() def to_internal_value(self, data): data = super().to_internal_value(data) template_files = [] for res_file in data['template_files']: res_file_ids = [f['id'] for f in res_file['files']] res_file = res2files.get_resource_file(res_file['resource_name'], res_file_ids, 'id', 'name', 'content') template_files.append(res_file) data['template_files'] = template_files return data