2013-03-24 02:43:11 +04:00
# (c) 2013, AnsibleWorks, Michael DeHaan <michael@ansibleworks.com>
#
# This file is part of Ansible Commander
#
# Ansible Commander is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible Commander is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 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-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 '''
return dict (
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 , ) ) ,
tags = reverse ( lib . urls . views_OrganizationsTagsList , args = ( obj . pk , ) )
)
2013-03-21 23:28:40 +04:00
class AuditTrailSerializer ( 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 = AuditTrail
fields = ( ' url ' , ' id ' , ' modified_by ' , ' delta ' , ' detail ' , ' comment ' )
def get_related ( self , obj ) :
return dict ( )
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
fields = ( ' url ' , ' id ' , ' name ' , ' description ' , ' creation_date ' , ' local_repository ' , ' 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 ) :
# FIXME: add related resources: inventories
return dict ( )
class UserSerializer ( BaseSerializer ) :
# 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 ' )
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-22 19:35:26 +04:00
# FIXME: add related lookups?
2013-03-21 23:28:40 +04:00
return dict ( )
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 ) :
# 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 ) :
return dict ( )
2013-03-21 23:11:47 +04:00