Example #1
0
def send_messages(*args, **kwargs):
    try:
        sender = User.query.get(kwargs.get('user_id'))
        recipients = User.query.filter(User.id != kwargs.get('user_id'))
        for user in recipients:
            message = Message()
            message.body = kwargs.get('body')
            # 发送方是自己
            message.sender = sender
            # 接收方是所有其他用户
            message.recipient = user
            db.session.add(message)
            db.session.commit()
            text_body = '''
            你好,
            这是liyang的博客管理团队发出的群邮件
            '''
            html_body = '''
            <p>你好 {0},</p>
            <p>{1}</p>
            <p> ----来自于Admin的邮件</p>
            '''.format(user.username, message.body)
            send_email('[myBlog] 温馨提醒',
                       sender=app.config['MAIL_SENDER'],
                       recipients=[user.email],
                       text_body=text_body,
                       html_body=html_body,
                       sync=True)
        job = get_current_job()  # 当前后台任务
        task = Task.query.get(job.get_id())  # 通过任务ID查出对应的Task对象
        task.complete = True
        db.session.commit()

    except Exception:
        app.logger.error('群发失败:', exc_info=sys.exc_info())
def talk(id):
    form = DisqusForm()
    if form.validate_on_submit():        
        message = Message()
        message.body = form.message.data
        message.sender_id = current_user.id
        message.recipient_id = id
        db.session.add(message)
        db.session.commit()        
        return redirect(url_for('main.talk', id=id))        
    messages = current_user.messeges_send
    return render_template('talking_room.html', form=form, messages=messages)
Example #3
0
def index():
    form = MsgForm()
    page = request.args.get('page', 1, type=int)
    if form.validate():
        with db.auto_commit():
            msg = Message()
            msg.name = form.name.data
            msg.body = form.body.data
            db.session.add(msg)
            print('提交成功')
        flash('发表成功')
        return redirect(url_for('msg.index'))

    pagination = Message.query.order_by(Message.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['MESSAGES_SHOW_PER_PAGE'],
        error_out=False)
    msgs = pagination.items
    return render_template('message_board.html',
                           form=form,
                           msgs=msgs,
                           pagination=pagination)
Example #4
0
def send_messages(*args, **kwargs):
    '''群发私信'''
    try:  # 由于 rq worker 运行在单独的进程中,当它出现意外错误时,我们需要捕获异常
        _set_task_progress(0)
        i = 0

        # 发送方
        sender = User.query.get(kwargs.get('user_id'))
        # 接收方
        recipients = User.query.filter(User.id != kwargs.get('user_id'))
        total_recipients = recipients.count()

        for user in recipients:
            message = Message()
            message.body = kwargs.get('body')
            message.sender = sender
            message.recipient = user
            db.session.add(message)
            # 给私信接收者发送新私信通知
            user.add_notification('unread_messages_count', user.new_recived_messages())
            db.session.commit()

            # 给接收者同时发送一封邮件(因为私信必须用户登录网站才看得到,邮件也需要)
            text_body = '''
            Dear {},
            {}
            Sincerely,
            The Madblog Team
            Note: replies to this email address are not monitored.
            '''.format(user.username, message.body)

            html_body = '''
            <p>Dear {0},</p>
            <p>{1}</p>
            <p>Sincerely,</p>
            <p>The Madblog Team</p>
            <p><small>Note: replies to this email address are not monitored.</small></p>
            '''.format(user.username, message.body)
            '''
            # 后台任务已经是异步了,所以send_email()没必要再用多线程异步,所以这里指定了 sync=True
            send_email('[Madblog] 温馨提醒',
                       sender=app.config['MAIL_SENDER'],
                       recipients=[user.email],
                       text_body=text_body,
                       html_body=html_body,
                       sync=True)
            '''
            # 模拟长时间的后台任务
            time.sleep(3)

            i += 1
            _set_task_progress(100 * i // total_recipients)

        # 群发结束后,由管理员再给发送方发送一条已完成的提示私信
        admin = User.query.filter_by(email=app.config['ADMINS'][0]).first()
        if sender != admin:  # 不能自己给自己发送私信
            message = Message()
            message.body = '[群发私信]已完成, 内容: \n\n' + kwargs.get('body')
            message.sender = admin
            message.recipient = sender
            db.session.add(message)
            # 给发送方发送新私信通知
            sender.add_notification('unread_messages_count', sender.new_recived_messages())
            db.session.commit()

    except Exception:
        app.logger.error('[群发私信]后台任务出错了', exc_info=sys.exc_info())
def send_messages(*args, **kwargs):
    """群发私信"""
    try:
        # 发送者
        _set_task_progress(0)
        i = 0
        sender = User.query.get(kwargs.get('user_id'))
        recipients = User.query.filter(User.id != sender.id)
        total_recipients = recipients.count()

        for user in recipients:
            message = Message()
            message.body = kwargs.get('body')
            message.sender = sender
            message.recipient = user
            db.session.add(message)
            user.add_notification('unread_message_count',
                                  user.new_recived_messages())
            db.session.commit()

            # 给接收者发送一封邮件
            text_body = '''
                        Dear {},
                        {}
                        Sincerely,
                        The Madblog Team
                        Note: replies to this email address are not monitored.
                        '''.format(user.username, message.body)

            html_body = '''
                        <p>Dear {0},</p>
                        <p>{1}</p>
                        <p>Sincerely,</p>
                        <p>The Madblog Team</p>
                        <p><small>Note: replies to this email address are not monitored.</small></p>
                        '''.format(user.username, message.body)
            # 后台任务已经是异步了,所以send_email()没必要再用多线程异步,所以这里指定了 sync=True
            send_email('[Madblog] 温馨提醒',
                       sender=app.config['MAIL_SENDER'],
                       recipients=[user.email],
                       text_body=text_body,
                       html_body=html_body,
                       sync=True)

            time.sleep(1)
            i += 1
            _set_task_progress(100 * i // total_recipients)

        job = get_current_job()
        task = Task.query.get(job.get_id())
        task.complate = True
        db.session.commit()

        # 群发结束后,由管理员再给发送方发送一条已完成的提示私信
        message = Message()
        message.body = '[群发私信]已完成, 内容: \n\n' + kwargs.get('body')
        message.sender = User.query.filter_by(
            email=app.config['ADMINS'][0]).first()
        message.recipient = sender
        db.session.add(message)
        # 给发送方发送新私信通知
        sender.add_notification('unread_messages_count',
                                sender.new_recived_messages())
        db.session.commit()

    except Exception as e:
        app.logger.error('[群发私信]后台任务出错了', exc_info=sys.exc_info())