def add_notification(self, name, data, related_id=0, permanent=False): from app.email import send_email if not permanent: self.notifications.filter_by(name=name).delete() n = Notification(name=name, payload_json=json.dumps(data), user=self, related_id=related_id) db.session.add(n) db.session.commit() n = Notification.query.get(n.id) kwargs = data kwargs['related'] = related_id get_queue().enqueue(send_email, recipient=self.email, subject='You Have a new notification on Mediville', template='account/email/notification', user=self.id, link=url_for('main.notifications', _external=True), notification=n.id, **kwargs) if not current_app.config['DEBUG']: ws_url = "https://www.mediville.com" path = 'sockets/socket.io' else: get_queue().empty() ws_url = "http://localhost:3000" path = "socket.io" sio = socketio.Client() sio.connect( ws_url + "?token={}".format(create_access_token(identity=current_user.id)), socketio_path=path) data = n.parsed() u = jsonify_object(data['user']) tu = jsonify_object(self) data['user'] = { key: u[key] for key in u.keys() & {'first_name', 'id', 'email', 'socket_id'} } data['touser'] = { key: tu[key] for key in tu.keys() & {'first_name', 'id', 'email', 'socket_id'} } sio.emit('new_notification', {'notification': data}) return n
def post(self, recipient_id): data = self.parser.parse_args() user = User.query.filter_by(id=recipient_id).first_or_404() msg = Message(user_id=current_user.id, recipient_id=recipient_id, body=strip_tags(data['message'])) db.session.add(msg) db.session.commit() db.session.refresh(msg) user.add_notification('unread_message', {'message': msg.id, 'count': user.new_messages()}, related_id=current_user.id, permanent=True) msg = Message.query.get(msg.id) return { 'status': 1, 'message': jsonify_object(msg) }
def serialize(self): return jsonify_object(self)
def toJson(self): return jsonify_object(self)