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