def put(self, request, project_id, version_id, resource_name, resource_id): """模板集中有 version 信息时,从 version 版本创建新的版本信息 """ validate_resource_name(resource_name) serializer = serializers_new.VentityWithTemplateSLZ(data=self.kwargs) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data ventity = validated_data['ventity'] template = validated_data['template'] self.can_edit_template(request, template) data = request.data or {} data.update({ 'version_id': version_id, 'resource_id': resource_id, 'project_id': project_id }) serializer_class = serializers_new.get_slz_class_by_resource_name( resource_name) serializer = serializer_class(data=data) serializer.is_valid(raise_exception=True) update_data = self._compose_update_data(serializer.validated_data) resource_class = get_model_class_by_resource_name(resource_name) if int(resource_id): robj = resource_class.perform_update(resource_id, **update_data) else: robj = resource_class.perform_create(**update_data) new_ventity = VersionedEntity.update_for_new_ventity( ventity.id, resource_name, resource_id, str(robj.id), **{'creator': self.request.user.username}) # model Template updated field need change when update resource template.save(update_fields=['updated']) return Response({ 'id': robj.id, 'version': new_ventity.id, 'resource_data': robj.get_res_config(is_simple=True) })
def delete(self, request, project_id, version_id, resource_name, resource_id): validate_resource_name(resource_name) serializer = serializers_new.VentityWithTemplateSLZ(data=self.kwargs) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data ventity = validated_data['ventity'] template = validated_data['template'] self.can_edit_template(request, template) # 关联关系检查 serializers_new.can_delete_resource(ventity, resource_name, resource_id) new_ventity = VersionedEntity.update_for_delete_ventity( ventity.id, resource_name, resource_id, **{'creator': self.request.user.username} ) return Response({'id': resource_id, 'version': new_ventity.id})