schema.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. or_filter_params = filter_params.pop("or",{})
  93. and_filter_params= filter_params.pop("and",{})
  94. and_filter_params |= filter_params
  95. if len(or_filter_params):
  96. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
  97. if len(and_filter_params):
  98. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
  99. query_set = query_set.order_by(order_by)[skip:skip+limit]
  100. return query_set
  101. def resolve_OrderFindOne(self,info,query = "[{}]"):
  102. additional_params = {}
  103. query_list = json.loads(query)
  104. filter_params = query_list[0]
  105. if len(query_list) > 1:
  106. additional_params = query_list[1]
  107. skip = additional_params.get("skip",0)
  108. limit = additional_params.get("limit",20)
  109. order_by = additional_params.get("orderBy","_id")
  110. user = info.context.user
  111. if user.is_superuser:
  112. query_set = Order.objects.all()
  113. else:
  114. query_set = Order.objects.filter(owner = user)
  115. if len(filter_params):
  116. or_filter_params = filter_params.pop("or",{})
  117. and_filter_params= filter_params.pop("and",{})
  118. and_filter_params |= filter_params
  119. if len(or_filter_params):
  120. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
  121. if len(and_filter_params):
  122. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
  123. return query_set.first()
  124. def resolve_OrderGoodFind(self,info,query = "[{}]"):
  125. additional_params = {}
  126. query_list = json.loads(query)
  127. filter_params = query_list[0]
  128. if len(query_list) > 1:
  129. additional_params = query_list[1]
  130. skip = additional_params.get("skip",0)
  131. limit = additional_params.get("limit",20)
  132. order_by = additional_params.get("orderBy","_id")
  133. query_set = OrderGood.objects.all()
  134. if len(filter_params):
  135. or_filter_params = filter_params.pop("or",{})
  136. and_filter_params= filter_params.pop("and",{})
  137. and_filter_params |= filter_params
  138. if len(or_filter_params):
  139. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
  140. if len(and_filter_params):
  141. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
  142. return query_set
  143. def resolve_OrderGoodFindOne(self,info,query = "[{}]"):
  144. additional_params = {}
  145. query_list = json.loads(query)
  146. filter_params = query_list[0]
  147. if len(query_list) > 1:
  148. additional_params = query_list[1]
  149. skip = additional_params.get("skip",0)
  150. limit = additional_params.get("limit",20)
  151. order_by = additional_params.get("orderBy","_id")
  152. query_set = OrderGood.objects.all()
  153. if len(filter_params):
  154. or_filter_params = filter_params.pop("or",{})
  155. and_filter_params= filter_params.pop("and",{})
  156. and_filter_params |= filter_params
  157. if len(or_filter_params):
  158. query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
  159. if len(and_filter_params):
  160. query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
  161. return query_set.first()
  162. class OrderUpsert(graphene.Mutation):
  163. class Arguments:
  164. order = OrderInput(required=True)
  165. Output =OrderType
  166. @staticmethod
  167. def mutate(root,info,order):
  168. user = info.context.user
  169. if not user.is_authenticated:
  170. raise Exception("Authentication credentials were not provided")
  171. orderPrice = 0
  172. new_order={}
  173. owner = None
  174. orderGoods_list = []
  175. if "orderGoods" in order:
  176. orderGoods_list = order.pop("orderGoods",[])
  177. if "owner" in order:
  178. try:
  179. owner = User.objects.get(pk =order.pop("owner")["_id"])
  180. except:
  181. raise Exception("Не вірні дані")
  182. try:
  183. if "_id" in order:
  184. new_order = Order.objects.get(_id = order._id)
  185. if not user.is_superuser:
  186. raise Exception("Authentication credentials were not provided")
  187. new_order.__dict__.update(**order)
  188. if owner:
  189. new_order.owner = owner
  190. else:
  191. new_order = Order(**order)
  192. if owner:
  193. if not user.is_superuser:
  194. raise Exception("Authentication credentials were not provided")
  195. new_order.owner = owner
  196. else:
  197. new_order.owner = user
  198. except Exception as e:
  199. raise Exception("Не вірні дані")
  200. if new_order:
  201. new_order.save()
  202. for orderGood in new_order.orderGoods.all():
  203. if new_order.status != 4:
  204. orderGood.good.amount += orderGood.count
  205. orderGood.good.save()
  206. new_order.orderGoods.clear()
  207. for orderGood in orderGoods_list:
  208. try:
  209. good = Good.objects.get(_id = orderGood.get("good")["_id"])
  210. except:
  211. raise Exception("Товар не знайдено")
  212. count = orderGood.get("count")
  213. if int(good.amount) - int(count) >= 0:
  214. price = int(count) * int(good.price)
  215. orderGoodToSave = OrderGood.objects.create(price = price, count = count, good = good)
  216. orderGoodToSave.save()
  217. new_order.orderGoods.add(orderGoodToSave)
  218. orderPrice+=int(price)
  219. if int(new_order.status) != 4:
  220. good.amount = int(good.amount) - int(count)
  221. good.save()
  222. else:
  223. raise Exception(f"{good.name}:Недостатньо товару!")
  224. new_order.price = orderPrice
  225. if new_order:
  226. new_order.save()
  227. else:
  228. raise Exception("Error during order save")
  229. order_data = model_to_dict(new_order)
  230. order_data["_id"] = new_order._id
  231. order_data["orderGoods"] = new_order.orderGoods.all()
  232. return OrderType(**order_data)
  233. class OrderDelete(graphene.Mutation):
  234. class Arguments:
  235. order = OrderInput(required=True)
  236. Output =OrderType
  237. @staticmethod
  238. def mutate(root,info,order):
  239. user = info.context.user
  240. if not user.is_superuser:
  241. raise Exception("Authentication credentials were not provided")
  242. try:
  243. _id = order._id
  244. order_to_delete = Order.objects.get(_id=_id)
  245. order_data = model_to_dict(order_to_delete)
  246. order_data["_id"] = order_to_delete._id
  247. order_data["orderGoods"] = order_to_delete.orderGoods.all()
  248. order_to_delete.delete()
  249. except:
  250. raise Exception("Не вірні дані")
  251. return OrderType(**order_data)
  252. class Mutations(graphene.ObjectType):
  253. OrderUpsert = OrderUpsert.Field()
  254. OrderDelete = OrderDelete.Field()