示例#1
0
def _send_email_handler(message, type_="activate"):
    from app.user.document import CodeDocument, UserDocument

    code_id = message.body

    code = CodeDocument.get_collection(True).find_one({
        '_id': ObjectId(code_id)
    })
    if not code:
        return True

    user = UserDocument.get_collection(True).find_one({
        "_id": ObjectId(code["uid"])
    })
    if not user:
        return True

    if type_ == "activate":
        subject = "账号激活【Young社区】"
        tpl = ''.join([
            '请点击链接激活你的Young社区帐号!',
            '<a href="{0}/account/active?uid={1}&code={2}">',
            '{0}/account/active?uid={1}&code={2}</a>'
        ])
    else:
        subject = "密码重置【Young社区】"
        tpl = ''.join([
            '请点击链接重置你的Young社区账号密码!',
            '<a href="{0}/password/reset?uid={1}&code={2}">',
            '{0}/password/reset?uid={1}&code={2}</a>'
        ])

    body = tpl.format(EMAIL_SETTINGS['url'], code['uid'], code['code'])

    msg = MIMEText(body, "html", 'utf-8')
    msg["subject"] = subject

    try:
        send_email(user['email'], msg)
    except Exception as e:
        logging.error('%s' % e)

    return True
示例#2
0
文件: handler.py 项目: anstones/yong
def _send_email_handler(message, type_="activate"):
    from app.user.document import CodeDocument, UserDocument

    code_id = message.body

    code = CodeDocument.get_collection(True).find_one(
        {'_id': ObjectId(code_id)})
    if not code:
        return True

    user = UserDocument.get_collection(True).find_one(
        {"_id": ObjectId(code["uid"])})
    if not user:
        return True

    if type_ == "activate":
        subject = "账号激活【Young社区】"
        tpl = ''.join([
            '请点击链接激活你的Young社区帐号!',
            '<a href="{0}/account/active?uid={1}&code={2}">',
            '{0}/account/active?uid={1}&code={2}</a>'
        ])
    else:
        subject = "密码重置【Young社区】"
        tpl = ''.join([
            '请点击链接重置你的Young社区账号密码!',
            '<a href="{0}/password/reset?uid={1}&code={2}">',
            '{0}/password/reset?uid={1}&code={2}</a>'
        ])

    body = tpl.format(EMAIL_SETTINGS['url'], code['uid'], code['code'])

    msg = MIMEText(body, "html", 'utf-8')
    msg["subject"] = subject

    try:
        send_email(user['email'], msg)
    except Exception as e:
        logging.error('%s' % e)

    return True
示例#3
0
文件: handler.py 项目: xxoxx/Young
    def _new_chat_message_handler(self, messages):
        '''处理未读的聊天信息, 返回前端易于处理的json格式. 返回的格式为:
        {
            'topic': MessageTopic.CHAT_MESSAGE_NEW,
            'each_people':
            [
                {
                    'sender': {'id': xxx, 'name': xxx},
                    'messages':
                    [
                        {
                            'body': xxx,
                            'html': xxx,
                            'since': xxx
                        },
                    ]
                },
            ]
        }

        :Parameters:
            - `messages`: ChatMessageDocument.get_unread_messages()相同的格式. 即:
              [
                  {
                      '_id': xxx,
                      'messages':
                      [
                          {
                              'send_time': xxx,
                              'body': xxx,
                          },
                      ]
                  },
              ]
        '''

        response_data = {
            'topic': MessageTopic.CHAT_MESSAGE_NEW,
            'each_people': []
        }
        for item in messages:
            sender_id = str(item['_id'].id)
            sender = UserDocument.get_collection(pymongo=True).find_one({
                '_id': ObjectId(sender_id)
            })

            each_people = {
                'sender': {
                    'id': sender_id,
                    'name': sender['name']
                },
                'messages': []
            }
            for message in item['messages']:
                html = self.render_string(
                    "chat/template/message-others.html",
                    message=message, handler=self
                )

                new_message = {
                    'body': xhtml_escape(message['body']),
                    'html': html,
                    'since': time.mktime(
                        message['send_time'].timetuple()
                    ) * 1000
                }
                each_people['messages'].append(new_message)

            response_data['each_people'].append(each_people)
        return json.dumps(response_data)
示例#4
0
    def _new_chat_message_handler(self, messages):
        '''处理未读的聊天信息, 返回前端易于处理的json格式. 返回的格式为:
        {
            'topic': MessageTopic.CHAT_MESSAGE_NEW,
            'each_people':
            [
                {
                    'sender': {'id': xxx, 'name': xxx},
                    'messages':
                    [
                        {
                            'body': xxx,
                            'html': xxx,
                            'since': xxx
                        },
                    ]
                },
            ]
        }

        :Parameters:
            - `messages`: ChatMessageDocument.get_unread_messages()相同的格式. 即:
              [
                  {
                      '_id': xxx,
                      'messages':
                      [
                          {
                              'send_time': xxx,
                              'body': xxx,
                          },
                      ]
                  },
              ]
        '''

        response_data = {
            'topic': MessageTopic.CHAT_MESSAGE_NEW,
            'each_people': []
        }
        for item in messages:
            sender_id = str(item['_id'].id)
            sender = UserDocument.get_collection(pymongo=True).find_one({
                '_id': ObjectId(sender_id)
            })

            each_people = {
                'sender': {
                    'id': sender_id,
                    'name': sender['name']
                },
                'messages': []
            }
            for message in item['messages']:
                html = self.render_string(
                    "chat/template/message-others.html",
                    message=message, handler=self
                )

                new_message = {
                    'body': xhtml_escape(message['body']),
                    'html': html,
                    'since': time.mktime(
                        message['send_time'].timetuple()
                    ) * 1000
                }
                each_people['messages'].append(new_message)
            response_data['each_people'].append(each_people)

        return response_data