Skip to content

Commit

Permalink
✨ Adding tag and untag methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jossmoff committed Nov 12, 2023
1 parent e4a9be3 commit 85a9119
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
33 changes: 33 additions & 0 deletions bookshelf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,36 @@ def cancel_chapter(story_name):
bookshelf_console.render_story_panel(story)
except KeyboardInterrupt:
bookshelf_console.print(f'❌ Current chapter in \'{story_name}\' has been cancelled!')


@bookshelf.command(name='tag')
@click.argument('story_name', type=story_type)
@click.argument('tag', type=str)
def tag_story(story_name: str, tag: str):
"""Add a tag to a story on your bookshelf"""
story = bookshelf_storage.load_story(story_name)
story.add_tag(tag)
bookshelf_console.print(f'🏷️ The tag \'{tag}\' has been added to \'{story_name}\'!')
bookshelf_storage.save_story(story)


@bookshelf.command(name='untag')
@click.argument('story_name', type=story_type)
@click.argument('tag', type=str)
def tag_story(story_name: str, tag: str):
"""Remove a tag from a story on your bookshelf"""
story = bookshelf_storage.load_story(story_name)
story.remove_tag(tag)
bookshelf_console.print(f'🏷️ The tag \'{tag}\' has been removed from \'{story_name}\'!')
bookshelf_storage.save_story(story)


@bookshelf.command(name='info')
@click.argument('story_name', type=story_type)
def story_info_entry(story_name: str):
"""Displays the information for a given story on your bookshelf"""
try:
story = bookshelf_storage.load_story(story_name)
bookshelf_console.render_story_panel(story)
except KeyboardInterrupt:
pass
13 changes: 12 additions & 1 deletion bookshelf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def finish_story(self) -> None:
self.finish_chapter()
self.end_date = self.get_last_chapter().end_time

def _has_tag(self, tag: str) -> bool:
return tag in self.tags

def add_tag(self, tag: str) -> None:
if not self._has_tag(tag):
self.tags.append(tag)

def remove_tag(self, tag: str) -> None:
if self._has_tag(tag):
self.tags.remove(tag)

def is_finished(self) -> bool:
return self.end_date is not None

Expand All @@ -96,7 +107,7 @@ def from_json(cls, data: dict) -> 'Story':
end_date = datetime.fromisoformat(data['end_date']) if data['end_date'] else None
tags = data['tags']
chapters = [Chapter.from_json(chapter_data) for chapter_data in data['chapters']]
return cls(name, start_date, end_date, chapters, tags)
return cls(name, start_date, end_date, chapters.copy(), tags.copy())

def in_progress(self) -> bool:
return self.get_last_chapter().in_progress() if len(self.chapters) > 0 else False
Expand Down
28 changes: 28 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ def test_story_to_dict(sample_story):
assert_that(story_dict).is_equal_to(STORY_JSON)


def test_add_new_tag_adds_tag():
story = Story.from_json(STORY_JSON)
new_tag = 'new_tag'
story.add_tag(new_tag)
assert_that(story.tags).contains_only('tag1', 'tag2', new_tag)


def test_add_duplicate_tag_does_not_add_tag():
story = Story.from_json(STORY_JSON)
existing_tag = 'tag1'
story.add_tag(existing_tag)
assert_that(story.tags).contains_only('tag1', 'tag2')


def test_remove_existing_tag_removes_tag():
story = Story.from_json(STORY_JSON)
existing_tag = 'tag1'
story.remove_tag(existing_tag)
assert_that(story.tags).contains_only('tag2')


def test_remove_non_existing_tag_does_not_remove_tag():
story = Story.from_json(STORY_JSON)
non_existing_tag = 'non_existing_tag'
story.remove_tag(non_existing_tag)
assert_that(story.tags).contains_only('tag1', 'tag2')


def test_story_from_dict():
story = Story.from_json(STORY_JSON)
assert story.name == STORY_NAME
Expand Down

0 comments on commit 85a9119

Please sign in to comment.