def add_reply( self, user: '******', comment: str, reply_type: CommentReplyType, in_reply_to: t.Optional[CommentReply], ) -> CommentReply: """Add a reply to this comment base. :param user: The author of the reply. :param comment: The text of the reply. :param reply_type: The format of the reply text. :param in_reply_to: An optional reply, in this case the new reply will be considered a reply on this given reply. :returns: The created reply. """ reply = CommentReply( comment=comment, author=user, reply_type=reply_type, in_reply_to=in_reply_to, comment_base=self, ) invalidate_cached_property(self, 'user_visible_replies') return reply
def test_invalidate_cached_property_on_non_property(): class A: def __init__(self): self.prop = 42 a = A() with pytest.raises(TypeError): utils.invalidate_cached_property(a, "prop")
def test_can_invalidate_cached_property(): foo = [] class A: def prop(self): foo.append(42) return 42 prop = utils.cached_property(prop) a = A() p = a.prop q = a.prop assert p == q == 42 assert foo == [42] utils.invalidate_cached_property(a, "prop") r = a.prop assert r == 42 assert foo == [42, 42] s = a.prop assert s == 42 assert foo == [42, 42]
def test_can_invalidate_cached_property(): foo = [] class A(object): def prop(self): foo.append(42) return 42 prop = utils.cached_property(prop) a = A() p = a.prop q = a.prop assert p == q == 42 assert foo == [42] utils.invalidate_cached_property(a, "prop") r = a.prop assert r == 42 assert foo == [42, 42] s = a.prop assert s == 42 assert foo == [42, 42]
def test_project_dates(test_db, new_project): # without any session the project will have no start and end dates assert new_project.sessions.count() == 0 assert new_project.schedule_start_at is None assert new_project.schedule_end_at is None assert new_project.datelocation == "Test Location" # let's add some sessions start_time_a = datetime(2019, 6, 12, 12, 15, 0).replace(tzinfo=new_project.timezone) end_time_a = start_time_a + timedelta(hours=3) new_session_a = Session( name="test-session-a", title="Test Session A", project=new_project, description="Test description", speaker_bio="Test speaker bio", is_break=False, featured=False, start_at=start_time_a, end_at=end_time_a, ) start_time_b = start_time_a + timedelta(days=2) end_time_b = end_time_a + timedelta(days=2) new_session_b = Session( name="test-session-b", title="Test Session B", project=new_project, description="Test description", speaker_bio="Test speaker bio", is_break=False, featured=False, start_at=start_time_b, end_at=end_time_b, ) test_db.session.add(new_session_a) test_db.session.add(new_session_b) test_db.session.commit() # now project.schedule_start_at will be the first session's start date # and project.schedule_end_at will be the last session's end date assert new_project.sessions.count() == 2 assert new_project.schedule_start_at.date() == new_session_a.start_at.date( ) assert new_project.schedule_end_at.date() == new_session_b.end_at.date() # Invalidate property cache invalidate_cached_property(new_project, 'datelocation') invalidate_cached_property(new_project, 'schedule_start_at_localized') invalidate_cached_property(new_project, 'schedule_end_at_localized') # both session dates are in same month, hence the format below. assert (new_project.datelocation == "{start_at}–{end_at} {month} {year}, {location}".format( start_at=start_time_a.day, end_at=end_time_b.day, month=start_time_a.strftime("%b"), year=end_time_b.year, location=new_project.location, )) # The sessions are in different months new_session_a.start_at = datetime(2019, 6, 28, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_a.end_at = datetime(2019, 6, 28, 14, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.start_at = datetime(2019, 7, 1, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.end_at = datetime(2019, 7, 1, 14, 15, 0).replace(tzinfo=new_project.timezone) test_db.session.commit() # Invalidate property cache invalidate_cached_property(new_project, 'datelocation') invalidate_cached_property(new_project, 'schedule_start_at_localized') invalidate_cached_property(new_project, 'schedule_end_at_localized') assert new_project.datelocation == "{start_date} {start_month}–{end_date} {end_month} {year}, {location}".format( start_date=new_session_a.start_at.strftime("%d"), start_month=new_session_a.start_at.strftime("%b"), end_date=new_session_b.end_at.strftime("%d"), end_month=new_session_b.end_at.strftime("%b"), year=new_session_b.end_at.year, location=new_project.location, ) # Both sessions are on same day new_session_a.start_at = datetime(2019, 6, 28, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_a.end_at = datetime(2019, 6, 28, 14, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.start_at = datetime(2019, 6, 28, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.end_at = datetime(2019, 6, 28, 14, 15, 0).replace(tzinfo=new_project.timezone) test_db.session.commit() # Invalidate property cache invalidate_cached_property(new_project, 'datelocation') invalidate_cached_property(new_project, 'schedule_start_at_localized') invalidate_cached_property(new_project, 'schedule_end_at_localized') assert (new_project.datelocation == "{start_date} {end_month} {year}, {location}".format( start_date=new_session_a.start_at.strftime("%d"), end_month=new_session_b.end_at.strftime("%b"), year=new_session_b.end_at.year, location=new_project.location, )) # The sessions are in different years new_session_a.start_at = datetime(2018, 12, 28, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_a.end_at = datetime(2018, 12, 28, 14, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.start_at = datetime(2019, 1, 1, 12, 15, 0).replace(tzinfo=new_project.timezone) new_session_b.end_at = datetime(2019, 1, 1, 14, 15, 0).replace(tzinfo=new_project.timezone) test_db.session.commit() # Invalidate property cache invalidate_cached_property(new_project, 'datelocation') invalidate_cached_property(new_project, 'schedule_start_at_localized') invalidate_cached_property(new_project, 'schedule_end_at_localized') assert new_project.datelocation == "{start_date} {start_month} {start_year}–{end_date} {end_month} {end_year}, {location}".format( start_date=new_session_a.start_at.strftime("%d"), start_month=new_session_a.start_at.strftime("%b"), end_date=new_session_b.end_at.strftime("%d"), end_month=new_session_b.end_at.strftime("%b"), start_year=new_session_a.start_at.strftime("%Y"), end_year=new_session_b.end_at.strftime("%Y"), location=new_project.location, )