schema.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import graphene
  2. from graphene_django.types import DjangoObjectType
  3. from graphql_jwt.decorators import login_required
  4. from goods.schema import ImageType,ImageInput
  5. import json
  6. from functools import reduce
  7. from django.forms.models import model_to_dict
  8. from django.contrib.auth import get_user_model
  9. from goods.models import Image
  10. import operator
  11. from django.db.models import Q
  12. import graphene
  13. import graphql_jwt
  14. User = get_user_model()
  15. class UserType(graphene.ObjectType):
  16. _id = graphene.String(name='_id')
  17. avatar = graphene.Field(ImageType)
  18. name = graphene.String()
  19. nick = graphene.String()
  20. username = graphene.String()
  21. acl = graphene.List(graphene.String)
  22. is_active = graphene.Boolean(name = 'is_active')
  23. createdAt = graphene.String()
  24. def resolve__id(self,info):
  25. return self._id
  26. def resolve_avatar(self,info):
  27. return self.avatar
  28. def resolve_nick(self,info):
  29. return self.nick
  30. def resolve_name(self,info):
  31. return self.name
  32. def resolve_username(self,info):
  33. return self.username
  34. def resolve_is_active(self,info):
  35. return self.is_active
  36. def resolve_acl(self,info):
  37. user = User.objects.get(_id = self._id)
  38. acl = ["anon"]
  39. if user._id:
  40. acl.append("user")
  41. if user.is_superuser:
  42. acl.append("admin")
  43. return acl
  44. def resolve_createdAt(self,info):
  45. return self.createdAt.strftime('%s')
  46. class UserInput(graphene.InputObjectType):
  47. _id = graphene.String(name='_id')
  48. avatar = graphene.Field(ImageInput)
  49. name = graphene.String()
  50. nick = graphene.String()
  51. acl = graphene.List(graphene.String)
  52. username = graphene.String()
  53. password = graphene.String()
  54. class Query(graphene.ObjectType):
  55. UserFind = graphene.List(UserType,query = graphene.String())
  56. UserFindOne = graphene.Field(UserType,query = graphene.String())
  57. def resolve_UserFind(self,info,query = "[{}]"):
  58. additional_params = {}
  59. query_list = json.loads(query)
  60. filter_params = query_list[0]
  61. if len(query_list) > 1:
  62. additional_params = query_list[1]
  63. skip = int(additional_params.get("skip",0))
  64. limit = int(additional_params.get("limit",20))
  65. order_by = additional_params.get("orderBy","_id")
  66. query_set = User.objects.all()
  67. if len(filter_params):
  68. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  69. query_set = query_set.order_by(order_by)[skip:skip+limit]
  70. return query_set
  71. def resolve_UserFindOne(self,info,query = "[{}]"):
  72. user = info.context.user
  73. additional_params = {}
  74. query_list = json.loads(query)
  75. filter_params = query_list[0]
  76. if len(query_list) > 1:
  77. additional_params = query_list[1]
  78. skip = additional_params.get("skip",0)
  79. limit = additional_params.get("limit",20)
  80. order_by = additional_params.get("orderBy","_id")
  81. if user.is_superuser:
  82. query_set = User.objects.all()
  83. else:
  84. query_set = User.objects.filter(pk=user._id)
  85. if len(filter_params):
  86. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  87. return query_set.first()
  88. class UserUpsert(graphene.Mutation):
  89. class Arguments:
  90. user = UserInput(required=True)
  91. Output =UserType
  92. @staticmethod
  93. def mutate(root,info,user):
  94. new_user={}
  95. ava = None
  96. if "avatar" in user:
  97. if user.get("avatar") == "null":
  98. ava = user.pop("avatar")
  99. else:
  100. try:
  101. ava = Image.objects.get(_id = user.pop("avatar")["_id"])
  102. except:
  103. raise Exception("Не вірні дані (аватар)")
  104. try:
  105. _id = user._id
  106. new_user = User.objects.get(_id = _id)
  107. if not info.context.user.is_superuser:
  108. raise Exception("Authentication credentials were not provided")
  109. user.pop("_id",None)
  110. new_user.__dict__.update(**user)
  111. except Exception as e:
  112. if info.context.user.is_authenticated:
  113. try:
  114. new_user = User.objects.get(username = info.context.user.username)
  115. new_user.__dict__.update(**user)
  116. except:
  117. raise Exception("Не вірні дані")
  118. else:
  119. try:
  120. User.objects.get(username = user.username)
  121. raise Exception("Username вже зайнятий")
  122. except:
  123. pass
  124. new_user = User.objects.create_user(username = user.username,password=user.password)
  125. if ava:
  126. if ava == "null":
  127. new_user.avatar = None
  128. else:
  129. new_user.avatar = ava
  130. new_user.save()
  131. user_data = {key: new_user.__dict__[key] for key in new_user.__dict__.keys() & {"username","_id","name","avatar","nick"}}
  132. user_data["_id"] = new_user._id
  133. return UserType(**user_data)
  134. # class GoodDelete(graphene.Mutation):
  135. # class Arguments:
  136. # good = GoodInput(required=True)
  137. # Output =GoodType
  138. # @staticmethod
  139. # def mutate(root,info,good):
  140. # user = info.context.user
  141. # if not user.is_superuser:
  142. # raise Exception("Authentication credentials were not provided")
  143. # try:
  144. # _id = good._id
  145. # good_to_delete = Good.objects.get(_id=_id)
  146. # good_data = model_to_dict(good_to_delete)
  147. # good_data["_id"] = new_good._id
  148. # good_to_delete.delete()
  149. # except:
  150. # raise Exception("Не вірні дані")
  151. # return GoodType(**good_data)
  152. class Mutations(graphene.ObjectType):
  153. token_auth = graphql_jwt.ObtainJSONWebToken.Field()
  154. verify_token = graphql_jwt.Verify.Field()
  155. refresh_token = graphql_jwt.Refresh.Field()
  156. UserUpsert = UserUpsert.Field()
  157. # GoodDelete = GoodDelete.Field()