Skip to content

Commit

Permalink
more testing, a bugfix and a little bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tobixen committed Sep 17, 2023
1 parent 190fa54 commit 8de2012
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
26 changes: 8 additions & 18 deletions plann/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import tempfile
import subprocess
from plann.panic_planning import timeline_suggestion
from plann.lib import _now, _ensure_ts, parse_dt, parse_add_dur, parse_timespec, find_calendars, _summary, _procrastinate, tz, _relships_by_type, _get_summary, _relationship_text, _adjust_relations, parentlike, childlike
from plann.lib import _now, _ensure_ts, parse_dt, parse_add_dur, parse_timespec, find_calendars, _summary, _procrastinate, tz, _relships_by_type, _get_summary, _relationship_text, _adjust_relations, parentlike, childlike, _remove_reverse_relations

list_type = list

Expand Down Expand Up @@ -619,7 +619,7 @@ def _set_relations_from_text_list(calendar, some_list, parent=None, indent=0):
Caveats:
* Currently it does not support RFC 9253 and enforces RELTYPE to be PARENT or CHILD
* Currently it also lacks Support for multiple parents
* Currently it also lacks support for multiple parents
* Relation type SIBLING is ignored
"""
## Logic:
Expand Down Expand Up @@ -649,8 +649,7 @@ def get_obj(line):
return calendar.object_by_uid(uid)

i=0
if parent:
children = []
children = []
while i<len(some_list):
line = some_list[i]
line_indent = count_indent(line)
Expand Down Expand Up @@ -681,26 +680,17 @@ def get_obj(line):
i=j
continue

## Unindented line without a parent.
if line_indent == indent and not parent:
## Noop as for now but ... TODO ... shouldn't be
## TODO: if no indented list follows, then remove all children.
## TODO: if the object has a parent, remove it
i+=1
continue

## Unindented line with a parent. Should be a direct child under parent
if line_indent == indent and parent:
## Unindented line. Should be a direct child under parent
if line_indent == indent:
children.append(get_obj(some_list[i]))
i+=1
continue

## TODO: look through all the conditions above. should we ever be here?
import pdb; pdb.set_trace()
if parent:
for c in children:
c.load()
_adjust_relations(parent, children)
for c in children:
c.load()
_adjust_relations(parent, children)

def _edit(ctx, add_category=None, cancel=None, interactive_ical=False, interactive_relations=False, interactive=False, complete=None, complete_recurrence_mode='safe', postpone=None, **kwargs):
"""
Expand Down
8 changes: 8 additions & 0 deletions plann/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ def _adjust_relations(parent, children):
* All relations should be bidirectional
* siblings are not supported
"""
if not parent:
for child in children:
old_parents = child.get_relatives('PARENT', fetch_objects=False)
if len(old_parents['PARENT']) == 1:
_remove_reverse_relations(child, old_parents)
_adjust_ical_relations(child, {'PARENT': set()})
child.save()
return
pmutated = _adjust_ical_relations(parent, {'CHILD': {str(x.icalendar_component['UID']) for x in children}})
for child in children:
cmutated = _adjust_ical_relations(child, {'PARENT': {str(parent.icalendar_component['UID'])}})
Expand Down
29 changes: 24 additions & 5 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## TODO: work in progress

from xandikos.web import XandikosBackend, XandikosApp
import plann.lib
from plann.lib import find_calendars, _adjust_relations, _adjust_ical_relations
from plann.cli import _add_todo, _select, _list, _check_for_panic, _interactive_relation_edit
from plann.panic_planning import timeline_suggestion
Expand Down Expand Up @@ -156,6 +157,8 @@ def test_plann():
def dag(obj, reltype, observed=None):
if not hasattr(obj, 'get_relatives'):
obj = ctx.obj['calendars'][0].object_by_uid(obj)
else:
obj.load()
if not observed:
observed = set()
ret = {}
Expand All @@ -167,9 +170,9 @@ def dag(obj, reltype, observed=None):
return ret

## We create two tasks todo1 and todo2, todo2 being a child of todo1
todo1 = _add_todo(ctx, summary=['make plann good'], set_due='2012-12-20 23:15:00', set_dtstart='2012-12-20 22:15:00')
todo1 = _add_todo(ctx, summary=['make plann good'], set_due='2012-12-20 23:15:00', set_dtstart='2012-12-20 22:15:00', set_uid='todo1')
uid1 = str(todo1.icalendar_component['uid'])
todo2 = _add_todo(ctx, summary=['fix some bugs in plann'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00')
todo2 = _add_todo(ctx, summary=['fix some bugs in plann'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00', set_uid='todo2')
uid2 = str(todo2.icalendar_component['uid'])

## Selecting the tasks should yield ... 2 (but only one if skip_children or skip_parents is used)
Expand Down Expand Up @@ -284,9 +287,9 @@ def dag(obj, reltype, observed=None):
assert cal_post_interactive_relation_edit == cal_pre_interactive_relation_edit

## Let's add some more tasks
todo3 = _add_todo(ctx, summary=['fix some more features in plann'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00')
todo4 = _add_todo(ctx, summary=['make plann even better'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00')
todo5 = _add_todo(ctx, summary=['use plann on a daily basis to find more bugs and missing features'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00')
todo3 = _add_todo(ctx, summary=['fix some more features in plann'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00', set_uid='todo3')
todo4 = _add_todo(ctx, summary=['make plann even better'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00', set_uid='todo4')
todo5 = _add_todo(ctx, summary=['use plann on a daily basis to find more bugs and missing features'], set_parent=[uid1], set_due='2012-12-21 23:15:00', set_dtstart='2012-12-21 22:15:00', set_uid='todo5')

uid3 = str(todo3.icalendar_component['uid'])
uid4 = str(todo4.icalendar_component['uid'])
Expand Down Expand Up @@ -314,6 +317,22 @@ def add_indent(text):
## This should not throw one into the debugger
list_td = _list(ctx, top_down=True)

def remove_parent(input):
return f"{uid2}: todo2\n{uid3}: todo3\n {uid4}: todo4\n {uid5}: todo5"

with patch('plann.cli._editor', new=remove_parent) as _editor:
_interactive_relation_edit([todo1])

assert(dag(todo1, 'CHILD') == {})
assert(dag(todo1, 'PARENT') == {})
assert(dag(todo2, 'PARENT') == {})
assert(dag(todo2, 'CHILD') == {})
assert(dag(todo3, 'CHILD') == {uid4: {uid5: {}}})
assert(dag(todo5, 'PARENT') == {uid4: {uid3: {}}})

## This should not throw one into the debugger
list_td = _list(ctx, top_down=True)

finally:
stop_xandikos_server(conn_details)

Expand Down

0 comments on commit 8de2012

Please sign in to comment.