def queryUnreadMessagesForConversationAndUser(conversation, user): """Creates a query for unread messages in a conversation for a user. Args: conversation: Key (ndb) of GCIConversation. user: Key (ndb) of User. Returns: An ndb query for GCIMessages the user has not yet read in the conversation. If the user is not part of the conversation, None is returned. """ conversation_user = queryConversationUserForConversationAndUser( conversation, user).get() if not conversation_user: return None date_last_seen = conversation_user.last_message_seen_on # The > filter in the query below seemed to still include equivalent # datetimes, so incrememting this by a second fixes this. date_last_seen += timedelta(seconds=1) return (gcimessage_logic.queryForConversation(conversation) .filter(gcimessage_model.GCIMessage.sent_on > date_last_seen))
def context(self, data, check, mutator): assert access_checker.isSet(data.conversation) assert access_checker.isSet(data.user) # Marks the conversation as "read" for the user gciconversation_logic.markAllReadForConversationAndUser( data.conversation.key, ndb.Key.from_old_key(data.user.key()) ) num_users = gciconversation_logic.queryConversationUserForConversation(data.conversation.key).count() messages = gcimessage_logic.queryForConversation(data.conversation.key).order( gcimessage_model.GCIMessage.sent_on ) for message in messages: message.author_name = db.get(ndb.Key.to_old_key(message.author)).name message.sent_on_relative = timeformat_helper.relativeTime(message.sent_on) message.sent_on_ctime = message.sent_on.ctime() return { "page_name": data.conversation.subject, "conversation": data.conversation, "num_users": num_users, "messages": messages, "user_list": UserList(data), "user_actions": UserActions(data), "reply_action": urlresolvers.reverse(url_names.GCI_CONVERSATION_REPLY, kwargs=data.kwargs), }
def queryUnreadMessagesForConversationAndUser(conversation, user): """Creates a query for unread messages in a conversation for a user. Args: conversation: Key (ndb) of GCIConversation. user: Key (ndb) of User. Returns: An ndb query for GCIMessages the user has not yet read in the conversation. If the user is not part of the conversation, None is returned. """ conversation_user = queryConversationUserForConversationAndUser( conversation, user).get() if not conversation_user: return None date_last_seen = conversation_user.last_message_seen_on # The > filter in the query below seemed to still include equivalent # datetimes, so incrememting this by a second fixes this. date_last_seen += timedelta(seconds=1) return (gcimessage_logic.queryForConversation(conversation).filter( gcimessage_model.GCIMessage.sent_on > date_last_seen))
def testQueryForConversation(self): """Tests that queryForConversation returns a query for all GCIMessage entities for a particular conversation. """ expected_keys = self.msg_keys actual_keys = gcimessage_logic.queryForConversation( conversation=self.conv.key).fetch(20, keys_only=True) self.assertEqual(expected_keys, actual_keys)
def context(self, data, check, mutator): assert access_checker.isSet(data.conversation) assert access_checker.isSet(data.user) # Marks the conversation as "read" for the user gciconversation_logic.markAllReadForConversationAndUser( data.conversation.key, ndb.Key.from_old_key(data.user.key())) num_users = ( gciconversation_logic.queryConversationUserForConversation( data.conversation.key).count()) messages = gcimessage_logic.queryForConversation( data.conversation.key).order(gcimessage_model.GCIMessage.sent_on) for message in messages: message.author_name = db.get(ndb.Key.to_old_key( message.author)).name message.sent_on_relative = timeformat_helper.relativeTime( message.sent_on) message.sent_on_ctime = message.sent_on.ctime() return { 'page_name': data.conversation.subject, 'conversation': data.conversation, 'num_users': num_users, 'messages': messages, 'user_list': UserList(data), 'user_actions': UserActions(data), 'reply_action': urlresolvers.reverse(url_names.GCI_CONVERSATION_REPLY, kwargs=data.kwargs) }
def assertConversation( self, conversation, subject=None, creator=None, recipients_type=None, include_admins=None, include_mentors=None, include_students=None, include_winners=None, organization=None, message_content=None, users=None, auto_update_users=None): """Asserts that a conversation has been created with certain properties. Only conversation must be passed. The other attributes are optional, and will not be tested if None. Args: conversation: Key (ndb) of GCIConversation. subject: If not None, test that the conversation has this subject string. creator: If not None, test that the conversation and first message (if applicable) was created by this User key (ndb). recipients_type: If not None, test that the conversation has this recipients_type. include_admins: If not None, test that the conversations will include admins or not. include_mentors: If not None, test that the conversations will include mentors or not. include_students: If not None, test that the conversations will include students or not. include_winners: If not None, test that the conversations will include winners or not. organization: If not None, test that the conversation organization is this organization key (ndb). message_content: If not None, test that the first message in the conversation has this content. users: If not None, test that this list of User keys (ndb) are added to the conversation. auto_update_users: If not None, test that the conversation has the correct auto_update_users value. """ self.assertIsNotNone(conversation, msg='The conversation key is None.') conversation_ent = conversation.get() message_query = (gcimessage_logic.queryForConversation(conversation) .order(gcimessage_model.GCIMessage.sent_on) .fetch(1)) message_ent = message_query[0] if message_query else None if subject is not None: self.assertEqual( subject, conversation_ent.subject, msg='Conversation subject is incorrect.') if creator is not None: self.assertEqual( creator, conversation_ent.creator, msg='Conversation creator is incorrect.') if message_ent: self.assertEqual( creator, message_ent.author, msg='Conversation message author is incorrect.') if recipients_type is not None: self.assertEqual( recipients_type, conversation_ent.recipients_type, msg='Conversation recipients_type is incorrect.') if include_admins is not None: self.assertEqual( include_admins, conversation_ent.include_admins, msg='Conversation include_admins is incorrect.') if include_mentors is not None: self.assertEqual( include_mentors, conversation_ent.include_mentors, msg='Conversation include_mentors is incorrect.') if include_students is not None: self.assertEqual( include_students, conversation_ent.include_students, msg='Conversation include_students is incorrect.') if include_winners is not None: self.assertEqual( include_winners, conversation_ent.include_winners, msg='Conversation include_winners is incorrect.') if organization is not None: self.assertEqual( organization, conversation_ent.organization, msg='Conversation organization is incorrect.') if message_content is not None: self.assertIsNotNone( message_ent, msg='Cannot test message content because initial message is None.') self.assertEqual( message_content, message_ent.content, msg='Conversation message content is incorrect.') if users is not None: added_users = map( lambda e: e.user, gciconversation_logic.queryConversationUserForConversation( conversation)) self.assertEqual( set(users), set(added_users), msg='Conversation users are incorrect.') if auto_update_users is not None: self.assertEqual( auto_update_users, conversation_ent.auto_update_users, msg='Conversation auto_update_users is incorrect.')
def assertConversation(self, conversation, subject=None, creator=None, recipients_type=None, include_admins=None, include_mentors=None, include_students=None, include_winners=None, organization=None, message_content=None, users=None, auto_update_users=None): """Asserts that a conversation has been created with certain properties. Only conversation must be passed. The other attributes are optional, and will not be tested if None. Args: conversation: Key (ndb) of GCIConversation. subject: If not None, test that the conversation has this subject string. creator: If not None, test that the conversation and first message (if applicable) was created by this User key (ndb). recipients_type: If not None, test that the conversation has this recipients_type. include_admins: If not None, test that the conversations will include admins or not. include_mentors: If not None, test that the conversations will include mentors or not. include_students: If not None, test that the conversations will include students or not. include_winners: If not None, test that the conversations will include winners or not. organization: If not None, test that the conversation organization is this organization key (ndb). message_content: If not None, test that the first message in the conversation has this content. users: If not None, test that this list of User keys (ndb) are added to the conversation. auto_update_users: If not None, test that the conversation has the correct auto_update_users value. """ self.assertIsNotNone(conversation, msg='The conversation key is None.') conversation_ent = conversation.get() message_query = ( gcimessage_logic.queryForConversation(conversation).order( gcimessage_model.GCIMessage.sent_on).fetch(1)) message_ent = message_query[0] if message_query else None if subject is not None: self.assertEqual(subject, conversation_ent.subject, msg='Conversation subject is incorrect.') if creator is not None: self.assertEqual(creator, conversation_ent.creator, msg='Conversation creator is incorrect.') if message_ent: self.assertEqual( creator, message_ent.author, msg='Conversation message author is incorrect.') if recipients_type is not None: self.assertEqual(recipients_type, conversation_ent.recipients_type, msg='Conversation recipients_type is incorrect.') if include_admins is not None: self.assertEqual(include_admins, conversation_ent.include_admins, msg='Conversation include_admins is incorrect.') if include_mentors is not None: self.assertEqual(include_mentors, conversation_ent.include_mentors, msg='Conversation include_mentors is incorrect.') if include_students is not None: self.assertEqual(include_students, conversation_ent.include_students, msg='Conversation include_students is incorrect.') if include_winners is not None: self.assertEqual(include_winners, conversation_ent.include_winners, msg='Conversation include_winners is incorrect.') if organization is not None: self.assertEqual(organization, conversation_ent.organization, msg='Conversation organization is incorrect.') if message_content is not None: self.assertIsNotNone( message_ent, msg= 'Cannot test message content because initial message is None.') self.assertEqual(message_content, message_ent.content, msg='Conversation message content is incorrect.') if users is not None: added_users = map( lambda e: e.user, gciconversation_logic.queryConversationUserForConversation( conversation)) self.assertEqual(set(users), set(added_users), msg='Conversation users are incorrect.') if auto_update_users is not None: self.assertEqual( auto_update_users, conversation_ent.auto_update_users, msg='Conversation auto_update_users is incorrect.')