Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New copy fields, clean up user_capabilities logic #1330

Merged
merged 2 commits into from
Mar 13, 2018

Conversation

AlanCoding
Copy link
Member

@AlanCoding AlanCoding commented Feb 22, 2018

Connect #1167

I still need to look at tests, and I may not be completely finished with this yet, but it seems to be functional as I go through the table of resources that need the new copy capability.

Some of this is me trying to pay down technical debt that I previously incurred. I really wanted to implement this efficiently, but I was not very good at it before. I put capabilities_prefetch on the view, whereas the prefetch list was on the serializer. This was very hard to follow. Possibly even worse, I forced the paginated queryset to execute prematurely by adding a gory special condition in generics.py.

Some sacrifices to the gods of polymorphism are still needed, but this is now completely serializer-based, and I am using a trick I learned with the serializer context. This hat trick was first employed here:

awx/awx/api/serializers.py

Lines 4172 to 4180 in c35c01e

def get_capacity_dict(self):
# Store capacity values (globally computed) in the context
if 'capacity_map' not in self.context:
ig_qs = None
if self.parent: # Is ListView:
ig_qs = self.parent.instance
self.context['capacity_map'] = InstanceGroup.objects.capacity_values(
qs=ig_qs, tasks=self.get_jobs_qs(), breakdown=True)
return self.context['capacity_map']

The goal here is kind of a "prefetch over a page with this custom logic" type thing.

Note: ultimately I will see about migrating the organization counts to this mechanism as well. It's the same general kind of situation. All 3 involve the same general category of "hack", and leveraging the context is the most elegant form I have for it.

@AlanCoding AlanCoding force-pushed the capable_of_anything branch 2 times, most recently from dbe6a6c to 71b8c38 Compare February 26, 2018 16:53
@AlanCoding AlanCoding changed the title [WIP] New copy fields, clean up user_capabilities logic New copy fields, clean up user_capabilities logic Feb 26, 2018
@AlanCoding
Copy link
Member Author

Sorry that WIP label took a while, I was experiencing some of the common headaches with mocking things inside the access code (I think there's some bad behavior somewhere that is messing up the logical behavior). This one tricky part really needed coverage, and I think I have something which is at least passable at this point.

The database queries look pretty good.

We need to optimize the unified job list. I leave this as an exercise for later.

@@ -1146,7 +1197,11 @@ def validate_variables(self, value):


class InventorySerializer(BaseSerializerWithVariables):
show_capabilities = ['edit', 'delete', 'adhoc']
show_capabilities = ['edit', 'delete', 'adhoc', 'copy']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakemcdermott here you can see where the lists grew by one new field, the "copy" field. Hopefully this should cover everything in the spec (but alas, still a PR).

The rest of this is really brutal stuff with using @wwitzel3's new roles to prefetch the fields for list views.

if cls != UnifiedJobTemplate:
return []
return ['project', 'inventorysource', 'systemjobtemplate']

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I genuinely feel bad that I ever introduced this method in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atonement accepted, Your sins are forgiven

Add copy fields corresponding to new server-side copying

Refactor the way user_capabilities are delivered
 - move the prefetch definition from views to serializer
 - store temporary mapping in serializer context
 - use serializer backlinks to denote polymorphic prefetch model exclusions
@@ -355,13 +355,6 @@ class ListAPIView(generics.ListAPIView, GenericAPIView):
def get_queryset(self):
return self.request.user.get_queryset(self.model)

def paginate_queryset(self, queryset):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, dead code

if cls != UnifiedJobTemplate:
return []
return ['project', 'inventorysource', 'systemjobtemplate']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atonement accepted, Your sins are forgiven

Copy link
Contributor

@wwitzel3 wwitzel3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@AlanCoding AlanCoding merged commit dcae4f6 into ansible:devel Mar 13, 2018
@AlanCoding AlanCoding deleted the capable_of_anything branch January 15, 2019 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants