123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- import graphene
- from graphene_django.types import DjangoObjectType
- from graphql_jwt.decorators import login_required
- from .models import Order,OrderGood
- from goods.models import Good
- import json
- from authAPI.schema import UserInput
- from functools import reduce
- from django.forms.models import model_to_dict
- from django.contrib.auth import get_user_model
- import operator
- from django.db.models import Q
- User = get_user_model()
- class OrderGoodType(graphene.ObjectType):
- _id = graphene.String(name='_id')
- good = graphene.Field('goods.schema.GoodType')
- price = graphene.Int()
- count = graphene.Int()
- order = graphene.Field(lambda:OrderType)
- createdAt = graphene.String()
- def resolve__id(self,info):
- return self._id
- def resolve_good(self,info):
- return self.good
- def resolve_price(self,info):
- return self.price
- def resolve_count(self,info):
- return self.count
- def resolve_order(self,info):
- return self.order
- def resolve_createdAt(self,info):
- return self.createdAt.strftime('%s')
- class OrderGoodInput(graphene.InputObjectType):
- _id = graphene.String(name='_id')
- good = graphene.Field("goods.schema.GoodInput")
- count = graphene.Int()
- class OrderType(graphene.ObjectType):
- _id = graphene.String(name='_id')
- createdAt = graphene.String()
- status = graphene.Int()
- price = graphene.Int()
- orderGoods = graphene.List(OrderGoodType)
- delivery = graphene.String()
- owner = graphene.Field('authAPI.schema.UserType')
- def resolve__id(self,info):
- return self._id
- def resolve_status(self,info):
- return self.status
- def resolve_price(self,info):
- return self.price
- def resolve_delivery(self,info):
- return self.delivery
- def resolve_orderGoods(self,info):
- try:
- iter(self.orderGoods)
- return self.orderGoods
- except:
- return self.orderGoods.all()
- def resolve_owner(self,info):
- return self.owner
- def resolve_createdAt(self,info):
- return self.createdAt.strftime('%s')
- class OrderInput(graphene.InputObjectType):
- _id = graphene.String(name='_id')
- status = graphene.Int()
- price = graphene.Int()
- delivery = graphene.String()
- owner = graphene.Field(UserInput)
- orderGoods = graphene.List(OrderGoodInput)
- class Query(graphene.ObjectType):
- OrderFind = graphene.List(OrderType,query = graphene.String())
- OrderFindOne = graphene.Field(OrderType,query = graphene.String())
- OrderGoodFind = graphene.List(OrderGoodType,query = graphene.String())
- OrderGoodFindOne = graphene.Field(OrderGoodType,query = graphene.String())
- def resolve_OrderFind(self,info,query = "[{}]"):
- additional_params = {}
- query_list = json.loads(query)
- filter_params = query_list[0]
-
- if "status" in filter_params and int(filter_params.get("status")) == 0:
- filter_params.pop("status")
-
- if len(query_list) > 1:
- additional_params = query_list[1]
- skip = int(additional_params.get("skip",0))
- limit = int(additional_params.get("limit",20))
- order_by = additional_params.get("orderBy","_id")
- user = info.context.user
- if user.is_superuser:
- query_set = Order.objects.all()
- else:
- query_set = Order.objects.filter(owner = user)
- if len(filter_params):
- or_filter_params = filter_params.pop("or",{})
- and_filter_params= filter_params.pop("and",{})
- and_filter_params |= filter_params
- if len(or_filter_params):
- query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
- if len(and_filter_params):
- query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
-
- query_set = query_set.order_by(order_by)[skip:skip+limit]
- return query_set
- def resolve_OrderFindOne(self,info,query = "[{}]"):
- additional_params = {}
- query_list = json.loads(query)
- filter_params = query_list[0]
- if len(query_list) > 1:
- additional_params = query_list[1]
- skip = additional_params.get("skip",0)
- limit = additional_params.get("limit",20)
- order_by = additional_params.get("orderBy","_id")
- user = info.context.user
- if user.is_superuser:
- query_set = Order.objects.all()
- else:
- query_set = Order.objects.filter(owner = user)
- if len(filter_params):
- or_filter_params = filter_params.pop("or",{})
- and_filter_params= filter_params.pop("and",{})
- and_filter_params |= filter_params
- if len(or_filter_params):
- query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
- if len(and_filter_params):
- query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
-
- return query_set.first()
- def resolve_OrderGoodFind(self,info,query = "[{}]"):
- additional_params = {}
- query_list = json.loads(query)
- filter_params = query_list[0]
- if len(query_list) > 1:
- additional_params = query_list[1]
- skip = additional_params.get("skip",0)
- limit = additional_params.get("limit",20)
- order_by = additional_params.get("orderBy","_id")
- query_set = OrderGood.objects.all()
- if len(filter_params):
- or_filter_params = filter_params.pop("or",{})
- and_filter_params= filter_params.pop("and",{})
- and_filter_params |= filter_params
- if len(or_filter_params):
- query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
- if len(and_filter_params):
- query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
-
- return query_set
- def resolve_OrderGoodFindOne(self,info,query = "[{}]"):
- additional_params = {}
- query_list = json.loads(query)
- filter_params = query_list[0]
- if len(query_list) > 1:
- additional_params = query_list[1]
- skip = additional_params.get("skip",0)
- limit = additional_params.get("limit",20)
- order_by = additional_params.get("orderBy","_id")
- query_set = OrderGood.objects.all()
- if len(filter_params):
- or_filter_params = filter_params.pop("or",{})
- and_filter_params= filter_params.pop("and",{})
- and_filter_params |= filter_params
- if len(or_filter_params):
- query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in or_filter_params.items()])))
- if len(and_filter_params):
- query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in and_filter_params.items()])))
-
- return query_set.first()
- class OrderUpsert(graphene.Mutation):
- class Arguments:
- order = OrderInput(required=True)
- Output =OrderType
- @staticmethod
- def mutate(root,info,order):
- user = info.context.user
- if not user.is_authenticated:
- raise Exception("Authentication credentials were not provided")
- orderPrice = 0
- new_order={}
- owner = None
- orderGoods_list = []
- if "orderGoods" in order:
- orderGoods_list = order.pop("orderGoods",[])
-
- if "owner" in order:
- try:
- owner = User.objects.get(pk =order.pop("owner")["_id"])
- except:
- raise Exception("Не вірні дані")
-
- try:
- if "_id" in order:
- new_order = Order.objects.get(_id = order._id)
- if not user.is_superuser:
- raise Exception("Authentication credentials were not provided")
- new_order.__dict__.update(**order)
- if owner:
- new_order.owner = owner
- else:
- new_order = Order(**order)
- if owner:
- if not user.is_superuser:
- raise Exception("Authentication credentials were not provided")
- new_order.owner = owner
- else:
- new_order.owner = user
- except Exception as e:
- raise Exception("Не вірні дані")
- if new_order:
- new_order.save()
- for orderGood in new_order.orderGoods.all():
- if new_order.status != 4:
- orderGood.good.amount += orderGood.count
- orderGood.good.save()
- new_order.orderGoods.clear()
- for orderGood in orderGoods_list:
- try:
- good = Good.objects.get(_id = orderGood.get("good")["_id"])
- except:
- raise Exception("Товар не знайдено")
- count = orderGood.get("count")
- if int(good.amount) - int(count) >= 0:
- price = int(count) * int(good.price)
- orderGoodToSave = OrderGood.objects.create(price = price, count = count, good = good)
- orderGoodToSave.save()
- new_order.orderGoods.add(orderGoodToSave)
- orderPrice+=int(price)
- if int(new_order.status) != 4:
- good.amount = int(good.amount) - int(count)
- good.save()
- else:
- raise Exception(f"{good.name}:Недостатньо товару!")
- new_order.price = orderPrice
- if new_order:
- new_order.save()
- else:
- raise Exception("Error during order save")
- order_data = model_to_dict(new_order)
- order_data["_id"] = new_order._id
- order_data["orderGoods"] = new_order.orderGoods.all()
- return OrderType(**order_data)
- class OrderDelete(graphene.Mutation):
- class Arguments:
- order = OrderInput(required=True)
- Output =OrderType
- @staticmethod
- def mutate(root,info,order):
- user = info.context.user
- if not user.is_superuser:
- raise Exception("Authentication credentials were not provided")
- try:
- _id = order._id
- order_to_delete = Order.objects.get(_id=_id)
- order_data = model_to_dict(order_to_delete)
- order_data["_id"] = order_to_delete._id
- order_data["orderGoods"] = order_to_delete.orderGoods.all()
- order_to_delete.delete()
- except:
- raise Exception("Не вірні дані")
- return OrderType(**order_data)
- class Mutations(graphene.ObjectType):
- OrderUpsert = OrderUpsert.Field()
- OrderDelete = OrderDelete.Field()
|