Пример #1
0
class ContentTypeSerializer(serializers.HyperlinkedModelSerializer):
    if drf_version() >= (3, 0):
        name = serializers.CharField(
        )  # in DRF 3, `name` is no longer automatically generated.

    class Meta:
        model = ContentType
Пример #2
0

class JsonField(ToNativeToRepresentationMixin, WritableField):
    def to_native(self, value):
        """
        :return:Returns the value itself, not a string representation.
        """
        return value

    def from_native(self, value):
        if value in validators.EMPTY_VALUES:
            return None
        return value


if drf_version() < (3, 0):

    class NodbSerializer(serializers.ModelSerializer):
        field_mapping = dict(
            serializers.ModelSerializer.field_mapping.items() +
            [(nodb.models.JsonField, JsonField)])

else:

    class NodbSerializer(serializers.ModelSerializer):
        serializer_field_mapping = dict(
            serializers.ModelSerializer.serializer_field_mapping.items() +
            [(nodb.models.JsonField, JsonField)])


class NodbViewSet(NoCacheModelViewSet):
Пример #3
0
class CephPoolSerializer(NodbSerializer):
    class Meta:
        model = CephPool

    erasure_code_profile = \
        serializers.PrimaryKeyRelatedField(default=None, required=False,
                                           queryset=CephErasureCodeProfile.objects.all())
    quota_max_objects = serializers.IntegerField(default=0)
    quota_max_bytes = serializers.IntegerField(default=0)
    #    crush_ruleset = serializers.IntegerField() # TODO OP-1415
    size = serializers.IntegerField(default=None, required=False)
    min_size = serializers.IntegerField(default=None, required=False)
    crash_replay_interval = serializers.IntegerField(default=0)
    cache_mode = serializers.CharField(default='none')
    if drf_version() >= (3, 0):
        # DRF 3 requires `allow_null=True` and DRF 2 cannot cope with `allow_null`
        tier_of = serializers.PrimaryKeyRelatedField(
            default=None,
            required=False,
            allow_null=True,
            queryset=CephPool.objects.all())
        write_tier = serializers.PrimaryKeyRelatedField(
            default=None,
            required=False,
            allow_null=True,
            queryset=CephPool.objects.all())
        read_tier = serializers.PrimaryKeyRelatedField(
            default=None,
            required=False,
            allow_null=True,
            queryset=CephPool.objects.all())
    else:
        tier_of = serializers.PrimaryKeyRelatedField(
            default=None, required=False, queryset=CephPool.objects.all())
        write_tier = serializers.PrimaryKeyRelatedField(
            default=None, required=False, queryset=CephPool.objects.all())
        read_tier = serializers.PrimaryKeyRelatedField(
            default=None, required=False, queryset=CephPool.objects.all())

    target_max_bytes = serializers.IntegerField(default=0)
    hit_set_period = serializers.IntegerField(default=0)
    hit_set_count = serializers.IntegerField(default=0)

    def validate(self, data):
        errors = {}
        if 'type' in data:
            if data['type'] == 'replicated':
                errors = {
                    field: ['Replicated pools need ' + field]
                    for field in ['size', 'min_size']
                    if field not in data or data[field] is None
                }
            else:
                errors = {
                    field: ['Erasure coded pools need ' + field]
                    for field in ['erasure_code_profile']
                    if field not in data or data[field] is None
                }
        if errors:
            raise serializers.ValidationError(errors)
        return data
Пример #4
0
# -*- coding: utf-8 -*-
"""
 *   Copyright (c) 2016 SUSE LLC
 *
 *  openATTIC is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2.
 *
 *  This package is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
"""
from rest_framework.pagination import PageNumberPagination

from rest.utilities import drf_version

assert drf_version() >= (3, 0)


class PageSizePageNumberPagination(PageNumberPagination):
    """
    This class must be declared in a module without any reference to any Views, otherwise
    you get a circular import.
    """
    page_size_query_param = "pageSize"