创建通知时出错,`为关键字参数' user'`获取了多个值?

时间:2017-04-27 13:17:30

标签: python flask flask-socketio

我的问题是,我无法在向特定用户发送消息后创建通知并推送通知

每次都会收到此错误:

File "/home/reznov/.local/lib/python2.7/site-packages/flask_login/utils.py", line 228, in decorated_view
return func(*args, **kwargs)
File "/home/reznov/Desktop/project/app/develop/panel/views.py", line 476, in chat
message='You got a message from {}'.format(from_user)
TypeError: create_user_notification() got multiple values for keyword argument 'user'

通知的表格如下所示:

class Notification(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    user = db.Column(db.Integer(), db.ForeignKey('user.id'))
    action = db.Column(db.String(), nullable=False)
    title = db.Column(db.String(), nullable=False)
    message = db.Column(db.String(), nullable=False)
    received_at = db.Column(db.DateTime(), default=datetime.now())

处理已发送消息和通知过程的函数如下所示,注意到

我正在保存邮件然后发送通知,然后将其推送给用户:

@only_client
@is_active_client
@socketio.on('response', namespace='/client/chat/')
def connect_handler():
    user_room = 'user_{}'.format(session['client_logged_in'])
    join_room(user_room)
    emit('response', {'meta': 'WS connected'})

@abonent_panel_route.route('/panel/chat/', methods=['GET','POST'])
@login_required
def chat():
    user = User.query.filter_by(id=current_user.id).first_or_404()
    from_user = user.name + ' ' + user.family
    if request.method == 'POST':
        text_message = parse_arg_from_requests('message')
        client_id = parse_arg_from_requests('client_id')
        message = Messages(
            message=text_message,
            user_id=current_user.id,
            client_id=client_id
        )
        notification = Notification()
        notification.user = user
        notification.action = 'got message'
        notification.title = text_message
        notification.message = 'You got a message from {}'.format(from_user)

        user.push_user_notification(user.id) # This should not be here sorry, i deleted it .
        db.session.add(message)
        db.session.commit()
        return jsonify({'result':'success'})
    return render_template('panel/chat.html')

这是我的用户模型,其中应该发生魔术:

def get_unread_notifs(self, reverse=False):
    notifs = []
    unread_notifs = Notification.query.filter_by(user=self, has_read=False)
    for notif in unread_notifs:
        notifs.append({
            'title': notif.title,
            'received_at': humanize.naturaltime(datetime.now() - notif.received_at),
            'mark_read': url_for('profile.mark_notification_as_read', notification_id=notif.id)
        })

    if reverse:
        return list(reversed(notifs))
    else:
        return notifs

def get_unread_notif_count(self):
    unread_notifs = Notification.query.filter_by(user=self, has_read=False)
    return unread_notifs.count() or len(unread_notifs) if not unread_notifs.count()

def create_user_notification(user, action, title, message):
    notification = Notification(
        user=user,
        action=action,
        title=title,
        message=message,
        received_at=datetime.now()
    )

    if notification:
        db.session.add(notification)
        db.session.commit()
        push_user_notification(user)

def push_user_notification(user):
    user_room = 'user_{}'.format(user)
    emit('response',
         {'meta': 'New notifications',
          'notif_count': user.get_unread_notif_count(),
          'notifs': user.get_unread_notifs()},
         room=user_room,
         namespace='/client/chat/')

0 个答案:

没有答案