def mark_as_read(notification_id): """ Mark a notification as read Input: notification_id: in url Output: 200 if updated successfully """ user = g.user notification = Notification.get(notification_id) if not notification or notification.user_id != user.id: return jsonify(error="Forbidden"), 403 notification.read = True db.session.commit() return jsonify(done=True), 200
def test_mark_notification_as_read(flask_client): user = User.create( email="[email protected]", password="******", name="Test User", activated=True ) db.session.commit() # create api_key api_key = ApiKey.create(user.id, "for test") db.session.commit() Notification.create(id=1, user_id=user.id, message="Test message 1") db.session.commit() r = flask_client.post( url_for("api.mark_as_read", notification_id=1), headers={"Authentication": api_key.code}, ) assert r.status_code == 200 notification = Notification.get(1) assert notification.read
def notification_route(notification_id): notification = Notification.get(notification_id) if not notification: flash("Incorrect link. Redirect you to the home page", "warning") return redirect(url_for("dashboard.index")) if notification.user_id != current_user.id: flash( "You don't have access to this page. Redirect you to the home page", "warning", ) return redirect(url_for("dashboard.index")) if request.method == "POST": notification_title = notification.title or notification.message[:20] Notification.delete(notification_id) Session.commit() flash(f"{notification_title} has been deleted", "success") return redirect(url_for("dashboard.index")) else: return render_template("dashboard/notification.html", notification=notification)