Skip to content

Commit

Permalink
Remove strawberry-django-plus, update strawberry. (#403)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
nrbnlulu and pre-commit-ci[bot] committed Jul 10, 2023
1 parent 64a27a5 commit 2e6561a
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 492 deletions.
11 changes: 11 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Release type: minor

This release updates to the latest version of strawberry
and removes dependencies with strawberry-django-plus.

- The directive `IsVerified` is migrated to the new field
extensions API and extends the django base permission
extension.

- The Channels middleware will inject the user in `request.user`
or `request.scope["UserOrError"]`
20 changes: 15 additions & 5 deletions gqlauth/core/directives.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import dataclasses
from typing import Any, final
from typing import Any, Callable, final

import strawberry
from strawberry.schema_directive import Location
from strawberry_django_plus.permissions import ConditionDirective
from strawberry.types import Info
from strawberry_django.permissions import DjangoNoPermission, DjangoPermissionExtension

from gqlauth.core.utils import UserProto

Expand All @@ -13,10 +14,19 @@
description="Checks whether a user is verified",
)
@final
class IsVerified(ConditionDirective):
class IsVerified(DjangoPermissionExtension):
"""Mark a field as only resolvable by authenticated users."""

message: strawberry.Private[str] = dataclasses.field(default="User is not authenticated.")

def check_condition(self, root: Any, info, user: UserProto, **kwargs) -> bool: # type: ignore
return user.is_authenticated and user.status.verified
def resolve_for_user(
self,
resolver: Callable,
user: UserProto,
*,
info: Info,
source: Any,
):
if user.is_authenticated and user.status.verified:
return resolver()
raise DjangoNoPermission
8 changes: 5 additions & 3 deletions gqlauth/core/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ def subscribe(self, *args, **kwargs):
def _inject_user_and_errors(kwargs: dict) -> UserOrError:
context = kwargs.get("context_value")
# channels compat
if ws := getattr(context, "ws", None):
user_or_error: UserOrError = ws.scope[USER_OR_ERROR_KEY]
if isinstance(context, dict):
request = context["request"]
user_or_error: UserOrError = request.scope[USER_OR_ERROR_KEY]
request.user = user_or_error.user # type: ignore
else:
user_or_error: UserOrError = getattr(context.request, USER_OR_ERROR_KEY) # type: ignore
context.request.user = user_or_error.user # type: ignore
context.request.user = user_or_error.user # type: ignore
return user_or_error
6 changes: 3 additions & 3 deletions gqlauth/core/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abc import ABC
from typing import Type

import strawberry_django
from django.contrib.auth import get_user_model
from strawberry.types import Info
from strawberry_django_plus import gql

from gqlauth.core.utils import hide_args_kwargs, inject_arguments
from gqlauth.user.resolvers import BaseMixin
Expand All @@ -16,7 +16,7 @@ def __init_subclass__(cls: Type[BaseMixin], **kwargs):
input_type = cls.resolve_mutation.__annotations__["input_"]
return_type = cls.resolve_mutation.__annotations__["return"]

@gql.django.field(description=cls.__doc__)
@strawberry_django.field(description=cls.__doc__)
@inject_arguments(input_type.__annotations__)
@hide_args_kwargs
def field(info: Info, **kwargs) -> return_type: # type: ignore
Expand All @@ -31,7 +31,7 @@ def __init_subclass__(cls: Type[BaseMixin], **kwargs):
input_type = cls.resolve_mutation.__annotations__["input_"]
return_type = cls.resolve_mutation.__annotations__["return"]

@gql.django.field(description=cls.__doc__)
@strawberry_django.field(description=cls.__doc__)
@hide_args_kwargs
def field(info: Info, input: input_type) -> return_type: # type: ignore
cls.verification_check(info)
Expand Down
5 changes: 4 additions & 1 deletion gqlauth/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def camelize(data):


def get_user(info: Info) -> USER_UNION:
return info.context.request.user # type: ignore
try:
return info.context.request.user # type: ignore
except AttributeError:
return info.context["request"].user


def cast_to_status_user(user: USER_UNION) -> UserProto:
Expand Down
8 changes: 4 additions & 4 deletions gqlauth/user/queries.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Optional

import strawberry
import strawberry_django
from django.contrib.auth import get_user_model
from strawberry.schema_directive import Location
from strawberry.types import Info
from strawberry_django_plus import gql

from gqlauth.core.types_ import GQLAuthError, GQLAuthErrors
from gqlauth.core.utils import get_user
Expand All @@ -20,16 +20,16 @@ class Sample:
message: str = "fdsafdsafdsfa"


@gql.django.type(model=USER_MODEL, filters=UserFilter)
@strawberry_django.type(model=USER_MODEL, filters=UserFilter)
class UserQueries:
@gql.django.field(description="Returns the current user if he is not anonymous.")
@strawberry_django.field(description="Returns the current user if he is not anonymous.")
def public_user(self, info: Info) -> Optional[UserType]:
user = get_user(info)
if not user.is_anonymous:
return user # type: ignore
return None

@gql.django.field()
@strawberry_django.field()
def me(self, info: Info) -> UserType:
user = get_user(info)
if not user.is_authenticated:
Expand Down
4 changes: 2 additions & 2 deletions gqlauth/user/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from uuid import UUID

import strawberry
import strawberry_django
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
from django.core.exceptions import ObjectDoesNotExist
from django.core.signing import BadSignature, SignatureExpired
from django.db import transaction
from strawberry.field import StrawberryField
from strawberry.types import Info
from strawberry_django_plus import gql

from gqlauth.core.constants import Messages, TokenAction
from gqlauth.core.exceptions import (
Expand Down Expand Up @@ -88,7 +88,7 @@ class Captcha: # type: ignore[no-redef]
**The captcha will be invoked when the timeout expires**.
"""

@gql.django.field(description=__doc__)
@strawberry_django.field(description=__doc__)
def field(self) -> CaptchaType:
return CaptchaModel.create_captcha()

Expand Down
Loading

0 comments on commit 2e6561a

Please sign in to comment.