Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

use_user_state hook #33

Closed
@Archmonger

Description

@Archmonger

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 aUser.

Implementation Details

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

Store context data within aBinaryField.

This will require only serializable data will be allowed within this. Consider usingdill.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'sstate value. On everyset_state call, the value should be synchronized to the database.

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

Example Database Model

classIdomUserState(models.Model):# One state is stored per user# When the user is deleted, the state is also deleted from the databaseuser=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 databaseset_state(state)

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

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

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp