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