From ae1257517064362188b893a939cb336c48097256 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 18 May 2012 08:19:45 -0400 Subject: Fix the pwpolicy_find post_callback Always call convert_time_for_output so time gets reported correctly. That method has its own checks for whether the attributes are present; an additional check is unnecessary. Use a key function for sorting; cmp is deprecated, slower and more complicated. Add a test https://fedorahosted.org/freeipa/ticket/2726 --- ipalib/plugins/pwpolicy.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'ipalib/plugins/pwpolicy.py') diff --git a/ipalib/plugins/pwpolicy.py b/ipalib/plugins/pwpolicy.py index 330f9f7e5..7be590f2e 100644 --- a/ipalib/plugins/pwpolicy.py +++ b/ipalib/plugins/pwpolicy.py @@ -459,34 +459,36 @@ class pwpolicy_find(LDAPSearch): # this command does custom sorting in post_callback sort_result_entries = False - def sort_priority(self,x,y): + def priority_sort_key(self, entry): + """Key for sorting password policies + + returns a pair: (is_global, priority) + """ # global policy will be always last in the output - if x[1]['cn'][0] == global_policy_name: - return 1 - elif y[1]['cn'][0] == global_policy_name: - return -1 + if entry[1]['cn'][0] == global_policy_name: + return True, 0 else: - # policies with higher priority will be at the beginning of the list + # policies with higher priority (lower number) will be at the + # beginning of the list try: - x = cmp(int(x[1]['cospriority'][0]), int(y[1]['cospriority'][0])) + cospriority = entry[1]['cospriority'][0] except KeyError: # if cospriority is not present in the entry, rather return 0 # than crash - x = 0 - return x + cospriority = 0 + return False, cospriority def post_callback(self, ldap, entries, truncated, *args, **options): for e in entries: - # attribute rights are not allowed for pwpolicy_find + # When pkey_only flag is on, entries should contain only a cn. + # Add a cospriority attribute that will be used for sorting. + # Attribute rights are not allowed for pwpolicy_find. self.obj.add_cospriority(e[1], e[1]['cn'][0], rights=False) - if options.get('pkey_only', False): - # when pkey_only flag is on, entries should contain only a cn - # and a cospriority attribute that will be used for sorting - # When the entries are sorted, cosentry is removed - self.obj.convert_time_for_output(e[1], **options) + + self.obj.convert_time_for_output(e[1], **options) # do custom entry sorting by its cospriority - entries.sort(self.sort_priority) + entries.sort(key=self.priority_sort_key) if options.get('pkey_only', False): # remove cospriority that was used for sorting @@ -497,4 +499,3 @@ class pwpolicy_find(LDAPSearch): pass api.register(pwpolicy_find) - -- cgit