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() |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 9 | class Meta: |
| 10 | model = Role |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 11 | fields = ('id', |
| 12 | 'role_id', |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 13 | 'role_type') |
| 14 | |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 15 | |
| 16 | class UserSerializer(serializers.HyperlinkedModelSerializer): |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 17 | # HyperlinkedModelSerializer doesn't include the id by default |
| 18 | id = serializers.Field() |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 19 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
| 20 | slice_memberships = serializers.HyperlinkedRelatedField(view_name='slice-membership-detail') |
| 21 | site_privileges = serializers.HyperlinkedRelatedField(view_name='site-privilege-detail') |
| 22 | class Meta: |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 23 | model = PLUser |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 24 | fields = ('id', |
| 25 | 'user_id', |
| 26 | 'firstname', |
| 27 | 'lastname', |
| 28 | 'email', |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 29 | 'password', |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 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 | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 71 | 'updated', |
| 72 | 'created') |
| 73 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 74 | class SliceMembershipSerializer(serializers.HyperlinkedModelSerializer): |
| 75 | id = serializers.Field() |
| 76 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 77 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 78 | role = serializers.HyperlinkedRelatedField(view_name='role-detail') |
| 79 | class Meta: |
| 80 | model = SitePrivilege |
| 81 | fields = ('id', |
| 82 | 'user', |
| 83 | 'slice', |
| 84 | 'role') |
| 85 | |
| 86 | class SubnetSerializer(serializers.HyperlinkedModelSerializer): |
| 87 | id = serializers.Field() |
| 88 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 89 | class Meta: |
| 90 | model = Subnet |
| 91 | fields = ('id', |
| 92 | 'subnet_id', |
| 93 | 'cidr', |
| 94 | 'ip_version', |
| 95 | 'start', |
| 96 | 'end', |
| 97 | 'slice') |
| 98 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 99 | class SiteSerializer(serializers.HyperlinkedModelSerializer): |
| 100 | |
| 101 | #Experimenting with whether to use ids, hyperlinks, or nested includes |
| 102 | #slices = serializers.PrimaryKeyRelatedField(many=True, read_only=True) |
| 103 | #slices = serializers.RelatedField(many=True, read_only=True) |
| 104 | #slices = SliceSerializer(many=True) |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 105 | # HyperlinkedModelSerializer doesn't include the id by default |
| 106 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 107 | slices = serializers.HyperlinkedRelatedField(many=True, read_only=True,view_name='slice-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 108 | |
| 109 | class Meta: |
| 110 | model = Site |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 111 | fields = ('id', |
| 112 | 'url', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 113 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 114 | 'slices', |
| 115 | 'site_url', |
| 116 | 'enabled', |
| 117 | 'longitude', |
| 118 | 'latitude', |
| 119 | 'login_base', |
Tony Mack | e87bffa | 2013-04-08 23:18:04 -0400 | [diff] [blame] | 120 | 'tenant_id', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 121 | 'is_public', |
| 122 | 'abbreviated_name', |
| 123 | 'updated', |
| 124 | 'created') |
| 125 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 126 | class SitePrivilegeSerializer(serializers.HyperlinkedModelSerializer): |
| 127 | id = serializers.Field() |
| 128 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
| 129 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 130 | role = serializers.HyperlinkedRelatedField(view_name='role-detail') |
| 131 | class Meta: |
| 132 | model = SitePrivilege |
| 133 | fields = ('id', |
| 134 | 'user', |
| 135 | 'site', |
| 136 | 'role') |
| 137 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 138 | class DeploymentNetworkSerializer(serializers.HyperlinkedModelSerializer): |
| 139 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 140 | # HyperlinkedModelSerializer doesn't include the id by default |
| 141 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 142 | sites = serializers.HyperlinkedRelatedField(view_name='deploymentnetwork-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 143 | class Meta: |
| 144 | model = DeploymentNetwork |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 145 | fields = ('id', |
| 146 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 147 | 'sites' |
| 148 | ) |
| 149 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 150 | class SliverSerializer(serializers.HyperlinkedModelSerializer): |
| 151 | # HyperlinkedModelSerializer doesn't include the id by default |
| 152 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 153 | image = serializers.HyperlinkedRelatedField(view_name='image-detail') |
| 154 | key = serializers.HyperlinkedRelatedField(view_name='key-detail') |
| 155 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 156 | deployment_network = serializers.HyperlinkedRelatedField(view_name='deployment_network-detail') |
| 157 | node = serializers.HyperlinkedRelatedField(view_name='node-detail') |
| 158 | |
| 159 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 160 | #slice = serializers.PrimaryKeyRelatedField(read_only=True) |
| 161 | |
| 162 | class Meta: |
| 163 | model = Sliver |
| 164 | fields = ('id', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 165 | 'instance_id', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 166 | 'name', |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 167 | 'instance_name', |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 168 | 'ip', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 169 | 'image', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 170 | 'key', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 171 | 'slice', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 172 | 'deploymentNetwork', |
| 173 | 'node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 174 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 175 | class NodeSerializer(serializers.HyperlinkedModelSerializer): |
| 176 | # HyperlinkedModelSerializer doesn't include the id by default |
| 177 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 178 | class Meta: |
| 179 | model = Node |
| 180 | fields = ('id', |
| 181 | 'name') |
| 182 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 183 | class ImageSerializer(serializers.HyperlinkedModelSerializer): |
| 184 | # HyperlinkedModelSerializer doesn't include the id by default |
| 185 | id = serializers.Field() |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 186 | class Meta: |
| 187 | model = Image |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 188 | fields = ('id', |
| 189 | 'image_id', |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 190 | 'name', |
| 191 | 'disk_format', |
| 192 | 'container_format') |
| 193 | |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 194 | serializerLookUp = { |
| 195 | Role: RoleSerializer, |
Tony Mack | 0e723b9 | 2013-04-27 11:08:19 -0400 | [diff] [blame] | 196 | PLUser: UserSerializer, |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 197 | Key: KeySerializer, |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 198 | Site: SiteSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 199 | SitePrivilege: SitePrivilegeSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 200 | Slice: SliceSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 201 | SliceMembership: SliceMembershipSerializer, |
| 202 | Subnet: SubnetSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 203 | Node: NodeSerializer, |
| 204 | Sliver: SliverSerializer, |
| 205 | DeploymentNetwork: DeploymentNetworkSerializer, |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 206 | Image: ImageSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 207 | None: None, |
| 208 | } |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 209 | |