From f4c18843a33bf59631d77c1029c6dd6700aeb0f2 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 2 May 2019 18:00:18 -0400 Subject: [PATCH] Resolve default ordering warnings from tests --- awx/api/filters.py | 8 +++ awx/api/generics.py | 1 + awx/api/views/__init__.py | 1 + awx/api/views/organization.py | 2 + .../0077_v360_add_default_orderings.py | 59 +++++++++++++++++++ awx/main/models/ad_hoc_commands.py | 1 + awx/main/models/credential/__init__.py | 1 + awx/main/models/ha.py | 1 + awx/main/models/inventory.py | 2 + awx/main/models/notifications.py | 1 + awx/main/models/oauth.py | 2 + awx/main/models/rbac.py | 1 + awx/main/models/unified_jobs.py | 2 + 13 files changed, 82 insertions(+) create mode 100644 awx/main/migrations/0077_v360_add_default_orderings.py diff --git a/awx/api/filters.py b/awx/api/filters.py index 68b55110d1..b41a191627 100644 --- a/awx/api/filters.py +++ b/awx/api/filters.py @@ -402,6 +402,8 @@ class OrderByBackend(BaseFilterBackend): order_by = value.split(',') else: order_by = (value,) + if order_by is None: + order_by = self.get_default_ordering(view) if order_by: order_by = self._validate_ordering_fields(queryset.model, order_by) @@ -428,6 +430,12 @@ class OrderByBackend(BaseFilterBackend): # Return a 400 for invalid field names. raise ParseError(*e.args) + def get_default_ordering(self, view): + ordering = getattr(view, 'ordering', None) + if isinstance(ordering, str): + return (ordering,) + return ordering + def _validate_ordering_fields(self, model, order_by): for field_name in order_by: # strip off the negation prefix `-` if it exists diff --git a/awx/api/generics.py b/awx/api/generics.py index c3f7d22700..6417e59871 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -815,6 +815,7 @@ class RetrieveUpdateDestroyAPIView(RetrieveUpdateAPIView, DestroyAPIView): class ResourceAccessList(ParentMixin, ListAPIView): serializer_class = ResourceAccessListElementSerializer + ordering = ('username',) def get_queryset(self): obj = self.get_parent_object() diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index eb02aec10f..0e4685f188 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -4455,6 +4455,7 @@ class RoleUsersList(SubListAttachDetachAPIView): serializer_class = serializers.UserSerializer parent_model = models.Role relationship = 'members' + ordering = ('username',) def get_queryset(self): role = self.get_parent_object() diff --git a/awx/api/views/organization.py b/awx/api/views/organization.py index 93d03b55a3..37cce7d289 100644 --- a/awx/api/views/organization.py +++ b/awx/api/views/organization.py @@ -116,6 +116,7 @@ class OrganizationUsersList(BaseUsersList): serializer_class = UserSerializer parent_model = Organization relationship = 'member_role.members' + ordering = ('username',) class OrganizationAdminsList(BaseUsersList): @@ -124,6 +125,7 @@ class OrganizationAdminsList(BaseUsersList): serializer_class = UserSerializer parent_model = Organization relationship = 'admin_role.members' + ordering = ('username',) class OrganizationProjectsList(SubListCreateAttachDetachAPIView): diff --git a/awx/main/migrations/0077_v360_add_default_orderings.py b/awx/main/migrations/0077_v360_add_default_orderings.py new file mode 100644 index 0000000000..b7be0f19fc --- /dev/null +++ b/awx/main/migrations/0077_v360_add_default_orderings.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-05-03 14:30 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0076_v360_add_new_instance_group_relations'), + ] + + operations = [ + migrations.AlterModelOptions( + name='adhoccommand', + options={'ordering': ('id',)}, + ), + migrations.AlterModelOptions( + name='credentialinputsource', + options={'ordering': ('target_credential', 'source_credential', 'input_field_name')}, + ), + migrations.AlterModelOptions( + name='instance', + options={'ordering': ('hostname',)}, + ), + migrations.AlterModelOptions( + name='inventorysource', + options={'ordering': ('inventory', 'name')}, + ), + migrations.AlterModelOptions( + name='inventoryupdate', + options={'ordering': ('inventory', 'name')}, + ), + migrations.AlterModelOptions( + name='notificationtemplate', + options={'ordering': ('name',)}, + ), + migrations.AlterModelOptions( + name='oauth2accesstoken', + options={'ordering': ('id',), 'verbose_name': 'access token'}, + ), + migrations.AlterModelOptions( + name='oauth2application', + options={'ordering': ('organization', 'name'), 'verbose_name': 'application'}, + ), + migrations.AlterModelOptions( + name='role', + options={'ordering': ('content_type', 'object_id'), 'verbose_name_plural': 'roles'}, + ), + migrations.AlterModelOptions( + name='unifiedjob', + options={'ordering': ('id',)}, + ), + migrations.AlterModelOptions( + name='unifiedjobtemplate', + options={'ordering': ('name',)}, + ), + ] diff --git a/awx/main/models/ad_hoc_commands.py b/awx/main/models/ad_hoc_commands.py index d6e173d181..5671bb3383 100644 --- a/awx/main/models/ad_hoc_commands.py +++ b/awx/main/models/ad_hoc_commands.py @@ -30,6 +30,7 @@ class AdHocCommand(UnifiedJob, JobNotificationMixin): class Meta(object): app_label = 'main' + ordering = ('id',) diff_mode = models.BooleanField( default=False, diff --git a/awx/main/models/credential/__init__.py b/awx/main/models/credential/__init__.py index 753a04788d..227766a855 100644 --- a/awx/main/models/credential/__init__.py +++ b/awx/main/models/credential/__init__.py @@ -1312,6 +1312,7 @@ class CredentialInputSource(PrimordialModel): class Meta: app_label = 'main' unique_together = (('target_credential', 'input_field_name'),) + ordering = ('target_credential', 'source_credential', 'input_field_name',) target_credential = models.ForeignKey( 'Credential', diff --git a/awx/main/models/ha.py b/awx/main/models/ha.py index e5437f42b2..c238fac89b 100644 --- a/awx/main/models/ha.py +++ b/awx/main/models/ha.py @@ -98,6 +98,7 @@ class Instance(HasPolicyEditsMixin, BaseModel): class Meta: app_label = 'main' + ordering = ("hostname",) POLICY_FIELDS = frozenset(('managed_by_policy', 'hostname', 'capacity_adjustment')) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 8775434de4..e7244bac5b 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -1451,6 +1451,7 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions, CustomVirtualE class Meta: app_label = 'main' + ordering = ('inventory', 'name') inventory = models.ForeignKey( 'Inventory', @@ -1680,6 +1681,7 @@ class InventoryUpdate(UnifiedJob, InventorySourceOptions, JobNotificationMixin, class Meta: app_label = 'main' + ordering = ('inventory', 'name') inventory = models.ForeignKey( 'Inventory', diff --git a/awx/main/models/notifications.py b/awx/main/models/notifications.py index ee75387f2f..9fa6e31b17 100644 --- a/awx/main/models/notifications.py +++ b/awx/main/models/notifications.py @@ -50,6 +50,7 @@ class NotificationTemplate(CommonModelNameNotUnique): class Meta: app_label = 'main' unique_together = ('organization', 'name') + ordering = ("name",) organization = models.ForeignKey( 'Organization', diff --git a/awx/main/models/oauth.py b/awx/main/models/oauth.py index 63a70ac016..4420c30c3f 100644 --- a/awx/main/models/oauth.py +++ b/awx/main/models/oauth.py @@ -28,6 +28,7 @@ class OAuth2Application(AbstractApplication): app_label = 'main' verbose_name = _('application') unique_together = (("name", "organization"),) + ordering = ('organization', 'name') CLIENT_CONFIDENTIAL = "confidential" CLIENT_PUBLIC = "public" @@ -89,6 +90,7 @@ class OAuth2AccessToken(AbstractAccessToken): class Meta: app_label = 'main' verbose_name = _('access token') + ordering = ('id',) user = models.ForeignKey( settings.AUTH_USER_MODEL, diff --git a/awx/main/models/rbac.py b/awx/main/models/rbac.py index e4c5053616..a0b5b6785f 100644 --- a/awx/main/models/rbac.py +++ b/awx/main/models/rbac.py @@ -138,6 +138,7 @@ class Role(models.Model): index_together = [ ("content_type", "object_id") ] + ordering = ("content_type", "object_id") role_field = models.TextField(null=False) singleton_name = models.TextField(null=True, default=None, db_index=True, unique=True) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index ed63cb6dde..40ebf155cb 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -98,6 +98,7 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique, Notificatio class Meta: app_label = 'main' + ordering = ('name',) # unique_together here is intentionally commented out. Please make sure sub-classes of this model # contain at least this uniqueness restriction: SOFT_UNIQUE_TOGETHER = [('polymorphic_ctype', 'name')] #unique_together = [('polymorphic_ctype', 'name')] @@ -556,6 +557,7 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique class Meta: app_label = 'main' + ordering = ('id',) old_pk = models.PositiveIntegerField( null=True,