def get_queryset(self): self.category_slug = self.kwargs.get('category_slug', 'all') all_category_slugs = settings_registry.get_registered_categories( features_enabled=get_licensed_features()).keys() for slug_to_delete in VERSION_SPECIFIC_CATEGORIES_TO_EXCLUDE[ get_request_version(self.request)]: all_category_slugs.remove(slug_to_delete) if self.request.user.is_superuser or getattr( self.request.user, 'is_system_auditor', False): category_slugs = all_category_slugs else: category_slugs = {'user'} if self.category_slug not in all_category_slugs: raise Http404 if self.category_slug not in category_slugs: raise PermissionDenied() registered_settings = settings_registry.get_registered_settings( category_slug=self.category_slug, read_only=False, features_enabled=get_licensed_features(), slugs_to_ignore=VERSION_SPECIFIC_CATEGORIES_TO_EXCLUDE[ get_request_version(self.request)]) if self.category_slug == 'user': return Setting.objects.filter(key__in=registered_settings, user=self.request.user) else: return Setting.objects.filter(key__in=registered_settings, user__isnull=True)
def post(self, request, *args, **kwargs): if get_request_version(request) < 2: return self.v1_not_allowed() obj = self.get_object() create_kwargs = self._build_create_dict(obj) create_kwargs_check = {} for key in create_kwargs: create_kwargs_check[key] = getattr(create_kwargs[key], 'pk', None) or create_kwargs[key] if not request.user.can_access(self.model, 'add', create_kwargs_check): raise PermissionDenied() if not request.user.can_access(self.model, 'copy_related', obj): raise PermissionDenied() serializer = self.get_serializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) new_obj, sub_objs = CopyAPIView.copy_model_obj( None, None, self.model, obj, request.user, create_kwargs=create_kwargs, copy_name=serializer.validated_data.get('name', '') ) if hasattr(new_obj, 'admin_role') and request.user not in new_obj.admin_role.members.all(): new_obj.admin_role.members.add(request.user) if sub_objs: permission_check_func = None if hasattr(type(self), 'deep_copy_permission_check_func'): permission_check_func = ( type(self).__module__, type(self).__name__, 'deep_copy_permission_check_func' ) trigger_delayed_deep_copy( self.model.__module__, self.model.__name__, obj.pk, new_obj.pk, request.user.pk, sub_objs, permission_check_func=permission_check_func ) serializer = self._get_copy_return_serializer(new_obj) headers = {'Location': new_obj.get_absolute_url(request=request)} return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def get(self, request, *args, **kwargs): if get_request_version(request) < 2: return self.v1_not_allowed() obj = self.get_object() if not request.user.can_access(obj.__class__, 'read', obj): raise PermissionDenied() create_kwargs = self._build_create_dict(obj) for key in create_kwargs: create_kwargs[key] = getattr(create_kwargs[key], 'pk', None) or create_kwargs[key] can_copy = request.user.can_access(self.model, 'add', create_kwargs) and \ request.user.can_access(self.model, 'copy_related', obj) return Response({'can_copy': can_copy})
def get(self, request, format=None): ''' List top level resources ''' data = OrderedDict() data['ping'] = reverse('api:api_v1_ping_view', request=request) data['instances'] = reverse('api:instance_list', request=request) data['instance_groups'] = reverse('api:instance_group_list', request=request) data['config'] = reverse('api:api_v1_config_view', request=request) data['settings'] = reverse('api:setting_category_list', request=request) data['me'] = reverse('api:user_me_list', request=request) data['dashboard'] = reverse('api:dashboard_view', request=request) data['organizations'] = reverse('api:organization_list', request=request) data['users'] = reverse('api:user_list', request=request) data['projects'] = reverse('api:project_list', request=request) data['project_updates'] = reverse('api:project_update_list', request=request) data['teams'] = reverse('api:team_list', request=request) data['credentials'] = reverse('api:credential_list', request=request) if get_request_version(request) > 1: data['credential_types'] = reverse('api:credential_type_list', request=request) data['credential_input_sources'] = reverse('api:credential_input_source_list', request=request) data['applications'] = reverse('api:o_auth2_application_list', request=request) data['tokens'] = reverse('api:o_auth2_token_list', request=request) data['metrics'] = reverse('api:metrics_view', request=request) data['inventory'] = reverse('api:inventory_list', request=request) data['inventory_scripts'] = reverse('api:inventory_script_list', request=request) data['inventory_sources'] = reverse('api:inventory_source_list', request=request) data['inventory_updates'] = reverse('api:inventory_update_list', request=request) data['groups'] = reverse('api:group_list', request=request) data['hosts'] = reverse('api:host_list', request=request) data['job_templates'] = reverse('api:job_template_list', request=request) data['jobs'] = reverse('api:job_list', request=request) data['job_events'] = reverse('api:job_event_list', request=request) data['ad_hoc_commands'] = reverse('api:ad_hoc_command_list', request=request) data['system_job_templates'] = reverse('api:system_job_template_list', request=request) data['system_jobs'] = reverse('api:system_job_list', request=request) data['schedules'] = reverse('api:schedule_list', request=request) data['roles'] = reverse('api:role_list', request=request) data['notification_templates'] = reverse('api:notification_template_list', request=request) data['notifications'] = reverse('api:notification_list', request=request) data['labels'] = reverse('api:label_list', request=request) data['unified_job_templates'] = reverse('api:unified_job_template_list', request=request) data['unified_jobs'] = reverse('api:unified_job_list', request=request) data['activity_stream'] = reverse('api:activity_stream_list', request=request) data['workflow_job_templates'] = reverse('api:workflow_job_template_list', request=request) data['workflow_jobs'] = reverse('api:workflow_job_list', request=request) data['workflow_job_template_nodes'] = reverse('api:workflow_job_template_node_list', request=request) data['workflow_job_nodes'] = reverse('api:workflow_job_node_list', request=request) return Response(data)
def get_queryset(self): setting_categories = [] categories = settings_registry.get_registered_categories() if self.request.user.is_superuser or self.request.user.is_system_auditor: pass # categories = categories elif 'user' in categories: categories = {'user', _('User')} else: categories = {} for category_slug in sorted(categories.keys()): if category_slug in VERSION_SPECIFIC_CATEGORIES_TO_EXCLUDE[ get_request_version(self.request)]: continue url = reverse('api:setting_singleton_detail', kwargs={'category_slug': category_slug}, request=self.request) setting_categories.append( SettingCategory(url, category_slug, categories[category_slug])) return setting_categories
def get_description(self, request, html=False): self.request = request template_list = [] for klass in inspect.getmro(type(self)): template_basename = camelcase_to_underscore(klass.__name__) template_list.append('api/%s.md' % template_basename) context = self.get_description_context() # "v2" -> 2 default_version = int(settings.REST_FRAMEWORK['DEFAULT_VERSION'].lstrip('v')) request_version = get_request_version(self.request) if request_version is not None and request_version < default_version: context['deprecated'] = True description = render_to_string(template_list, context) if context.get('deprecated'): # render deprecation messages at the very top description = '\n'.join([render_to_string('api/_deprecated.md', context), description]) return description
def get_object(self): settings_qs = self.get_queryset() registered_settings = settings_registry.get_registered_settings( category_slug=self.category_slug, slugs_to_ignore=VERSION_SPECIFIC_CATEGORIES_TO_EXCLUDE[ get_request_version(self.request)]) all_settings = {} for setting in settings_qs: all_settings[setting.key] = setting.value for key in registered_settings: if key in all_settings or self.category_slug == 'changed': continue try: field = settings_registry.get_setting_field( key, for_user=bool(self.category_slug == 'user')) all_settings[key] = field.get_default() except serializers.SkipField: all_settings[key] = None all_settings[ 'user'] = self.request.user if self.category_slug == 'user' else None obj = type('Settings', (object, ), all_settings)() self.check_object_permissions(self.request, obj) return obj
def filter_queryset(self, request, queryset, view): # TODO: remove in 3.3 from awx.api.versioning import get_request_version if get_request_version(request) == 1: queryset = queryset.filter(credential_type__managed_by_tower=True) return queryset