Yam Mesicka
Yam Mesicka

Reputation: 6601

Change state and redirect

I have a notification list at the top of my website.

  1. Each notification can be either "read" or "unread."
  2. Each notification is a clickable anchor element, taking the user to the post related to this notification.

Currently I use GET request to handle the user's click:

@webapp.route('/read')
@webapp.route('/read/<int:notification_id>')
@login_required
def read_notification(notification_id=None):
    if notification_id is None:
        return notifications.read(user=current_user)

    fetched_notifications = notifications.get(current_user)
    if notification is None:
        return fail(404, 'Invalid notification ID.')
    if notification.user.id != current_user.id:
        return fail(403, "You aren't allowed to access this page.")

    notifications.read(id_=notification_id)
    return redirect(notification.action_url or '/exercises')

I have a bad feeling about using a GET request to /read in order to change the state of the notification. On the other hand, I don't want to use POST (or PATCH), 'cause it will require me to use <form> (semantically wrong) or JavaScript (may create a laggy user experience).

Is there any better option?

The stack is vanilla JavaScript and Flask (Python 3.8).

Upvotes: 0

Views: 145

Answers (1)

Patch
Patch

Reputation: 754

You can actually redirect with the POST method by using code=307. For more information, you can read this

return redirect(notification.action_url or '/exercises', code=307)

Upvotes: 1

Related Questions