2013-04-02 00:04:27 +04:00
# Copyright (c) 2013 AnsibleWorks, Inc.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# This file is part of Ansible Commander.
#
2013-03-24 02:43:11 +04:00
# Ansible Commander is free software: you can redistribute it and/or modify
2013-04-09 09:05:55 +04:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
2013-03-24 02:43:11 +04:00
#
2013-04-09 09:05:55 +04:00
# Ansible Commander is distributed in the hope that it will be useful,
2013-03-24 02:43:11 +04:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2013-04-09 09:05:55 +04:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible Commander. If not, see <http://www.gnu.org/licenses/>.
2013-03-24 02:43:11 +04:00
2013-03-22 19:35:26 +04:00
from django . contrib . auth . models import User
2013-03-21 23:28:40 +04:00
from lib . main . models import *
2013-03-20 06:26:35 +04:00
from rest_framework import serializers , pagination
2013-03-21 23:11:47 +04:00
from django . core . urlresolvers import reverse
2013-03-24 22:14:59 +04:00
from django . core . serializers import json
2013-03-21 23:11:47 +04:00
import lib . urls
2013-03-21 19:06:47 +04:00
2013-03-21 23:28:40 +04:00
class BaseSerializer ( serializers . ModelSerializer ) :
pass
class OrganizationSerializer ( BaseSerializer ) :
2013-03-20 06:26:35 +04:00
2013-03-21 23:11:47 +04:00
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
# make certain fields read only
2013-03-22 20:01:31 +04:00
creation_date = serializers . DateTimeField ( read_only = True ) # FIXME: is model Date or DateTime, fix model
2013-03-21 23:11:47 +04:00
active = serializers . BooleanField ( read_only = True )
2013-03-21 08:12:03 +04:00
2013-03-20 06:26:35 +04:00
class Meta :
model = Organization
2013-03-21 23:28:40 +04:00
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' related ' ) # whitelist
2013-03-21 23:11:47 +04:00
def get_related ( self , obj ) :
''' related resource URLs '''
2013-04-24 02:51:47 +04:00
res = dict (
2013-03-21 23:11:47 +04:00
audit_trail = reverse ( lib . urls . views_OrganizationsAuditTrailList , args = ( obj . pk , ) ) ,
projects = reverse ( lib . urls . views_OrganizationsProjectsList , args = ( obj . pk , ) ) ,
users = reverse ( lib . urls . views_OrganizationsUsersList , args = ( obj . pk , ) ) ,
admins = reverse ( lib . urls . views_OrganizationsAdminsList , args = ( obj . pk , ) ) ,
2013-04-23 18:25:12 +04:00
tags = reverse ( lib . urls . views_OrganizationsTagsList , args = ( obj . pk , ) ) ,
2013-04-24 02:51:47 +04:00
teams = reverse ( lib . urls . views_OrganizationsTeamsList , args = ( obj . pk , ) ) ,
2013-04-08 06:17:33 +04:00
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-21 23:11:47 +04:00
2013-03-21 23:28:40 +04:00
class AuditTrailSerializer ( BaseSerializer ) :
2013-04-08 06:17:33 +04:00
2013-03-21 23:28:40 +04:00
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
2013-04-08 06:17:33 +04:00
2013-03-21 23:28:40 +04:00
class Meta :
model = AuditTrail
fields = ( ' url ' , ' id ' , ' modified_by ' , ' delta ' , ' detail ' , ' comment ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict ( )
2013-04-24 02:54:27 +04:00
if obj . modified_by :
res [ ' modified_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . modified_by . pk , ) )
2013-04-24 02:51:47 +04:00
return res
2013-03-21 23:28:40 +04:00
class ProjectSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = Project
2013-04-19 23:40:08 +04:00
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' local_path ' ) #, 'default_playbook', 'scm_type')
2013-03-21 23:11:47 +04:00
2013-03-21 23:28:40 +04:00
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict (
organizations = reverse ( lib . urls . views_ProjectsOrganizationsList , args = ( obj . pk , ) ) ,
2013-04-23 19:13:33 +04:00
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-21 23:28:40 +04:00
2013-03-26 00:41:21 +04:00
class InventorySerializer ( BaseSerializer ) :
2013-04-08 06:17:33 +04:00
2013-03-26 00:41:21 +04:00
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = Inventory
2013-03-26 03:00:07 +04:00
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' organization ' )
2013-03-26 00:41:21 +04:00
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict (
hosts = reverse ( lib . urls . views_HostsList , args = ( obj . pk , ) ) ,
groups = reverse ( lib . urls . views_GroupsList , args = ( obj . pk , ) ) ,
2013-04-24 03:01:05 +04:00
inventory = reverse ( lib . urls . views_InventoryDetail , args = ( obj . inventory . pk , ) ) ,
2013-04-24 02:08:38 +04:00
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-26 22:44:12 +04:00
class HostSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = Host
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' inventory ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict (
2013-04-24 02:08:38 +04:00
variable_data = reverse ( lib . urls . views_HostsVariableDetail , args = ( obj . pk , ) ) ,
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-26 00:41:21 +04:00
2013-03-26 22:44:12 +04:00
class GroupSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
2013-03-28 02:17:21 +04:00
model = Group
2013-03-26 22:44:12 +04:00
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' inventory ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict (
2013-04-24 02:08:38 +04:00
variable_data = reverse ( lib . urls . views_GroupsVariableDetail , args = ( obj . pk , ) ) ,
hosts = reverse ( lib . urls . views_GroupsHostsList , args = ( obj . pk , ) ) ,
children = reverse ( lib . urls . views_GroupsChildrenList , args = ( obj . pk , ) ) ,
all_hosts = reverse ( lib . urls . views_GroupsAllHostsList , args = ( obj . pk , ) ) ,
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-26 00:41:21 +04:00
2013-03-24 23:00:01 +04:00
class TeamSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = Team
2013-04-02 01:44:06 +04:00
fields = ( ' url ' , ' id ' , ' related ' , ' name ' , ' description ' , ' organization ' , ' creation_date ' )
2013-03-24 23:00:01 +04:00
2013-04-24 02:08:38 +04:00
# FIXME: TODO: include related collections but also related FK urls
2013-03-24 23:00:01 +04:00
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict (
2013-04-24 02:08:38 +04:00
projects = reverse ( lib . urls . views_TeamsProjectsList , args = ( obj . pk , ) ) ,
users = reverse ( lib . urls . views_TeamsUsersList , args = ( obj . pk , ) ) ,
credentials = reverse ( lib . urls . views_TeamsCredentialsList , args = ( obj . pk , ) ) ,
)
2013-04-24 02:51:47 +04:00
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-24 23:00:01 +04:00
2013-04-02 04:38:03 +04:00
class CredentialSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
# FIXME: may want to make soem of these filtered based on user accessing
class Meta :
model = Credential
fields = (
2013-04-08 06:17:33 +04:00
' url ' , ' id ' , ' related ' , ' name ' , ' description ' , ' creation_date ' ,
2013-04-02 22:59:58 +04:00
' default_username ' , ' ssh_key_data ' , ' ssh_key_unlock ' , ' ssh_password ' , ' sudo_password ' ,
2013-04-02 04:38:03 +04:00
' user ' , ' team '
)
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
# FIXME: no related collections, do want to add user and team if defined
res = dict (
)
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-24 23:00:01 +04:00
2013-04-08 06:17:33 +04:00
def validate ( self , attrs ) :
''' some fields cannot be changed once written '''
if self . object is not None :
# this is an update
if self . object . user != attrs [ ' user ' ] :
raise serializers . ValidationError ( " user cannot be changed " )
if self . object . team != attrs [ ' team ' ] :
raise serializers . ValidationError ( " team cannot be changed " )
return attrs
2013-03-21 23:28:40 +04:00
class UserSerializer ( BaseSerializer ) :
2013-04-08 06:17:33 +04:00
2013-03-21 23:28:40 +04:00
# add the URL and related resources
2013-03-24 22:14:59 +04:00
url = serializers . SerializerMethodField ( ' get_absolute_url_override ' )
2013-03-21 23:28:40 +04:00
related = serializers . SerializerMethodField ( ' get_related ' )
2013-04-08 06:17:33 +04:00
2013-03-21 23:28:40 +04:00
class Meta :
model = User
2013-03-24 22:14:59 +04:00
fields = ( ' url ' , ' id ' , ' username ' , ' first_name ' , ' last_name ' , ' email ' , ' is_active ' , ' is_superuser ' , ' related ' )
2013-03-21 23:28:40 +04:00
def get_related ( self , obj ) :
2013-03-24 23:00:01 +04:00
return dict (
teams = reverse ( lib . urls . views_UsersTeamsList , args = ( obj . pk , ) ) ,
organizations = reverse ( lib . urls . views_UsersOrganizationsList , args = ( obj . pk , ) ) ,
admin_of_organizations = reverse ( lib . urls . views_UsersAdminOrganizationsList , args = ( obj . pk , ) ) ,
)
2013-03-24 22:14:59 +04:00
def get_absolute_url_override ( self , obj ) :
import lib . urls
return reverse ( lib . urls . views_UsersDetail , args = ( obj . pk , ) )
2013-03-21 23:28:40 +04:00
class TagSerializer ( BaseSerializer ) :
2013-04-08 06:17:33 +04:00
2013-03-21 23:28:40 +04:00
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = Tag
fields = ( ' url ' , ' id ' , ' name ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
res = dict ( )
return res
2013-03-21 23:11:47 +04:00
2013-03-27 06:24:03 +04:00
class VariableDataSerializer ( BaseSerializer ) :
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = VariableData
fields = ( ' url ' , ' id ' , ' data ' , ' related ' , ' name ' , ' description ' , ' creation_date ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
# FIXME: add host or group if defined
res = dict (
)
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-03-27 06:24:03 +04:00
2013-04-19 04:52:54 +04:00
class JobTemplateSerializer ( BaseSerializer ) :
2013-03-21 23:11:47 +04:00
2013-04-19 04:52:54 +04:00
# add the URL and related resources
url = serializers . CharField ( source = ' get_absolute_url ' , read_only = True )
related = serializers . SerializerMethodField ( ' get_related ' )
class Meta :
model = JobTemplate
fields = ( ' url ' , ' id ' , ' related ' , ' name ' , ' description ' , ' job_type ' , ' credential ' , ' project ' , ' inventory ' , ' created_by ' , ' creation_date ' )
def get_related ( self , obj ) :
2013-04-24 02:51:47 +04:00
# FIXME: fill in once further defined. related resources, credential, project, inventory, etc
res = dict (
)
if obj . created_by :
res [ ' created_by ' ] = reverse ( lib . urls . views_UsersDetail , args = ( obj . created_by . pk , ) )
return res
2013-04-19 04:52:54 +04:00
2013-03-21 23:11:47 +04:00