From 71e53a5f5e4dfdb8a3dcfcd6a416aad905373ba2 Mon Sep 17 00:00:00 2001 From: Justin Sexton Date: Sat, 27 Feb 2021 16:59:07 -0600 Subject: [PATCH] Updated ReaderCollection to be able to display other helpful information around multiple readers --- chalicelib/model.py | 15 ++++++++++++++- tests/test_model.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/chalicelib/model.py b/chalicelib/model.py index 95439a7..288a524 100644 --- a/chalicelib/model.py +++ b/chalicelib/model.py @@ -64,10 +64,23 @@ class ReaderCollection(CamelCaseAttributesMixin): Will be most commonly used in responses """ - def __init__(self, readers: Sequence[Reader]): + def __init__(self, readers: Sequence[Reader], user_id: str = None): + self.flagged_by_any = any(reader.flagged for reader in readers) + self.read_by_any = len(readers) > 0 self.count = len(readers) self.reader_list = readers + # If a user id is provided we can calculate some quick things around + # if that user has flagged or read any messages in the collection. + # The properties use 'you' because the assumption is made that whoever makes this request + # will have their ID passed in most of the time. + if user_id is not None: + self.read_by_you = any(reader.user_id == user_id for reader in readers) + self.flagged_by_you = any(reader.user_id == user_id and reader.flagged for reader in readers) + else: + self.read_by_you = None + self.flagged_by_you = None + class ContactMessage(Document): """ diff --git a/tests/test_model.py b/tests/test_model.py index f2ee3a8..8990c1c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -2,7 +2,7 @@ import pytest -from chalicelib.model import Reason, Sender, Reader, ContactMessage +from chalicelib.model import Reason, Sender, Reader, ContactMessage, ReaderCollection @pytest.fixture @@ -92,3 +92,45 @@ def test_contact_message_is_correctly_serialized(contact_message: ContactMessage 'timeUpdated': '2001-09-09T01:46:40', 'timeCreated': '2001-09-09T01:46:40' } + + +def test_reader_collection_fields_are_calculated_correctly_without_user_id(reader: Reader): + readers = [reader] + collection = ReaderCollection(readers) + + assert collection.__dict__ == { + 'count': 1, + 'read_by_any': True, + 'flagged_by_any': True, + 'read_by_you': None, + 'flagged_by_you': None, + 'reader_list': readers + } + + +def test_reader_collection_fields_are_calculated_correctly_with_known_id(reader: Reader): + readers = [reader] + collection = ReaderCollection(readers, '123') + + assert collection.__dict__ == { + 'count': 1, + 'read_by_any': True, + 'flagged_by_any': True, + 'read_by_you': True, + 'flagged_by_you': True, + 'reader_list': readers + } + + +def test_reader_collection_fields_are_calculated_correctly_with_unknown_id(reader: Reader): + readers = [reader] + collection = ReaderCollection(readers, 'unknown id') + + assert collection.__dict__ == { + 'count': 1, + 'read_by_any': True, + 'flagged_by_any': True, + 'read_by_you': False, + 'flagged_by_you': False, + 'reader_list': readers + }