Пример #1
0
def send_message(thread_id,
                 sender_id,
                 message_text,
                 sender_name=None):
    """
    This function takes Thread object id (first argument),
    sender id (second argument), message text (third argument)
    and can also take sender's name.

    It creates a new Message object and increases the
    values stored in Redis that represent the total number
    of messages for the thread and the number of this thread's
    messages sent from this specific user.

    If a sender's name is passed, it also publishes
    the message in the thread's channel in Redis
    (otherwise it is assumed that the message was
    already published in the channel).
    """

    message = Message()
    message.text = message_text
    message.thread_id = thread_id
    message.sender_id = sender_id
    message.save()

    thread_id = str(thread_id)
    sender_id = str(sender_id)

    #r = redis.StrictRedis()
    redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
    urlparse.uses_netloc.append('redis')
    url = urlparse.urlparse(redis_url)
    r = redis.StrictRedis(host=url.hostname, port=url.port, db=0, password=url.password)
    #r = redis.StrictRedis(host='spinyfin.redistogo.com', port=10695, db=0, password='******')

    if sender_name:
        r.publish("".join(["thread_", thread_id, "_messages"]), json.dumps({
            "timestamp": dateformat.format(message.datetime, 'U'),
            "sender": sender_name,
            "text": message_text,
        }))

    for key in ("total_messages", "".join(["from_", sender_id])):
        r.hincrby(
            "".join(["thread_", thread_id, "_messages"]),
            key,
            1
        )
def send_message(thread_id, sender_id, message_text, sender_name=None):
    """
    This function takes Thread object id (first argument),
    sender id (second argument), message text (third argument)
    and can also take sender's name.

    It creates a new Message object and increases the
    values stored in Redis that represent the total number
    of messages for the thread and the number of this thread's
    messages sent from this specific user.

    If a sender's name is passed, it also publishes
    the message in the thread's channel in Redis
    (otherwise it is assumed that the message was
    already published in the channel).
    """

    message = Message()
    message.text = message_text
    message.thread_id = thread_id
    message.sender_id = sender_id
    message.save()
    if message_text == 'Средства отправлены':
        thread = Thread.objects.get(id=thread_id)
        blocked = Blocked.objects.get(id=thread.bl_id)
        user = User.objects.get(id=blocked.user_id)
        send_templated_mail(
            template_name="sended.html",
            from_email='*****@*****.**',
            recipient_list=[user.email],
            context={'username': user.username},
        )
    thread_id = str(thread_id)
    sender_id = str(sender_id)

    r = redis.StrictRedis()
    if sender_name:
        r.publish(
            "".join(["thread_", thread_id, "_messages"]),
            json.dumps({
                "sender": sender_name,
                "timestamp": dateformat.format(message.datetime, 'U'),
                "text": message_text,
            }))

    for key in ("total_messages", "".join(["from_", sender_id])):
        r.hincrby("".join(["thread_", thread_id, "_messages"]), key, 1)
Пример #3
0
def send_message(thread_id, sender_id, message_text, sender_name=None):
    """
    This function takes Thread object id (first argument),
    sender id (second argument), message text (third argument)
    and can also take sender's name.

    It creates a new Message object and increases the
    values stored in Redis that represent the total number
    of messages for the thread and the number of this thread's
    messages sent from this specific user.

    If a sender's name is passed, it also publishes
    the message in the thread's channel in Redis
    (otherwise it is assumed that the message was
    already published in the channel).
    """

    message = Message()
    message.text = message_text
    message.thread_id = thread_id
    message.sender_id = sender_id
    message.save()

    thread_id = str(thread_id)
    sender_id = str(sender_id)

    r = redis.StrictRedis()
    #assert False

    if sender_name:
        r.publish(
            "".join(["thread_id", thread_id, "_messages"]),
            json.dumps({
                "timestamp": dateformat.format(message.datetime, 'U'),
                "sender": sender_name,
                "text": message_text,
            }))

    for key in ("total_messages", "".join(["from_", sender_id])):
        r.hincrby("".join(["thread_", thread_id, "_messages"]), key, 1)
Пример #4
0
    def convert_pms(self):
        start_time = time.time()
        self.clear_messages()

        cursor = connection.cursor()
        # cursor.execute("SELECT * FROM smf_personal_messages ORDER BY id_pm ASC")
        cursor.execute(
            " SELECT f.id_pm, f.deleted_by_sender, f.id_member_from, f.from_name, \
            f.msgtime, f.subject, f.body, f.from_name, t.id_member, t.is_read, t.deleted FROM \
            smf_personal_messages AS f INNER JOIN smf_pm_recipients AS t ON f.id_pm = t.id_pm;"
        )
        rows = cursor.fetchall()
        for row in rows:
            try:
                message = Message()
                try:
                    sender = Profile.objects.get(old_user_id=row[2])
                    message.sender = sender.user
                except Profile.DoesNotExist, e:
                    if not row[2] == 0:
                        print "Profile does not exist for %s" % (row[2])
                        sender = None
                except Exception, e:
                    print "Sender %s does not exist." % (row[2])
                    sender = None

                try:
                    recipient = Profile.objects.get(old_user_id=row[8])
                    message.recipient = recipient.user
                except Profile.DoesNotExist, e:
                    try:
                        message.recipient = User.objects.get(username=row[7])
                    except User.DoesNotExist, e:
                        message.recipient_name = row[7]
                        message.recipient = None
                        print "Recipient %s could not be found." % row[8]
Пример #5
0
def send_message(thread_id, sender_id, message_text, sender_name=None):

    message = Message()
    message.text = message_text
    message.thread_id = thread_id
    message.sender_id = sender_id
    message.save()

    thread_id = str(thread_id)
    sender_id = str(sender_id)

    r = redis.StrictRedis()

    if sender_name:
        r.publish(
            "".join(["thread_", thread_id, "_messages"]),
            json.dumps({
                "timestamp": dateformat.format(message.datetime, 'U'),
                "sender": sender_name,
                "text": message_text,
            }))

    for key in ("total_messages", "".join(["from_", sender_id])):
        r.hincrby("".join(["thread_", thread_id, "_messages"]), key, 1)