schema.py 5.1 KB

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