-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschema.py
117 lines (96 loc) · 3.38 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
This defines the `Query` and `Muatation` for all GraphQL operations
for `account` app.
"""
from typing import Optional
from graphql.error import GraphQLError
import strawberry
import strawberry.django
from gqlauth.user import arg_mutations
from strawberry.file_uploads import Upload
from strawberry.types.info import Info
from strawberry_django.permissions import IsAuthenticated
from django.contrib.auth.models import User
from cloudinary.uploader import upload_image
from django.contrib.sites.shortcuts import get_current_site
from account.models import Profile
from .types import (
UserType_,
ProfileType,
ProfilePicUpdateType,
ProfileInput,
OwnsObjPerm,
)
@strawberry.type
class UserMutations:
token_auth = arg_mutations.ObtainJSONWebToken.field # login mutation
verify_token = arg_mutations.VerifyToken.field
refresh_token = arg_mutations.RefreshToken.field
revoke_token = arg_mutations.RevokeToken.field
register = arg_mutations.Register.field
verify_account = arg_mutations.VerifyAccount.field
send_password_reset_email = arg_mutations.SendPasswordResetEmail.field
password_reset = arg_mutations.PasswordReset.field
@strawberry.type
class Query:
"""
Main entry for all query type for `account` app.
"""
@strawberry.field
def me(self, info) -> Optional[UserType_]:
user = info.context.request.user
if user.is_anonymous:
return None
return user
@strawberry.type
class Mutation(UserMutations):
"""
Main entry for all `Mutation` types for `account` app.
It inherits from `AuthMutation`.
"""
profile_update: ProfileType = strawberry.django.mutations.update(
ProfileInput,
extensions=[
OwnsObjPerm(),
IsAuthenticated(),
],
)
@strawberry.mutation(extensions=[IsAuthenticated()])
def become_premium(self, info: Info) -> bool:
profile = info.context.request.user.profile
if profile.premium:
return True
if Profile.objects.filter(premium=True).count() <= 10:
profile.premium = True
profile.save()
return True
raise GraphQLError(
"Sorry we reached 10 premium users. But still be happy we won the Expo."
)
@strawberry.django.mutation(extensions=[IsAuthenticated()])
def profile_pic_update(self, info, file: Upload) -> Optional[ProfilePicUpdateType]:
"""
Mutation to help upload only a profile pic to Cloudinary
then save it to Profile model.
The pic will be save in the current logged in user's profile.
"""
user: User = info.context.request.user
try:
# to prvent colliding with dev & prod
ext = get_current_site(info.context.request).domain
res = upload_image(
file,
folder=f"profile_pics/{ext}",
public_id=user.username,
overwrite=True,
invalidate=True,
transformation=[{"width": 200, "height": 200, "crop": "fill"}],
format="jpg",
)
user.profile.profile_pic = res
user.profile.save()
except Exception:
return ProfilePicUpdateType(success=False, profile_pic="")
return ProfilePicUpdateType(
success=True, profile_pic=str(user.profile.profile_pic)
)