3
3
from django .contrib .auth import get_user_model , authenticate , update_session_auth_hash
4
4
import graphene
5
5
from graphene_django .types import DjangoObjectType
6
+ from graphql import GraphQLError
6
7
from graphql_jwt .shortcuts import create_refresh_token , get_token
7
8
import graphql_jwt
8
9
from photonix .photos .models import Library , LibraryPath , LibraryUser
12
13
13
14
14
15
class UserType (DjangoObjectType ):
15
- """Docstring for UserType."""
16
-
17
16
class Meta :
18
17
model = User
19
18
20
19
21
20
class CreateUser (graphene .Mutation ):
22
- """Docstring for CreateUser."""
23
-
24
21
class Arguments :
25
- """Docstring for Arguments."""
26
-
27
22
username = graphene .String (required = True )
28
23
password = graphene .String (required = True )
29
24
password1 = graphene .String (required = True )
@@ -34,13 +29,12 @@ class Arguments:
34
29
35
30
@staticmethod
36
31
def mutate (self , info , username , password , password1 ):
37
- """Mutate method."""
38
32
if User .objects .filter (username = username ).exists ():
39
- raise Exception ( " Username already exists!" )
33
+ raise GraphQLError ( ' Username already exists!' )
40
34
elif len (password ) < 8 and len (password1 ) < 8 :
41
- raise Exception ( " Password must be at least 8 characters long!" )
35
+ raise GraphQLError ( ' Password must be at least 8 characters long!' )
42
36
elif password != password1 :
43
- raise Exception ( " Password fields do not match!" )
37
+ raise GraphQLError ( ' Password fields do not match!' )
44
38
else :
45
39
user = User (username = username )
46
40
user .set_password (password1 )
@@ -53,6 +47,7 @@ def mutate(self, info, username, password, password1):
53
47
54
48
class Environment (graphene .ObjectType ):
55
49
demo = graphene .Boolean ()
50
+ sample_data = graphene .Boolean ()
56
51
first_run = graphene .Boolean ()
57
52
form = graphene .String ()
58
53
user_id = graphene .ID ()
@@ -61,11 +56,11 @@ class Environment(graphene.ObjectType):
61
56
62
57
63
58
class AfterSignup (graphene .ObjectType ):
64
- """Pass token for login, after signup."""
65
-
59
+ '''Pass token for login, after signup.'''
66
60
token = graphene .String ()
67
61
refresh_token = graphene .String ()
68
62
63
+
69
64
class Query (graphene .ObjectType ):
70
65
profile = graphene .Field (UserType )
71
66
environment = graphene .Field (Environment )
@@ -74,66 +69,72 @@ class Query(graphene.ObjectType):
74
69
def resolve_profile (self , info ):
75
70
user = info .context .user
76
71
if user .is_anonymous :
77
- raise Exception ('Not logged in' )
72
+ raise GraphQLError ('Not logged in' )
78
73
return user
79
74
80
75
def resolve_environment (self , info ):
81
76
user = User .objects .first ()
77
+ demo = os .environ .get ('DEMO' , False )
78
+ sample_data = os .environ .get ('DEMO' , False ) or os .environ .get ('SAMPLE_DATA' , False )
79
+
82
80
if user and user .has_config_persional_info and \
83
81
user .has_created_library and user .has_configured_importing and \
84
82
user .has_configured_image_analysis :
85
- # raise Exception(info.context.user.is_anonymous)
86
83
return {
87
- 'demo' : os .environ .get ('DEMO' , False ),
84
+ 'demo' : demo ,
85
+ 'sample_data' : sample_data ,
88
86
'first_run' : False ,
89
87
}
90
88
else :
91
- if not user :
89
+ if not user or not user . is_authenticated :
92
90
return {
93
- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
91
+ 'demo' : demo ,
92
+ 'sample_data' : sample_data ,
93
+ 'first_run' : True ,
94
94
'form' : 'has_config_persional_info' }
95
95
if not user .has_created_library :
96
96
return {
97
- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
97
+ 'demo' : demo ,
98
+ 'sample_data' : sample_data ,
99
+ 'first_run' : True ,
98
100
'form' : 'has_created_library' , 'user_id' : user .id }
99
101
if not user .has_configured_importing :
100
102
return {
101
- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
103
+ 'demo' : demo ,
104
+ 'sample_data' : sample_data ,
105
+ 'first_run' : True ,
102
106
'form' : 'has_configured_importing' , 'user_id' : user .id ,
103
107
'library_id' : Library .objects .filter (users__user = user )[0 ].id ,
104
108
'library_path_id' : LibraryPath .objects .filter (library__users__user = user )[0 ].id
105
109
}
106
110
if not user .has_configured_image_analysis :
107
111
return {
108
- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
112
+ 'demo' : demo ,
113
+ 'sample_data' : sample_data ,
114
+ 'first_run' : True ,
109
115
'form' : 'has_configured_image_analysis' , 'user_id' : user .id ,
110
116
'library_id' : Library .objects .filter (users__user = user )[0 ].id ,
111
117
}
112
118
113
119
def resolve_after_signup (self , info ):
114
- """ To login user from frontend after finish sigunp process."""
120
+ ''' To login user from frontend after finish sigunp process.'''
115
121
user = info .context .user
116
- if user .has_configured_image_analysis :
122
+ if user .is_authenticated and user . has_configured_image_analysis :
117
123
return {'token' : get_token (user ), 'refresh_token' : create_refresh_token (user )}
118
124
return {'token' : None , 'refresh_token' : None }
119
125
120
126
121
127
class ChangePassword (graphene .Mutation ):
122
- """docstring for ChangePassword."""
123
-
124
128
class Arguments :
125
- """docstring for Arguments."""
126
-
127
129
old_password = graphene .String (required = True )
128
130
new_password = graphene .String (required = True )
129
131
130
132
ok = graphene .Boolean ()
131
133
132
134
@staticmethod
133
135
def mutate (self , info , old_password , new_password ):
134
- """Mutate method for change password."""
135
136
if os .environ .get ('DEMO' , False ) and os .environ .get ('ENV' ) != 'test' :
136
- raise Exception ( " Password cannot be changed in demo mode!" )
137
+ raise GraphQLError ( ' Password cannot be changed in demo mode!' )
137
138
if authenticate (username = info .context .user .username , password = old_password ):
138
139
info .context .user .set_password (new_password )
139
140
info .context .user .save ()
@@ -143,8 +144,6 @@ def mutate(self, info, old_password, new_password):
143
144
144
145
145
146
class Mutation (graphene .ObjectType ):
146
- """To create objects for all mutaions."""
147
-
148
147
token_auth = graphql_jwt .ObtainJSONWebToken .Field ()
149
148
verify_token = graphql_jwt .Verify .Field ()
150
149
refresh_token = graphql_jwt .Refresh .Field ()
0 commit comments