def testDeleteWithoutOwner(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) m = Conversation.add_new_message(self.u2.username, content_plain_text2, conv_key=c.key) Conversation.add_new_message(self.u1.username, content_plain_text, conv_key=c.key) # c.put() self.assertEqual(len(c.messages_list), 3, "Length of messages list != 3") Conversation.delete_message(self.u2.username, c.key.id(), m.key.id()) self.assertEqual(len(c.messages_list), 3, "Length of messages list != 3 after deletion") deleted = m.key.get() self.assertIsNotNone(deleted, "Message is None") self.assertEqual(deleted.deleted_for, self.u2.username, "Deleted for is incorrect") for k in c.messages_list: msg = k.get() if k != deleted.key: self.assertIsNone(msg.deleted_for, "deleted_for was not None for a message other than deleted")
def testDeleteConversationWithOneMsgAndOwner(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) self.assertEqual(len(c.messages_list), 1, "1ength of messages list != 1") m = c.messages_list[0] Conversation.delete_message(self.u1.username, c.key.id(), m.id()) conv = c.key.get() msg = m.get() self.assertIsNone(conv, "Conversation was not deleted") self.assertIsNone(msg, "Message was not deleted")
def notify_user(self, sender, conv_id, msg): #TODO: use boilerplate/task queue conv = Conversation.get_by_id(int(conv_id)) for uname in conv.receivers_list_norm: if uname != sender: user = User.get_user(uname) if user.notify_on_msg: new_message_notify(user.email, conv_id, msg)
def add_new_conversation(cls, sender, receiver, title, content): """Adds new conversation with receiver for sender, returns Conversation object Args: sender: sender's username as string receiver: receiver's username as string title: title of the conversation content: content of the first post Returns: tupple with: newly created Conversation instance newly created Message instance """ #TODO: check if sender and receiver aren't the same person, if so, add only once skey = cls.get_user(sender).key rkey = cls.get_user(receiver).key if sender == receiver: rl = [skey] rln = [sender] else: rl = [rkey, skey] rln = [sender, receiver] conv = Conversation( owner = skey, receivers_list = rl, receivers_list_norm = rln, title = title, ) msg = Message( sender = sender, content = content ) k = msg.put() conv.insert_message(k) ck = conv.put() User.add_conversation_for_users(ck, sender, receiver) return conv, msg
def testDeleteOneMessageWithOwner(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) m = Conversation.add_new_message(self.u2.username, content_plain_text2, conv_key=c.key) Conversation.add_new_message(self.u1.username, content_plain_text, conv_key=c.key) # c.put() self.assertEqual(len(c.messages_list), 3, "Length of messages list != 3") Conversation.delete_message(self.u1.username, c.key.id(), m.key.id()) self.assertEqual(len(c.messages_list), 2, "Length of messages list != 2 after deletion") deleted = m.key.get() self.assertIsNone(deleted, "Message was not deleted")
def testDeleteConversationWithoutOwner(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) m = Conversation.add_new_message(self.u2.username, content_plain_text2, conv_key=c.key) # c.put() Conversation.delete_conversation(self.u2.username, key=c.key) conv = c.key.get() self.assertIsNotNone(conv, "Conversation is None") self.assertEqual(conv.deleted_for, self.u2.username, "Conversation not deleted for %s" % self.u2.username) for m in conv.messages: self.assertEqual(m.deleted_for, self.u2.username, "Message not deleted for %s" % self.u2.username)
def testDeleteConversationWithOwner(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) m = Conversation.add_new_message(self.u2.username, content_plain_text2, conv_key=c.key) message_keys = deepcopy(c.messages_list) # c.put() Conversation.delete_conversation(self.u1.username, key=c.key) conv = c.key.get() self.assertIsNone(conv, "Conversation is not None") for k in message_keys: msg = k.get() self.assertIsNone(msg, "Message is not None")
def get_conversations_for(cls, username, offset, limit): """Gets conversations for user with username Returns: A list of Conversation objects for username """ limit = int(limit) if limit else 10 offset = int(offset) if offset else 0 c = Conversation.query(Conversation.receivers_list_norm.IN([username]))\ .order(-Conversation.modified)\ .fetch(limit=limit, offset=offset, keys_only=True) return ndb.get_multi(c)
def display_message(self, msg_id, conv_id): conv = Conversation.get_by_id(conv_id) if self.username not in conv.receivers_list_norm: self.abort(403) else: message = Message.get_by_id(msg_id) if message.sender != self.username: message.read = True message.put() template_values = { 'message' : message, 'conv_id': conv_id, 'username': self.username,} self.render_template("messages/display_message.html", **template_values)
def show_form_for_new_message(self, thread=None, id=None): """Shows a form for a brand new message and a reply if given thread and id """ context = {'username': self.username} if id and thread: id = int(id) thread = int(thread) msg = Message.get_by_id(id) conv = Conversation.get_by_id(thread) self.form.receiver.data = msg.sender self.form.title.data = conv.title self.render_template("messages/new_message.html", **context)
def testAddNewMessage(self): c, msg = User.add_new_conversation(self.u1.username, self.u2.username, title_plain_text, content_plain_text) m = Conversation.add_new_message(self.u2.username, content_plain_text2, conv_key=c.key) # c.put() self.assertEqual(len(c.messages_list), 2, "Length of message list %s != %s" % (len(c.messages_list), 2)) test_cont = (content_plain_text, content_plain_text2) for i, msg in enumerate(c.messages_list): newmsg = msg.get() self.assertEqual(newmsg.content, test_cont[i], "Message Content")
def post(self, conv_id=None, msg_id=None): if not self.form.validate(): return self.show_form_for_new_message() # Adds a new message to conversation if conv_id and msg_id and conv_id.isdigit() and msg_id.isdigit(): msg = Conversation.add_new_message(self.form.sender.data, self.form.content.data, conv_id=conv_id) self.notify_user(self.form.sender.data, conv_id, msg) # new conversation: adds a new conversation with first message elif self.form.receiver.data and self.form.title.data and self.form.content.data: data = [self.form.data.get(i) for i in ('sender', 'receiver', 'title', 'content')] (conv, msg) = User.add_new_conversation(*data) self.notify_user(self.form.sender.data, conv.key.id(), msg) else: self.response.out.write("Error in Messages.post()") self.redirect(self.request.referer)