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