Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_user_state hook #33

Closed
1 task done
Archmonger opened this issue Jan 3, 2022 · 0 comments · Fixed by #190
Closed
1 task done

use_user_state hook #33

Archmonger opened this issue Jan 3, 2022 · 0 comments · Fixed by #190

Comments

@Archmonger
Copy link
Contributor

Archmonger commented Jan 3, 2022

Old Behavior

State is generated on a per-render basis and has no method of being permanently stored.

New Behavior

Allow for persistent state that is databased backed, linked to a User.

Implementation Details

Persistently store/restore state within a database model, linked to a User as a foreign key.

Store context data within a BinaryField.

This will require only serializable data will be allowed within this. Consider using dill.pickle to serialize data more robustly than standard library.

The state should only be fetched from the database once, upon first initialization of the hook's state value. On every set_state call, the value should be synchronized to the database.

We may also want to implement an expires_at parameter to decide when the value gets evicted from the database.

Example Database Model

class IdomUserState(models.Model):
    # One state is stored per user
    # When the user is deleted, the state is also deleted from the database
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    state = models.BinaryField(blank=True, null=True)

The interface may look like this...

# Note: The default state is only used in situations where there is no value in the database
# The websocket is needed in order to grab the user from the `scope`
state, set_state = hooks.use_user_state({"foo":"bar"})
state = {"something":"else"}

# This saves the values to the database
set_state(state)

In the background, the set state is doing the following...

def set_state(state, websocket):
    query = IdomUserState.objects.get_or_create(user=websocket.scope.user)
    query.state = dill.pickle(state)
    query.save()
    ...

Code of Conduct

@Archmonger Archmonger self-assigned this Jan 3, 2022
@Archmonger Archmonger changed the title useUserContext Hook useUserState Hook Jan 10, 2022
@Archmonger Archmonger changed the title useUserState Hook use_user_state Hook Jan 10, 2022
@Archmonger Archmonger removed their assignment Feb 6, 2022
@Archmonger Archmonger changed the title use_user_state Hook use_user_state hook Jan 16, 2023
@Archmonger Archmonger mentioned this issue Sep 16, 2023
1 task
@Archmonger Archmonger linked a pull request Sep 28, 2023 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant