예제 #1
0
파일: handler.py 프로젝트: anstones/yong
def send_has_unread_message_email_handler(message):
    '''如果用户不在线就发送邮件'''

    from young.handler import BaseHandler
    from app.user.document import UserDocument
    from app.message.document import MessageDocument

    message = MessageDocument.get_collection(pymongo=True).find_one(
        {'_id': ObjectId(message.body)})

    if not message:
        return True

    recipient_id = message['recipient'].id
    topic = MessageTopic.message_type2topic(message['message_type'])

    recipient = UserDocument.get_user_sync(recipient_id)
    if recipient and recipient['activated']:
        message = BaseHandler.translate_dbref_in_document(message)
        if 'data' in message:
            message['data'] = BaseHandler.translate_dbref_in_document(
                message['data'], depth=2)

        kwargs = {
            'message_topic': topic,
            'message': message,
            'MessageTopic': MessageTopic,
            'handler': BaseHandler
        }

        root = os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
        path = os.path.join(root, 'app/message/template')

        loader = template.Loader(path)
        html = loader.load("message.html").generate(**kwargs)

        soup = BeautifulSoup(html, "html.parser")
        link_list = soup.find_all('a')
        for link in link_list:
            new_link = link
            if link['href'].startswith('/'):
                new_link['href'] = EMAIL_SETTINGS['url'] + link['href']
                link.replace_with(new_link)

        img_list = soup.find_all('img')
        for img in img_list:
            new_img = img
            if img['src'].startswith('/'):
                new_img['src'] = EMAIL_SETTINGS['url'] + img['src']
                img.replace_with(new_img)

        body = ('{} &nbsp;&nbsp; <a href="{}/setting/notification">'
                '关闭邮件提醒</a>').format(soup.prettify(), EMAIL_SETTINGS["url"])

        msg = MIMEText(body, "html", 'utf-8')
        msg["subject"] = "你有未读消息【Young社区】"
        send_email(recipient['email'], msg)

    return True
예제 #2
0
파일: handler.py 프로젝트: anstones/yong
def message_handler(message):
    '''向用户发送消息'''

    from app.message.document import MessageDocument

    message = MessageDocument.get_collection(pymongo=True).find_one(
        {'_id': ObjectId(message.body)})

    if not message:
        return True

    recipient_id = message['recipient'].id
    topic = MessageTopic.message_type2topic(message['message_type'])

    _reponse_browser_callback(recipient_id, topic, message)
    return True
예제 #3
0
def message_handler(message):
    '''向用户发送消息'''

    from app.message.document import MessageDocument

    message = MessageDocument.get_collection(pymongo=True).find_one(
        {'_id': ObjectId(message.body)}
    )

    if not message:
        return True

    recipient_id = message['recipient'].id
    topic = MessageTopic.message_type2topic(message['message_type'])

    _reponse_browser_callback(recipient_id, topic, message)
    return True
예제 #4
0
def send_has_unread_message_email_handler(message):
    '''如果用户不在线就发送邮件'''

    from young.handler import BaseHandler
    from app.user.document import UserDocument
    from app.message.document import MessageDocument

    message = MessageDocument.get_collection(pymongo=True).find_one(
        {'_id': ObjectId(message.body)}
    )

    if not message:
        return True

    recipient_id = message['recipient'].id
    topic = MessageTopic.message_type2topic(message['message_type'])

    recipient = UserDocument.get_user_sync(recipient_id)
    if recipient and recipient['activated']:
        message = BaseHandler.translate_dbref_in_document(message)
        if 'data' in message:
            message['data'] = BaseHandler.translate_dbref_in_document(
                message['data'], depth=2
            )

        kwargs = {
            'message_topic': topic,
            'message': message,
            'MessageTopic': MessageTopic,
            'handler': BaseHandler
        }

        root = os.path.dirname(os.path.dirname(
            os.path.dirname(os.path.abspath(__file__))
        ))
        path = os.path.join(root, 'app/message/template')

        loader = template.Loader(path)
        html = loader.load("message.html").generate(**kwargs)

        soup = BeautifulSoup(html, "html.parser")
        link_list = soup.find_all('a')
        for link in link_list:
            new_link = link
            if link['href'].startswith('/'):
                new_link['href'] = EMAIL_SETTINGS['url'] + link['href']
                link.replace_with(new_link)

        img_list = soup.find_all('img')
        for img in img_list:
            new_img = img
            if img['src'].startswith('/'):
                new_img['src'] = EMAIL_SETTINGS['url'] + img['src']
                img.replace_with(new_img)

        body = (
            '{} &nbsp;&nbsp; <a href="{}/setting/notification">'
            '关闭邮件提醒</a>'
        ).format(soup.prettify(), EMAIL_SETTINGS["url"])

        msg = MIMEText(body, "html", 'utf-8')
        msg["subject"] = "你有未读消息【Young社区】"
        send_email(recipient['email'], msg)

    return True