def notifications_route(): page = 0 if request.args.get("page"): page = int(request.args.get("page")) notifications = ( Notification.filter_by(user_id=current_user.id).order_by( Notification.read, Notification.created_at.desc()).limit( PAGE_LIMIT + 1) # load a record more to know whether there's more .offset(page * PAGE_LIMIT).all()) return render_template( "dashboard/notifications.html", notifications=notifications, page=page, last_page=len(notifications) <= PAGE_LIMIT, )
def get_notifications(): """ Get notifications Input: - page: in url. Starts at 0 Output: - more: boolean. Whether there's more notification to load - notifications: list of notifications. - id - message - title - read - created_at """ user = g.user try: page = int(request.args.get("page")) except (ValueError, TypeError): return jsonify(error="page must be provided in request query"), 400 notifications = ( Notification.filter_by(user_id=user.id).order_by( Notification.read, Notification.created_at.desc()).limit( PAGE_LIMIT + 1) # load a record more to know whether there's more .offset(page * PAGE_LIMIT).all()) have_more = len(notifications) > PAGE_LIMIT return ( jsonify( more=have_more, notifications=[{ "id": notification.id, "message": notification.message, "title": notification.title, "read": notification.read, "created_at": notification.created_at.humanize(), } for notification in notifications[:PAGE_LIMIT]], ), 200, )