From f6abbbe7e27ed1495aef3207ce2019c4ec929e39 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 26 Sep 2023 00:37:07 -0700 Subject: [PATCH] fix some bugs --- src/reactpy_django/hooks.py | 14 +++++------ .../0007_alter_userdatamodel_user.py | 24 +++++++++++++++++++ .../0008_alter_userdatamodel_user.py | 22 +++++++++++++++++ .../0009_alter_userdatamodel_data.py | 17 +++++++++++++ src/reactpy_django/models.py | 4 ++-- src/reactpy_django/types.py | 8 +++---- 6 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 src/reactpy_django/migrations/0007_alter_userdatamodel_user.py create mode 100644 src/reactpy_django/migrations/0008_alter_userdatamodel_user.py create mode 100644 src/reactpy_django/migrations/0009_alter_userdatamodel_data.py diff --git a/src/reactpy_django/hooks.py b/src/reactpy_django/hooks.py index 3dcb2955..58547ca5 100644 --- a/src/reactpy_django/hooks.py +++ b/src/reactpy_django/hooks.py @@ -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), @@ -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)}") diff --git a/src/reactpy_django/migrations/0007_alter_userdatamodel_user.py b/src/reactpy_django/migrations/0007_alter_userdatamodel_user.py new file mode 100644 index 00000000..7eaea2a3 --- /dev/null +++ b/src/reactpy_django/migrations/0007_alter_userdatamodel_user.py @@ -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, + ), + ), + ] diff --git a/src/reactpy_django/migrations/0008_alter_userdatamodel_user.py b/src/reactpy_django/migrations/0008_alter_userdatamodel_user.py new file mode 100644 index 00000000..ee31c2fe --- /dev/null +++ b/src/reactpy_django/migrations/0008_alter_userdatamodel_user.py @@ -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 + ), + ), + ] diff --git a/src/reactpy_django/migrations/0009_alter_userdatamodel_data.py b/src/reactpy_django/migrations/0009_alter_userdatamodel_data.py new file mode 100644 index 00000000..47b731b6 --- /dev/null +++ b/src/reactpy_django/migrations/0009_alter_userdatamodel_data.py @@ -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), + ), + ] diff --git a/src/reactpy_django/models.py b/src/reactpy_django/models.py index 5a1f9cfd..20165c97 100644 --- a/src/reactpy_django/models.py +++ b/src/reactpy_django/models.py @@ -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 diff --git a/src/reactpy_django/types.py b/src/reactpy_django/types.py index 43a76988..0e223e98 100644 --- a/src/reactpy_django/types.py +++ b/src/reactpy_django/types.py @@ -7,6 +7,7 @@ Callable, Generic, MutableMapping, + NamedTuple, Protocol, Sequence, TypeVar, @@ -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]