Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1 | from django.forms import widgets |
| 2 | from rest_framework import serializers |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from 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', |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 13 | 'role', |
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: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 24 | model = User |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 25 | fields = ('id', |
| 26 | 'user_id', |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 27 | 'kuser_id', |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 28 | 'firstname', |
| 29 | 'lastname', |
| 30 | 'email', |
Tony Mack | 8484bdb | 2013-04-14 20:26:03 -0400 | [diff] [blame] | 31 | 'password', |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 32 | 'phone', |
| 33 | 'user_url', |
| 34 | 'is_admin', |
| 35 | 'site', |
| 36 | 'slice_memberships', |
| 37 | 'site_privileges') |
| 38 | |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 39 | class KeySerializer(serializers.HyperlinkedModelSerializer): |
| 40 | id = serializers.Field() |
| 41 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 42 | class Meta: |
| 43 | model = Key |
| 44 | fields = ('id', |
| 45 | 'name', |
| 46 | 'key', |
| 47 | 'type', |
| 48 | 'blacklisted', |
| 49 | 'user') |
| 50 | |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 51 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 52 | class SliceSerializer(serializers.HyperlinkedModelSerializer): |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 53 | # HyperlinkedModelSerializer doesn't include the id by default |
| 54 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 55 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 56 | slivers = serializers.HyperlinkedRelatedField(view_name='sliver-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 57 | class Meta: |
| 58 | model = Slice |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 59 | fields = ('id', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 60 | 'tenant_id', |
| 61 | 'enabled', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 62 | 'name', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 63 | 'url', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 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 | 1e7169e | 2013-05-07 11:09:57 -0400 | [diff] [blame] | 69 | 'subnet_id', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 70 | 'site', |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 71 | 'slivers', |
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 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 87 | class SiteSerializer(serializers.HyperlinkedModelSerializer): |
| 88 | |
| 89 | #Experimenting with whether to use ids, hyperlinks, or nested includes |
| 90 | #slices = serializers.PrimaryKeyRelatedField(many=True, read_only=True) |
| 91 | #slices = serializers.RelatedField(many=True, read_only=True) |
| 92 | #slices = SliceSerializer(many=True) |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 93 | # HyperlinkedModelSerializer doesn't include the id by default |
| 94 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 95 | slices = serializers.HyperlinkedRelatedField(many=True, read_only=True,view_name='slice-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 96 | |
| 97 | class Meta: |
| 98 | model = Site |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 99 | fields = ('id', |
| 100 | 'url', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 101 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 102 | 'slices', |
| 103 | 'site_url', |
| 104 | 'enabled', |
| 105 | 'longitude', |
| 106 | 'latitude', |
| 107 | 'login_base', |
Tony Mack | e87bffa | 2013-04-08 23:18:04 -0400 | [diff] [blame] | 108 | 'tenant_id', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 109 | 'is_public', |
| 110 | 'abbreviated_name', |
| 111 | 'updated', |
| 112 | 'created') |
| 113 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 114 | class SitePrivilegeSerializer(serializers.HyperlinkedModelSerializer): |
| 115 | id = serializers.Field() |
| 116 | site = serializers.HyperlinkedRelatedField(view_name='site-detail') |
| 117 | user = serializers.HyperlinkedRelatedField(view_name='user-detail') |
| 118 | role = serializers.HyperlinkedRelatedField(view_name='role-detail') |
| 119 | class Meta: |
| 120 | model = SitePrivilege |
| 121 | fields = ('id', |
| 122 | 'user', |
| 123 | 'site', |
| 124 | 'role') |
| 125 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 126 | class DeploymentNetworkSerializer(serializers.HyperlinkedModelSerializer): |
| 127 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 128 | # HyperlinkedModelSerializer doesn't include the id by default |
| 129 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 130 | sites = serializers.HyperlinkedRelatedField(view_name='deploymentnetwork-detail') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 131 | class Meta: |
| 132 | model = DeploymentNetwork |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 133 | fields = ('id', |
| 134 | 'name', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 135 | 'sites' |
| 136 | ) |
| 137 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 138 | class SliverSerializer(serializers.HyperlinkedModelSerializer): |
| 139 | # HyperlinkedModelSerializer doesn't include the id by default |
| 140 | id = serializers.Field() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 141 | image = serializers.HyperlinkedRelatedField(view_name='image-detail') |
| 142 | key = serializers.HyperlinkedRelatedField(view_name='key-detail') |
| 143 | slice = serializers.HyperlinkedRelatedField(view_name='slice-detail') |
| 144 | deployment_network = serializers.HyperlinkedRelatedField(view_name='deployment_network-detail') |
| 145 | node = serializers.HyperlinkedRelatedField(view_name='node-detail') |
| 146 | |
| 147 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 148 | #slice = serializers.PrimaryKeyRelatedField(read_only=True) |
| 149 | |
| 150 | class Meta: |
| 151 | model = Sliver |
| 152 | fields = ('id', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 153 | 'instance_id', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 154 | 'name', |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 155 | 'instance_name', |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 156 | 'ip', |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 157 | 'image', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 158 | 'key', |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 159 | 'slice', |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 160 | 'deploymentNetwork', |
| 161 | 'node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 162 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 163 | class NodeSerializer(serializers.HyperlinkedModelSerializer): |
| 164 | # HyperlinkedModelSerializer doesn't include the id by default |
| 165 | id = serializers.Field() |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 166 | class Meta: |
| 167 | model = Node |
| 168 | fields = ('id', |
| 169 | 'name') |
| 170 | |
Tony Mack | a2dfb37 | 2013-04-09 11:02:32 -0400 | [diff] [blame] | 171 | class ImageSerializer(serializers.HyperlinkedModelSerializer): |
| 172 | # HyperlinkedModelSerializer doesn't include the id by default |
| 173 | id = serializers.Field() |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 174 | class Meta: |
| 175 | model = Image |
Tony Mack | 879d14a | 2013-04-09 09:34:19 -0400 | [diff] [blame] | 176 | fields = ('id', |
| 177 | 'image_id', |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 178 | 'name', |
| 179 | 'disk_format', |
| 180 | 'container_format') |
| 181 | |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 182 | serializerLookUp = { |
| 183 | Role: RoleSerializer, |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 184 | User: UserSerializer, |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 185 | Key: KeySerializer, |
Tony Mack | a28f1cd | 2013-04-07 20:02:31 -0400 | [diff] [blame] | 186 | Site: SiteSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 187 | SitePrivilege: SitePrivilegeSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 188 | Slice: SliceSerializer, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 189 | SliceMembership: SliceMembershipSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 190 | Node: NodeSerializer, |
| 191 | Sliver: SliverSerializer, |
| 192 | DeploymentNetwork: DeploymentNetworkSerializer, |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 193 | Image: ImageSerializer, |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 194 | None: None, |
| 195 | } |
Tony Mack | 5331008 | 2013-04-09 09:26:12 -0400 | [diff] [blame] | 196 | |