Exemple #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)
Exemple #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()

    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
        )
Exemple #4
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)
Exemple #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)