Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Sep 26, 2023
1 parent b0bdbfc commit f6abbbe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/reactpy_django/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ def use_user_data(
async def _set_user_data(data):
if not isinstance(data, dict):
raise TypeError(f"Expected dict while setting user data, got {type(data)}")
if user.is_anonymous:
raise ValueError("AnonymousUser cannot have user data.")

model, _ = await UserDataModel.objects.aget_or_create(user=user)
model.data = pickle.dumps(data)
model.save()
await model.asave()

data: Query[dict] = use_query(
QueryOptions(postprocessor=None),
Expand Down Expand Up @@ -363,13 +365,11 @@ def _use_mutation_args_2(mutation, refetch=None):
async def _get_user_data(user: AbstractUser, defaults: None | dict) -> dict:
from reactpy_django.models import UserDataModel

if user is None:
raise ValueError("No user is available.")
if not user or user.is_anonymous:
raise UserNotFoundError("No user is available, cannot fetch user data.")

model, _ = await UserDataModel.objects.aget_or_create(
user=user, data=pickle.dumps({})
)
data = pickle.loads(model.data)
model, _ = await UserDataModel.objects.aget_or_create(user=user)
data = pickle.loads(model.data) if model.data else {}

if not isinstance(data, dict):
raise TypeError(f"Expected dict while loading user data, got {type(data)}")
Expand Down
24 changes: 24 additions & 0 deletions src/reactpy_django/migrations/0007_alter_userdatamodel_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.5 on 2023-09-26 07:03

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("reactpy_django", "0006_userdatamodel"),
]

operations = [
migrations.AlterField(
model_name="userdatamodel",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
unique=True,
),
),
]
22 changes: 22 additions & 0 deletions src/reactpy_django/migrations/0008_alter_userdatamodel_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.5 on 2023-09-26 07:04

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("reactpy_django", "0007_alter_userdatamodel_user"),
]

operations = [
migrations.AlterField(
model_name="userdatamodel",
name="user",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
]
17 changes: 17 additions & 0 deletions src/reactpy_django/migrations/0009_alter_userdatamodel_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.5 on 2023-09-26 07:33

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("reactpy_django", "0008_alter_userdatamodel_user"),
]

operations = [
migrations.AlterField(
model_name="userdatamodel",
name="data",
field=models.BinaryField(blank=True, null=True),
),
]
4 changes: 2 additions & 2 deletions src/reactpy_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def load(cls):
class UserDataModel(models.Model):
"""A model for storing `user_state` data."""

user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) # type: ignore
data = models.BinaryField(blank=True) # type: ignore
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) # type: ignore
data = models.BinaryField(null=True, blank=True) # type: ignore
8 changes: 4 additions & 4 deletions src/reactpy_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Callable,
Generic,
MutableMapping,
NamedTuple,
Protocol,
Sequence,
TypeVar,
Expand Down Expand Up @@ -101,7 +102,6 @@ class ComponentParams:
kwargs: MutableMapping[str, Any]


@dataclass
class UserData(Generic[Inferred]):
data: Query[Inferred]
set_data: Mutation[Inferred]
class UserData(NamedTuple):
data: Query[dict]
set_data: Mutation[dict]

0 comments on commit f6abbbe

Please sign in to comment.