Skip to content

Commit

Permalink
entry: move entries within a group (#347)
Browse files Browse the repository at this point in the history
* entry: move entries within a group

* chore: rename move to reindex

* tests: reindex entry

* docs: add docstrings to reindex and index
  • Loading branch information
pwall2222 authored Nov 14, 2023
1 parent 8fc9708 commit 1085c59
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pykeepass/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def _get_string_field_keys(self, exclude_reserved=False):
else:
return results

@property
def index(self):
"""int: get index of a entry within a group"""
group = self.group._element
children = group.getchildren()
first_index = self.group._first_entry
index = children.index(self._element)
return index - first_index

def reindex(self, new_index):
"""Move entry to a new index within a group
Args:
new_index (int): new index for the entry starting at 0
"""
group = self.group._element
first_index = self.group._first_entry
group.remove(self._element)
group.insert(new_index+first_index, self._element)

@property
def attachments(self):
return self._kp.find_attachments(
Expand Down
6 changes: 6 additions & 0 deletions pykeepass/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def __init__(self, name=None, element=None, icon=None, notes=None,
'element, but a {}'.format(element.tag)
self._element = element

@property
def _first_entry(self):
children = self._element.getchildren()
first_element = next(e for e in children if e.tag == "Entry")
return children.index(first_element)

@property
def name(self):
"""str: get or set group name"""
Expand Down
11 changes: 11 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,17 @@ def test_is_custom_property_protected(self):
self.assertFalse(e.is_custom_property_protected('not-protected'))
self.assertFalse(e.is_custom_property_protected('non-existent'))

def test_reindex(self):
e1 = self.kp.add_entry(self.kp.root_group, 'Test-Index1', 'user-index', 'pass')
e2 = self.kp.add_entry(self.kp.root_group, 'Test-Index2', 'user-index', 'pass')
e3 = self.kp.add_entry(self.kp.root_group, 'Test-Index3', 'user-index', 'pass')
e4 = self.kp.add_entry(self.kp.root_group, 'Test-Index4', 'user-index', 'pass')
e2.reindex(0)
e3.reindex(0)
e4.reindex(0)
entries = self.kp.find_entries(username="user-index")
self.assertEqual(entries, [e4,e3,e2,e1])


class EntryHistoryTests3(KDBX3Tests):

Expand Down

0 comments on commit 1085c59

Please sign in to comment.