33from django .contrib .auth import get_user_model , authenticate , update_session_auth_hash
44import graphene
55from graphene_django .types import DjangoObjectType
6+ from graphql import GraphQLError
67from graphql_jwt .shortcuts import create_refresh_token , get_token
78import graphql_jwt
89from photonix .photos .models import Library , LibraryPath , LibraryUser
1213
1314
1415class UserType (DjangoObjectType ):
15- """Docstring for UserType."""
16-
1716 class Meta :
1817 model = User
1918
2019
2120class CreateUser (graphene .Mutation ):
22- """Docstring for CreateUser."""
23-
2421 class Arguments :
25- """Docstring for Arguments."""
26-
2722 username = graphene .String (required = True )
2823 password = graphene .String (required = True )
2924 password1 = graphene .String (required = True )
@@ -34,13 +29,12 @@ class Arguments:
3429
3530 @staticmethod
3631 def mutate (self , info , username , password , password1 ):
37- """Mutate method."""
3832 if User .objects .filter (username = username ).exists ():
39- raise Exception ( " Username already exists!" )
33+ raise GraphQLError ( ' Username already exists!' )
4034 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!' )
4236 elif password != password1 :
43- raise Exception ( " Password fields do not match!" )
37+ raise GraphQLError ( ' Password fields do not match!' )
4438 else :
4539 user = User (username = username )
4640 user .set_password (password1 )
@@ -53,6 +47,7 @@ def mutate(self, info, username, password, password1):
5347
5448class Environment (graphene .ObjectType ):
5549 demo = graphene .Boolean ()
50+ sample_data = graphene .Boolean ()
5651 first_run = graphene .Boolean ()
5752 form = graphene .String ()
5853 user_id = graphene .ID ()
@@ -61,11 +56,11 @@ class Environment(graphene.ObjectType):
6156
6257
6358class AfterSignup (graphene .ObjectType ):
64- """Pass token for login, after signup."""
65-
59+ '''Pass token for login, after signup.'''
6660 token = graphene .String ()
6761 refresh_token = graphene .String ()
6862
63+
6964class Query (graphene .ObjectType ):
7065 profile = graphene .Field (UserType )
7166 environment = graphene .Field (Environment )
@@ -74,66 +69,72 @@ class Query(graphene.ObjectType):
7469 def resolve_profile (self , info ):
7570 user = info .context .user
7671 if user .is_anonymous :
77- raise Exception ('Not logged in' )
72+ raise GraphQLError ('Not logged in' )
7873 return user
7974
8075 def resolve_environment (self , info ):
8176 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+
8280 if user and user .has_config_persional_info and \
8381 user .has_created_library and user .has_configured_importing and \
8482 user .has_configured_image_analysis :
85- # raise Exception(info.context.user.is_anonymous)
8683 return {
87- 'demo' : os .environ .get ('DEMO' , False ),
84+ 'demo' : demo ,
85+ 'sample_data' : sample_data ,
8886 'first_run' : False ,
8987 }
9088 else :
91- if not user :
89+ if not user or not user . is_authenticated :
9290 return {
93- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
91+ 'demo' : demo ,
92+ 'sample_data' : sample_data ,
93+ 'first_run' : True ,
9494 'form' : 'has_config_persional_info' }
9595 if not user .has_created_library :
9696 return {
97- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
97+ 'demo' : demo ,
98+ 'sample_data' : sample_data ,
99+ 'first_run' : True ,
98100 'form' : 'has_created_library' , 'user_id' : user .id }
99101 if not user .has_configured_importing :
100102 return {
101- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
103+ 'demo' : demo ,
104+ 'sample_data' : sample_data ,
105+ 'first_run' : True ,
102106 'form' : 'has_configured_importing' , 'user_id' : user .id ,
103107 'library_id' : Library .objects .filter (users__user = user )[0 ].id ,
104108 'library_path_id' : LibraryPath .objects .filter (library__users__user = user )[0 ].id
105109 }
106110 if not user .has_configured_image_analysis :
107111 return {
108- 'demo' : os .environ .get ('DEMO' , False ), 'first_run' : True ,
112+ 'demo' : demo ,
113+ 'sample_data' : sample_data ,
114+ 'first_run' : True ,
109115 'form' : 'has_configured_image_analysis' , 'user_id' : user .id ,
110116 'library_id' : Library .objects .filter (users__user = user )[0 ].id ,
111117 }
112118
113119 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.'''
115121 user = info .context .user
116- if user .has_configured_image_analysis :
122+ if user .is_authenticated and user . has_configured_image_analysis :
117123 return {'token' : get_token (user ), 'refresh_token' : create_refresh_token (user )}
118124 return {'token' : None , 'refresh_token' : None }
119125
120126
121127class ChangePassword (graphene .Mutation ):
122- """docstring for ChangePassword."""
123-
124128 class Arguments :
125- """docstring for Arguments."""
126-
127129 old_password = graphene .String (required = True )
128130 new_password = graphene .String (required = True )
129131
130132 ok = graphene .Boolean ()
131133
132134 @staticmethod
133135 def mutate (self , info , old_password , new_password ):
134- """Mutate method for change password."""
135136 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!' )
137138 if authenticate (username = info .context .user .username , password = old_password ):
138139 info .context .user .set_password (new_password )
139140 info .context .user .save ()
@@ -143,8 +144,6 @@ def mutate(self, info, old_password, new_password):
143144
144145
145146class Mutation (graphene .ObjectType ):
146- """To create objects for all mutaions."""
147-
148147 token_auth = graphql_jwt .ObtainJSONWebToken .Field ()
149148 verify_token = graphql_jwt .Verify .Field ()
150149 refresh_token = graphql_jwt .Refresh .Field ()
0 commit comments