Notifications
This page will document the notifications flow, where we are able to send the user emails with relevant information and also show it on a notifications tab from the navbar.
Notification-Related Components
1. Get Notifications API
Route:
get_notifications
Method: POST
Response: JSON
Function:
get_notifications(request)
Purpose: Retrieves all notifications for the current user, ordered by newest first.
Key Details:
User ID retrieved from session.
Queries
Notification
model filtering byuser_id
.Returns a list of notifications with fields:
message
and formattedcreated_at
timestamp.
pythonCopyEdit@view_config(route_name='get_notifications', renderer='json', request_method='POST')
def get_notifications(request):
user = request.session.get('user_id')
notifications = request.dbsession.query(Notification).filter_by(user_id=user).order_by(Notification.time_sent.desc()).all()
return {
'notifications': [
{
'message': n.message,
'created_at': n.time_sent.strftime("%Y-%m-%d %H:%M")
}
for n in notifications
]
}
2. Notification Creation in Event Subscribers
a) When a User is Added to a Project
Event:
UserAddedToProjectEvent
Handler:
handle_user_added_to_project(event)
Purpose:
Sends an email to the user notifying them of project addition.
Creates a
Notification
record for the user about the project addition.
Notification Data:
user_id
- the added userproject_id
- the project added tomessage
- "You have been added to the project {project.name}"time_sent
- current timestamp
pythonCopyEditnotif = Notification(
user_id=user.id,
project_id=project.id,
message=f"You have been added to the project {project.name}",
time_sent=datetime.now()
)
request.dbsession.add(notif)
request.dbsession.flush()
b) When a Task is Ready for Review
Event:
TaskReadyForReviewEvent
Handler:
handle_task_ready_for_review(event)
Purpose:
Sends emails to all project managers informing a task is ready for review.
Creates
Notification
records for all project managers about the task status update.
Notification Data (for each project manager):
user_id
- project manager's user idproject_id
- related project idmessage
- "The task {task.task_title} has been moved to Under Review in project {project.name}"time_sent
- current timestamp
pythonCopyEditfor manager in project_managers:
notif = Notification(
user_id=manager.id,
project_id=project.id,
message=f"The task {task.task_title} has been moved to Under Review in project {project.name}",
time_sent=datetime.now()
)
request.dbsession.add(notif)
request.dbsession.flush()
Summary
Notification Model Usage:
Used to store messages related to project and task events.
Each notification stores the user it belongs to, the associated project, a message string, and the timestamp.
Notification Creation Points:
When a user is added to a project.
When a task status changes to "under review" and notifications are sent to project managers.
Notification Retrieval:
Through a dedicated API endpoint, filtered by user and ordered by most recent.
To add more notification events, first you need to add an event, define its variables, and add an HTML to be added in the email. Also save it to the database so it can be shown inside the application.
Last updated