0

All my problem is, i can't create notification and push it after sending a message to a specific user,

am getting this error every time:

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'

The table of the Notifications looks like this:

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

The function that handle the sent message and the notifying process looks like the following, noticing that

I'am saving the message then am sending the notification which by it turn it pushes it to the user:

@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')

Here is my User model where the magic should happenes:

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/')
reznov11
  • 143
  • 4
  • 16
  • `create_user_notification` is a method of class or function? – Azat Ibrakov Apr 27 '17 at 13:20
  • what means `user=self` in instantiating of `Notifiaction` object? – Azat Ibrakov Apr 27 '17 at 13:21
  • Here am trying to get any argument for the user so maybe user.id or user.name etc. Also i tried to just type user=user or just self which by this am getting it from the class object and the same error raising up !! – reznov11 Apr 27 '17 at 13:23
  • Your call to `create_user_notification` inside `chat()` is not syntactically valid - something must have gone wrong when you pasted the code in. – jasonharper Apr 27 '17 at 13:25
  • if `def get_unread_notifs(self, reverse=False):` then it's some class method, but there is no class definition in your sample – Azat Ibrakov Apr 27 '17 at 13:28
  • I believe it is, am just calling a function from object class which do some of background processes , i have a lot of functions inside the **User Class** and they working very well , am confused about user parameter weither i should be called as an argument or as a self class parameter !! – reznov11 Apr 27 '17 at 13:29
  • this `def get_unread_notifs(self, reverse=False):` must be a class method so i can get the self.user, but for that function, am trying to save the notification and to push it thats all ! – reznov11 Apr 27 '17 at 13:42
  • I will edit my code , take a look to see the `class Notification`, – reznov11 Apr 27 '17 at 13:43

0 Answers0