1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 01:21:21 +03:00

AC-1040 Exposed list of choices for type field.

This commit is contained in:
Chris Church 2014-03-29 14:34:18 -04:00
parent 5ad0282495
commit fe5d154d76
4 changed files with 37 additions and 7 deletions

View File

@ -17,7 +17,7 @@ from rest_framework.exceptions import ParseError
from rest_framework.filters import BaseFilterBackend from rest_framework.filters import BaseFilterBackend
# Ansible Tower # Ansible Tower
from awx.main.utils import camelcase_to_underscore from awx.main.utils import get_type_for_model
class ActiveOnlyBackend(BaseFilterBackend): class ActiveOnlyBackend(BaseFilterBackend):
''' '''
@ -52,10 +52,10 @@ class TypeFilterBackend(BaseFilterBackend):
ct_model = ct.model_class() ct_model = ct.model_class()
if not ct_model: if not ct_model:
continue continue
ct_type = camelcase_to_underscore(ct_model._meta.object_name) ct_type = get_type_for_model(ct_model)
types_map[ct_type] = ct.pk types_map[ct_type] = ct.pk
model = queryset.model model = queryset.model
model_type = camelcase_to_underscore(model._meta.object_name) model_type = get_type_for_model(model)
if 'polymorphic_ctype' in model._meta.get_all_field_names(): if 'polymorphic_ctype' in model._meta.get_all_field_names():
types_pks = set([v for k,v in types_map.items() if k in types]) types_pks = set([v for k,v in types_map.items() if k in types])
queryset = queryset.filter(polymorphic_ctype_id__in=types_pks) queryset = queryset.filter(polymorphic_ctype_id__in=types_pks)

View File

@ -197,6 +197,14 @@ class GenericAPIView(generics.GenericAPIView, APIView):
# appropriate metadata about the fields that should be supplied. # appropriate metadata about the fields that should be supplied.
serializer = self.get_serializer() serializer = self.get_serializer()
actions['GET'] = serializer.metadata() actions['GET'] = serializer.metadata()
# Inject the type field choices into GET options as well as on
# the metadata itself.
if 'type' in actions['GET']:
actions['GET']['type']['type'] = 'multiple choice'
actions['GET']['type']['choices'] = [
(x, unicode(get_model_for_type(x)._meta.verbose_name))
for x in serializer.get_types()
]
ret['types'] = serializer.get_types() ret['types'] = serializer.get_types()
if actions: if actions:
ret['actions'] = actions ret['actions'] = actions

View File

@ -30,7 +30,7 @@ from rest_framework import serializers
# AWX # AWX
from awx.main.models import * from awx.main.models import *
from awx.main.utils import update_scm_url, camelcase_to_underscore from awx.main.utils import update_scm_url, get_type_for_model
logger = logging.getLogger('awx.api.serializers') logger = logging.getLogger('awx.api.serializers')
@ -214,8 +214,7 @@ class BaseSerializer(serializers.ModelSerializer):
return ret return ret
def get_type(self, obj): def get_type(self, obj):
opts = get_concrete_model(self.opts.model)._meta return get_type_for_model(self.opts.model)
return camelcase_to_underscore(opts.object_name)
def get_types(self): def get_types(self):
return [self.get_type(None)] return [self.get_type(None)]

View File

@ -17,7 +17,8 @@ from rest_framework.exceptions import ParseError, PermissionDenied
from Crypto.Cipher import AES from Crypto.Cipher import AES
__all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore', __all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore',
'get_ansible_version', 'get_awx_version', 'update_scm_url'] 'get_ansible_version', 'get_awx_version', 'update_scm_url',
'get_type_for_model', 'get_model_for_type']
def get_object_or_400(klass, *args, **kwargs): def get_object_or_400(klass, *args, **kwargs):
''' '''
@ -301,6 +302,28 @@ def model_to_dict(obj, serializer_mapping=None):
attr_d[field.name] = "hidden" attr_d[field.name] = "hidden"
return attr_d return attr_d
def get_type_for_model(model):
'''
Return type name for a given model class.
'''
from rest_framework.compat import get_concrete_model
opts = get_concrete_model(model)._meta
return camelcase_to_underscore(opts.object_name)
def get_model_for_type(type):
'''
Return model class for a given type name.
'''
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
for ct in ContentType.objects.filter(Q(app_label='main') | Q(app_label='auth', model='user')):
ct_model = ct.model_class()
if not ct_model:
continue
ct_type = get_type_for_model(ct_model)
if type == ct_type:
return ct_model
def get_system_task_capacity(): def get_system_task_capacity():
from django.conf import settings from django.conf import settings
if hasattr(settings, 'SYSTEM_TASK_CAPACITY'): if hasattr(settings, 'SYSTEM_TASK_CAPACITY'):