From 78aa1b611dfd78d4df7b06de9e197692eb47f108 Mon Sep 17 00:00:00 2001 From: Scott Mcdermott Date: Sat, 17 Sep 2022 15:39:13 -0700 Subject: [PATCH] serialize_tags: make .filter(tags__contains='string') work the example in readme says this example works, but actually it splits the argument by ',' unconditionally, expecting a list. so a string will be made into a "s,t,r,i,n,g". so we should handle getting passed either a single string and pass it through unchanged, or a list and join them actually if a list, we'd want to dedupe so we use a set which is the native type of the library for tags anyways --- README.rst | 2 +- tasklib/serializing.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index cc36c3f..7a8e471 100644 --- a/README.rst +++ b/README.rst @@ -36,7 +36,7 @@ tasklib has a similar API to that of Django's ORM:: >>> tasks = tw.tasks.pending() >>> tasks ['Tidy the house', 'Learn German'] - >>> tasks.filter(tags__contain='chores') + >>> tasks.filter(tags__contains='chores') ['Tidy the house'] >>> type(tasks[0]) diff --git a/tasklib/serializing.py b/tasklib/serializing.py index 7b7a46e..4f7d7d2 100644 --- a/tasklib/serializing.py +++ b/tasklib/serializing.py @@ -174,7 +174,13 @@ def deserialize_annotations(self, data): return [TaskAnnotation(self, d) for d in data] if data else [] def serialize_tags(self, tags): - return ','.join(tags) if tags else '' + if isinstance(tags, list): + tags = set(tags) + if isinstance(tags, set): + return ','.join(tags) if tags else '' + if isinstance(tags, str): + return tags + raise ValueError("serialize_tags only supports list, set or string") def deserialize_tags(self, tags): if isinstance(tags, str):