Ejemplo n.º 1
0
    def add_new_conversation(cls, sender, receiver, title, content):
        """Adds new conversation with receiver for sender, returns Conversation object
        """
        #TODO: check if sender and receiver aren't the same person, if so, add only once

        skey = ndb.Key('User', sender)
        rkey = ndb.Key('User', receiver)

        if sender == receiver:
            rl = [skey]
            rln  = [sender]
        else:
            rl = [rkey, skey]
            rln = [sender, receiver]

        conv = Conversation(
            owner = sender,
            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
Ejemplo n.º 2
0
    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 = ndb.Key('User', sender)
        rkey = ndb.Key('User', receiver)

        if sender == receiver:
            rl = [skey]
            rln  = [sender]
        else:
            rl = [rkey, skey]
            rln = [sender, receiver]

        conv = Conversation(
            owner = sender,
            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
Ejemplo n.º 3
0
    def get_conversations(self, limit=10, offset=0):
        """UNUSED - Get conversations for current User object

        Returns:
         A list of Conversation objects
        """

        c = Conversation.query(self.username_norm in Conversation.receivers, keys_only=True)\
        .order(Conversation.modified)\
        .limit(limit, offset=offset)
        return ndb.get_multi(c)
Ejemplo n.º 4
0
    def get_conversations(self, limit=10, offset=0):
        """UNUSED - Get conversations for current User object

        Returns:
         A list of Conversation objects
        """

        c = Conversation.query(self.username_norm in Conversation.receivers, keys_only=True)\
                        .order(Conversation.modified)\
                        .limit(limit, offset=offset)
        return ndb.get_multi(c)
Ejemplo n.º 5
0
    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.lower()]))\
        .order(-Conversation.modified)\
        .fetch(limit=limit, offset=offset, keys_only=True)
        return ndb.get_multi(c)
Ejemplo n.º 6
0
    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.lower()]))\
                        .order(-Conversation.modified)\
                        .fetch(limit=limit, offset=offset, keys_only=True)
        return ndb.get_multi(c)