Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1 | from django.forms import widgets |
| 2 | from rest_framework import serializers |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 3 | from plstackapi.core.models import * |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 4 | |
| 5 | |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 6 | class RoleSerializer(serializers.HyperlinkedModelSerializer): |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 7 | # HyperlinkedModelSerializer doesn't include the id by default |
| 8 | id = serializers.Field() |
| 9 | |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 10 | class Meta: |
| 11 | model = Role |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 12 | fields = ('id', |
| 13 | 'role_id', |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 14 | 'role_type') |
| 15 | |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 16 | |
| 17 | class UserSerializer(serializers.HyperlinkedModelSerializer): |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 18 | # HyperlinkedModelSerializer doesn't include the id by default |
| 19 | id = serializers.Field() |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 20 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
| 21 | slice_memberships = serializers.HyperlinkedRelatedField(view_name='slice-membership-detail') |
| 22 | site_privileges = serializers.HyperlinkedRelatedField(view_name='site-privilege-detail') |
| 23 | class Meta: |
| 24 | model = User |
| 25 | fields = ('id', |
| 26 | 'user_id', |
| 27 | 'firstname', |
| 28 | 'lastname', |
| 29 | 'email', |
| 30 | 'phone', |
| 31 | 'user_url', |
| 32 | 'is_admin', |
| 33 | 'site', |
| 34 | 'slice_memberships', |
| 35 | 'site_privileges') |
| 36 | |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 37 | class KeySerializer(serializers.HyperlinkedModelSerializer): |
| 38 | id = serializers.Field() |
| 39 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 40 | class Meta: |
| 41 | model = Key |
| 42 | fields = ('id', |
| 43 | 'name', |
| 44 | 'key', |
| 45 | 'type', |
| 46 | 'blacklisted', |
| 47 | 'user') |
| 48 | |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 49 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 50 | class SliceSerializer(serializers.HyperlinkedModelSerializer): |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 51 | # HyperlinkedModelSerializer doesn't include the id by default |
| 52 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 53 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 54 | slivers = serializers.HyperlinkedRelatedField(view_name='sliver-detail') |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 55 | subnet= serializers.HyperlinkedRelatedField(view_name='subnet-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 56 | class Meta: |
| 57 | model = Slice |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 58 | fields = ('id', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 59 | 'tenant_id', |
| 60 | 'enabled', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 61 | 'name', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 62 | 'url', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 63 | 'instantiation', |
| 64 | 'omf_friendly', |
| 65 | 'description', |
| 66 | 'slice_url', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 67 | 'network_id', |
| 68 | 'router_id', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 69 | 'site', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 70 | 'slivers', |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 71 | 'subnet', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 72 | 'updated', |
| 73 | 'created') |
| 74 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 75 | class SliceMembershipSerializer(serializers.HyperlinkedModelSerializer): |
| 76 | id = serializers.Field() |
| 77 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 78 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 79 | role = serializers.HyperlinkedRelatedField(view_name='role-detail') |
| 80 | class Meta: |
| 81 | model = SitePrivilege |
| 82 | fields = ('id', |
| 83 | 'user', |
| 84 | 'slice', |
| 85 | 'role') |
| 86 | |
| 87 | class SubnetSerializer(serializers.HyperlinkedModelSerializer): |
| 88 | id = serializers.Field() |
| 89 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 90 | class Meta: |
| 91 | model = Subnet |
| 92 | fields = ('id', |
| 93 | 'subnet_id', |
| 94 | 'cidr', |
| 95 | 'ip_version', |
| 96 | 'start', |
| 97 | 'end', |
| 98 | 'slice') |
| 99 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 100 | class SiteSerializer(serializers.HyperlinkedModelSerializer): |
| 101 | |
| 102 | #Experimenting with whether to use ids, hyperlinks, or nested includes |
| 103 | #slices = serializers.PrimaryKeyRelatedField(many=True, read_only=True) |
| 104 | #slices = serializers.RelatedField(many=True, read_only=True) |
| 105 | #slices = SliceSerializer(many=True) |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 106 | # HyperlinkedModelSerializer doesn't include the id by default |
| 107 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 108 | slices = serializers.HyperlinkedRelatedField(many=True, read_only=True,view_name='slice-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 109 | |
| 110 | class Meta: |
| 111 | model = Site |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 112 | fields = ('id', |
| 113 | 'url', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 114 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 115 | 'slices', |
| 116 | 'site_url', |
| 117 | 'enabled', |
| 118 | 'longitude', |
| 119 | 'latitude', |
| 120 | 'login_base', |
Tony Mack | e87bffa | 2013-04-08 23:18:04 -0400 | [diff] [blame] | 121 | 'tenant_id', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 122 | 'is_public', |
| 123 | 'abbreviated_name', |
| 124 | 'updated', |
| 125 | 'created') |
| 126 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 127 | class SitePrivilegeSerializer(serializers.HyperlinkedModelSerializer): |
| 128 | id = serializers.Field() |
| 129 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
| 130 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 131 | role = serializers.HyperlinkedRelatedField(view_name='role-detail') |
| 132 | class Meta: |
| 133 | model = SitePrivilege |
| 134 | fields = ('id', |
| 135 | 'user', |
| 136 | 'site', |
| 137 | 'role') |
| 138 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 139 | class DeploymentNetworkSerializer(serializers.HyperlinkedModelSerializer): |
| 140 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 141 | # HyperlinkedModelSerializer doesn't include the id by default |
| 142 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 143 | sites = serializers.HyperlinkedRelatedField(view_name='deploymentnetwork-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 144 | class Meta: |
| 145 | model = DeploymentNetwork |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 146 | fields = ('id', |
| 147 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 148 | 'sites' |
| 149 | ) |
| 150 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 151 | class SliverSerializer(serializers.HyperlinkedModelSerializer): |
| 152 | # HyperlinkedModelSerializer doesn't include the id by default |
| 153 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 154 | flavor = serializers.HyperlinkedRelatedField(view_name='flavor-detail') |
| 155 | image = serializers.HyperlinkedRelatedField(view_name='image-detail') |
| 156 | key = serializers.HyperlinkedRelatedField(view_name='key-detail') |
| 157 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 158 | deployment_network = serializers.HyperlinkedRelatedField(view_name='deployment_network-detail') |
| 159 | node = serializers.HyperlinkedRelatedField(view_name='node-detail') |
| 160 | |
| 161 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 162 | #slice = serializers.PrimaryKeyRelatedField(read_only=True) |
| 163 | |
| 164 | class Meta: |
| 165 | model = Sliver |
| 166 | fields = ('id', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 167 | 'instance_id', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 168 | 'name', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 169 | 'flavor', |
| 170 | 'image', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 171 | 'key', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 172 | 'slice', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 173 | 'deploymentNetwork', |
| 174 | 'node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 175 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 176 | class NodeSerializer(serializers.HyperlinkedModelSerializer): |
| 177 | # HyperlinkedModelSerializer doesn't include the id by default |
| 178 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 179 | class Meta: |
| 180 | model = Node |
| 181 | fields = ('id', |
| 182 | 'name') |
| 183 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 184 | class ImageSerializer(serializers.HyperlinkedModelSerializer): |
| 185 | # HyperlinkedModelSerializer doesn't include the id by default |
| 186 | id = serializers.Field() |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 187 | class Meta: |
| 188 | model = Image |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 189 | fields = ('id', |
| 190 | 'image_id', |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 191 | 'name', |
| 192 | 'disk_format', |
| 193 | 'container_format') |
| 194 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 195 | class FlavorSerializer(serializers.HyperlinkedModelSerializer): |
| 196 | # HyperlinkedModelSerializer doesn't include the id by default |
| 197 | id = serializers.Field() |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 198 | class Meta: |
| 199 | model = Flavor |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 200 | fields = ('id', |
| 201 | 'flavor_id', |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 202 | 'name', |
| 203 | 'memory_mb', |
| 204 | 'disk_gb', |
| 205 | 'vcpus') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 206 | |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 207 | serializerLookUp = { |
| 208 | Role: RoleSerializer, |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 209 | User: UserSerializer, |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 210 | Key: KeySerializer, |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 211 | Site: SiteSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 212 | SitePrivilege: SitePrivilegeSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 213 | Slice: SliceSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 214 | SliceMembership: SliceMembershipSerializer, |
| 215 | Subnet: SubnetSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 216 | Node: NodeSerializer, |
| 217 | Sliver: SliverSerializer, |
| 218 | DeploymentNetwork: DeploymentNetworkSerializer, |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 219 | Image: ImageSerializer, |
| 220 | Flavor: FlavorSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 221 | None: None, |
| 222 | } |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 223 | |