schema.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import graphene
  2. from graphene_django.types import DjangoObjectType
  3. from graphql_jwt.decorators import login_required
  4. from .models import Order,OrderGood
  5. from goods.models import Good
  6. import json
  7. from authAPI.schema import UserInput
  8. from functools import reduce
  9. from django.forms.models import model_to_dict
  10. from django.contrib.auth import get_user_model
  11. import operator
  12. from django.db.models import Q
  13. User = get_user_model()
  14. class OrderGoodType(graphene.ObjectType):
  15. _id = graphene.String(name='_id')
  16. good = graphene.Field('goods.schema.GoodType')
  17. price = graphene.Int()
  18. count = graphene.Int()
  19. order = graphene.Field(lambda:OrderType)
  20. createdAt = graphene.String()
  21. def resolve__id(self,info):
  22. return self._id
  23. def resolve_good(self,info):
  24. return self.good
  25. def resolve_price(self,info):
  26. return self.price
  27. def resolve_count(self,info):
  28. return self.count
  29. def resolve_order(self,info):
  30. return self.order
  31. def resolve_createdAt(self,info):
  32. return self.createdAt.strftime('%s')
  33. class OrderGoodInput(graphene.InputObjectType):
  34. _id = graphene.String(name='_id')
  35. good = graphene.Field("goods.schema.GoodInput")
  36. count = graphene.Int()
  37. class OrderType(graphene.ObjectType):
  38. _id = graphene.String(name='_id')
  39. createdAt = graphene.String()
  40. status = graphene.Int()
  41. price = graphene.Int()
  42. orderGoods = graphene.List(OrderGoodType)
  43. delivery = graphene.String()
  44. owner = graphene.Field('authAPI.schema.UserType')
  45. def resolve__id(self,info):
  46. return self._id
  47. def resolve_status(self,info):
  48. return self.status
  49. def resolve_price(self,info):
  50. return self.price
  51. def resolve_delivery(self,info):
  52. return self.delivery
  53. def resolve_orderGoods(self,info):
  54. try:
  55. iter(self.orderGoods)
  56. return self.orderGoods
  57. except:
  58. return self.orderGoods.all()
  59. def resolve_owner(self,info):
  60. return self.owner
  61. def resolve_createdAt(self,info):
  62. return self.createdAt.strftime('%s')
  63. class OrderInput(graphene.InputObjectType):
  64. _id = graphene.String(name='_id')
  65. status = graphene.Int()
  66. price = graphene.Int()
  67. delivery = graphene.String()
  68. owner = graphene.Field(UserInput)
  69. orderGoods = graphene.List(OrderGoodInput)
  70. class Query(graphene.ObjectType):
  71. OrderFind = graphene.List(OrderType,query = graphene.String())
  72. OrderFindOne = graphene.Field(OrderType,query = graphene.String())
  73. OrderGoodFind = graphene.List(OrderGoodType,query = graphene.String())
  74. OrderGoodFindOne = graphene.Field(OrderGoodType,query = graphene.String())
  75. def resolve_OrderFind(self,info,query = "[{}]"):
  76. additional_params = {}
  77. query_list = json.loads(query)
  78. filter_params = query_list[0]
  79. if "status" in filter_params and int(filter_params.get("status")) == 0:
  80. filter_params.pop("status")
  81. if len(query_list) > 1:
  82. additional_params = query_list[1]
  83. skip = int(additional_params.get("skip",0))
  84. limit = int(additional_params.get("limit",20))
  85. order_by = additional_params.get("orderBy","_id")
  86. user = info.context.user
  87. if user.is_superuser:
  88. query_set = Order.objects.all()
  89. else:
  90. query_set = Order.objects.filter(owner = user)
  91. if len(filter_params):
  92. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  93. query_set = query_set.order_by(order_by)[skip:skip+limit]
  94. return query_set
  95. def resolve_OrderFindOne(self,info,query = "[{}]"):
  96. additional_params = {}
  97. query_list = json.loads(query)
  98. filter_params = query_list[0]
  99. if len(query_list) > 1:
  100. additional_params = query_list[1]
  101. skip = additional_params.get("skip",0)
  102. limit = additional_params.get("limit",20)
  103. order_by = additional_params.get("orderBy","_id")
  104. user = info.context.user
  105. if user.is_superuser:
  106. query_set = Order.objects.all()
  107. else:
  108. query_set = Order.objects.filter(owner = user)
  109. if len(filter_params):
  110. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  111. return query_set.first()
  112. def resolve_OrderGoodFind(self,info,query = "[{}]"):
  113. additional_params = {}
  114. query_list = json.loads(query)
  115. filter_params = query_list[0]
  116. if len(query_list) > 1:
  117. additional_params = query_list[1]
  118. skip = additional_params.get("skip",0)
  119. limit = additional_params.get("limit",20)
  120. order_by = additional_params.get("orderBy","_id")
  121. query_set = OrderGood.objects.all()
  122. if len(filter_params):
  123. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  124. return query_set
  125. def resolve_OrderGoodFindOne(self,info,query = "[{}]"):
  126. additional_params = {}
  127. query_list = json.loads(query)
  128. filter_params = query_list[0]
  129. if len(query_list) > 1:
  130. additional_params = query_list[1]
  131. skip = additional_params.get("skip",0)
  132. limit = additional_params.get("limit",20)
  133. order_by = additional_params.get("orderBy","_id")
  134. query_set = OrderGood.objects.all()
  135. if len(filter_params):
  136. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
  137. return query_set.first()
  138. class OrderUpsert(graphene.Mutation):
  139. class Arguments:
  140. order = OrderInput(required=True)
  141. Output =OrderType
  142. @staticmethod
  143. def mutate(root,info,order):
  144. user = info.context.user
  145. if not user.is_authenticated:
  146. raise Exception("Authentication credentials were not provided")
  147. orderPrice = 0
  148. new_order={}
  149. owner = None
  150. orderGoods_list = []
  151. if "orderGoods" in order:
  152. orderGoods_list = order.pop("orderGoods",[])
  153. if "owner" in order:
  154. try:
  155. owner = User.objects.get(pk =order.pop("owner")["_id"])
  156. except:
  157. raise Exception("Не вірні дані")
  158. try:
  159. if "_id" in order:
  160. new_order = Order.objects.get(_id = order._id)
  161. if not user.is_superuser:
  162. raise Exception("Authentication credentials were not provided")
  163. new_order.__dict__.update(**order)
  164. if owner:
  165. new_order.owner = owner
  166. else:
  167. new_order = Order(**order)
  168. new_order.owner = user
  169. except Exception as e:
  170. raise Exception("Не вірні дані")
  171. if new_order:
  172. new_order.save()
  173. for orderGood in new_order.orderGoods.all():
  174. if new_order.status != 4:
  175. orderGood.good.amount += orderGood.count
  176. orderGood.good.save()
  177. new_order.orderGoods.clear()
  178. for orderGood in orderGoods_list:
  179. try:
  180. good = Good.objects.get(_id = orderGood.get("good")["_id"])
  181. except:
  182. raise Exception("Товар не знайдено")
  183. count = orderGood.get("count")
  184. if int(good.amount) - int(count) >= 0:
  185. price = int(count) * int(good.price)
  186. orderGoodToSave = OrderGood.objects.create(price = price, count = count, good = good)
  187. orderGoodToSave.save()
  188. new_order.orderGoods.add(orderGoodToSave)
  189. orderPrice+=int(price)
  190. if int(new_order.status) != 4:
  191. good.amount = int(good.amount) - int(count)
  192. good.save()
  193. else:
  194. raise Exception(f"{good.name}:Недостатньо товару!")
  195. new_order.price = orderPrice
  196. if new_order:
  197. new_order.save()
  198. else:
  199. raise Exception("Error during order save")
  200. order_data = model_to_dict(new_order)
  201. order_data["_id"] = new_order._id
  202. order_data["orderGoods"] = new_order.orderGoods.all()
  203. return OrderType(**order_data)
  204. class OrderDelete(graphene.Mutation):
  205. class Arguments:
  206. order = OrderInput(required=True)
  207. Output =OrderType
  208. @staticmethod
  209. def mutate(root,info,order):
  210. user = info.context.user
  211. if not user.is_superuser:
  212. raise Exception("Authentication credentials were not provided")
  213. try:
  214. _id = order._id
  215. order_to_delete = Order.objects.get(_id=_id)
  216. order_data = model_to_dict(order_to_delete)
  217. order_data["_id"] = order_to_delete._id
  218. order_data["orderGoods"] = order_to_delete.orderGoods.all()
  219. order_to_delete.delete()
  220. except:
  221. raise Exception("Не вірні дані")
  222. return OrderType(**order_data)
  223. class Mutations(graphene.ObjectType):
  224. OrderUpsert = OrderUpsert.Field()
  225. OrderDelete = OrderDelete.Field()