Skip to content
This repository has been archived by the owner on Aug 5, 2023. It is now read-only.

Commit

Permalink
Updated ReaderCollection to be able to display other helpful informat…
Browse files Browse the repository at this point in the history
…ion around multiple readers
  • Loading branch information
jusexton committed Feb 27, 2021
1 parent 9b9eaab commit 71e53a5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
15 changes: 14 additions & 1 deletion chalicelib/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
44 changes: 43 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 71e53a5

Please sign in to comment.