def get_image(self, obj): if obj.source.is_volume(): return {} image_uuid = obj.application_uuid() image = Image.objects.get(uuid=image_uuid) serializer = ImageSummarySerializer(image, context=self.context) return serializer.data
class InstanceStatusHistorySerializer(serializers.HyperlinkedModelSerializer): instance = ModelRelatedField( queryset=Instance.objects.all(), serializer_class=InstanceSuperSummarySerializer, style={'base_template': 'input.html'}) size = SizeRelatedField(queryset=Size.objects.none()) provider = ProviderSummarySerializer(source='instance.get_provider') image = ImageSummarySerializer( source='instance.provider_machine.application_version.application') status = serializers.SlugRelatedField(slug_field='name', read_only=True) activity = serializers.CharField(max_length=36, allow_blank=True) url = UUIDHyperlinkedIdentityField( view_name='api:v2:instancestatushistory-detail', ) class Meta: model = InstanceStatusHistory fields = ( 'id', 'uuid', 'url', 'instance', 'status', 'activity', 'size', 'provider', 'image', 'extra', 'start_date', 'end_date', )
class InstanceStatusHistorySerializer(serializers.HyperlinkedModelSerializer): instance = InstanceRelatedField(queryset=Instance.objects.none()) size = SizeRelatedField(queryset=Size.objects.none()) provider = ProviderSummarySerializer( source='instance.provider_machine.provider') image = ImageSummarySerializer( source='instance.provider_machine.application_version.application') status = serializers.SlugRelatedField(slug_field='name', read_only=True) url = UUIDHyperlinkedIdentityField( view_name='api:v2:instancestatushistory-detail', ) class Meta: model = InstanceStatusHistory fields = ( 'id', 'uuid', 'url', 'instance', 'status', 'size', 'provider', 'image', 'start_date', 'end_date', )
class ProviderMachineSerializer(serializers.HyperlinkedModelSerializer): # NOTE: these fields could be generalized for reuse in VolumeSerializer uuid = serializers.ReadOnlyField(source='instance_source.identifier') provider = ProviderSummarySerializer(source='instance_source.provider') version = ImageVersionSummarySerializer(source='application_version') image = ImageSummarySerializer(source='application') created_by = UserSummarySerializer(source='instance_source.created_by') start_date = serializers.DateTimeField(source='instance_source.start_date') end_date = serializers.DateTimeField(source='instance_source.end_date', allow_null=True) licenses = LicenseSummarySerializer(many=True, read_only=True) # NEW members = serializers.SlugRelatedField(slug_field='name', read_only=True, many=True) # NEW # NOTE: this is still using ID instead of UUID -- due to abstract classes and use of getattr in L271 of rest_framework/relations.py, this is a 'kink' that has not been worked out yet. url = InstanceSourceHyperlinkedIdentityField( view_name='api:v2:providermachine-detail', ) class Meta: model = ProviderMachine fields = ('id', 'uuid', 'url', 'provider', 'image', 'licenses', 'members', 'version', 'created_by', 'start_date', 'end_date') def update(self, instance, validated_data): if 'instance_source' in validated_data: source = instance.instance_source source_data = validated_data.pop('instance_source') for key, val in source_data.items(): setattr(source, key, val) source.save() for (key, val) in validated_data.items(): setattr(instance, key, val) instance.save() return instance
class ProviderMachineSerializer(serializers.HyperlinkedModelSerializer): uuid = serializers.ReadOnlyField(source='instance_source.identifier') image = ImageSummarySerializer(source='application') provider = ProviderSummarySerializer(source='instance_source.provider') created_by = UserSummarySerializer(source='instance_source.created_by') start_date = serializers.DateTimeField(source='instance_source.start_date') end_date = serializers.DateTimeField(source='instance_source.end_date') class Meta: model = ProviderMachine view_name = 'api_v2:providermachine-detail' fields = ('id', 'uuid', 'url', 'image', 'provider', 'created_by', 'start_date', 'end_date')
class ProjectSerializer(serializers.HyperlinkedModelSerializer): instances = InstanceSummarySerializer(many=True, read_only=True) volumes = VolumeSummarySerializer(many=True, read_only=True) images = ImageSummarySerializer(source='applications', many=True, read_only=True) # note: both of these requests become a single DB query, but I'm choosing the # owner.name route so the API doesn't break when we start adding users to groups # owner = UserSerializer(source='owner.user_set.first') owner = UserRelatedField(source='owner.name') class Meta: model = Project view_name = 'api_v2:project-detail' fields = ('id', 'url', 'name', 'description', 'owner', 'instances', 'volumes', 'images', 'start_date', 'end_date')
class ProjectSerializer(serializers.HyperlinkedModelSerializer): images = ImageSummarySerializer(source='applications', many=True, read_only=True) instances = InstanceSummarySerializer(source='active_instances', many=True, read_only=True) links = ExternalLinkSummarySerializer(many=True, read_only=True) volumes = VolumeSummarySerializer(source='active_volumes', many=True, read_only=True) # note: both of these requests become a single DB query, but I'm choosing # the owner.name route so the API doesn't break when we start adding users # to groups owner = UserSerializer(source='owner.user_set.first') owner = UserRelatedField(source='owner.name') url = UUIDHyperlinkedIdentityField(view_name='api:v2:project-detail', ) class Meta: model = Project fields = ('id', 'uuid', 'url', 'name', 'description', 'owner', 'instances', 'images', 'links', 'volumes', 'start_date', 'end_date')
def to_representation(self, value): volume = Image.objects.get(pk=value.pk) serializer = ImageSummarySerializer(volume, context=self.context) return serializer.data
def to_representation(self, value): serializer = ImageSummarySerializer(value, context=self.context) return serializer.data
class ProjectSerializer(serializers.HyperlinkedModelSerializer): images = ImageSummarySerializer( source='applications', many=True, read_only=True ) instances = InstanceSummarySerializer( source='active_instances', many=True, read_only=True ) links = ExternalLinkSummarySerializer(many=True, read_only=True) volumes = VolumeSummarySerializer( source='active_volumes', many=True, read_only=True ) # note: both of these requests become a single DB query, but I'm choosing # the owner.name route so the API doesn't break when we start adding users # to groups owner = UserSerializer(source='owner.user_set.first') created_by = ModelRelatedField( lookup_field="username", default=serializers.CurrentUserDefault(), queryset=AtmosphereUser.objects.all(), serializer_class=UserSummarySerializer, style={'base_template': 'input.html'} ) owner = ModelRelatedField( lookup_field="name", queryset=Group.objects.all(), serializer_class=GroupSummarySerializer, style={'base_template': 'input.html'} ) url = UUIDHyperlinkedIdentityField(view_name='api:v2:project-detail', ) users = UserSummarySerializer(source='get_users', many=True, read_only=True) leaders = UserSummarySerializer( source='get_leaders', many=True, read_only=True ) def update(self, instance, validated_data): """ Check that project does not have shared resources. If so, do not allow owner changes. """ new_owner = validated_data.get('owner') current_owner = instance.owner if new_owner and current_owner != new_owner: self.validate_ownership(instance, current_owner) return super(ProjectSerializer, self).update(instance, validated_data) def validate_ownership(self, project, current_owner): if project.has_shared_resources(): raise ValidationError( "Cannot update ownership for a project when it contains " "shared cloud resources. To update project ownership, " "all shared cloud resources must be " "moved to another project or deleted" ) request = self.context.get('request') if not request: return True request_user = request.user if request_user not in project.get_leaders(): raise ValidationError( "This project has been shared with you. " "To change a projects ownership, " "you must be the project owner." ) return True class Meta: model = Project fields = ( 'id', 'uuid', 'url', 'name', 'description', 'created_by', 'owner', 'users', 'leaders', 'instances', 'images', 'links', 'volumes', 'start_date', 'end_date' )