123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- import graphene
- from graphene_django.types import DjangoObjectType
- from graphql_jwt.decorators import login_required
- from goods.schema import ImageType,ImageInput
- import json
- from functools import reduce
- from django.forms.models import model_to_dict
- from django.contrib.auth import get_user_model
- from goods.models import Image
- import operator
- from django.db.models import Q
- import graphene
- import graphql_jwt
- User = get_user_model()
- class UserType(graphene.ObjectType):
- _id = graphene.String(name='_id')
- avatar = graphene.Field(ImageType)
- name = graphene.String()
- nick = graphene.String()
- username = graphene.String()
- acl = graphene.List(graphene.String)
- is_active = graphene.Boolean(name = 'is_active')
- createdAt = graphene.String()
- def resolve__id(self,info):
- return self._id
- def resolve_avatar(self,info):
- return self.avatar
- def resolve_nick(self,info):
- return self.nick
- def resolve_name(self,info):
- return self.name
- def resolve_username(self,info):
- return self.username
- def resolve_is_active(self,info):
- return self.is_active
- def resolve_acl(self,info):
- user = User.objects.get(_id = self._id)
- acl = ["anon"]
- if user._id:
- if user.is_active:
- acl.append("active")
- if user.is_superuser:
- acl.append("admin")
- return acl
- def resolve_createdAt(self,info):
- return self.createdAt.strftime('%s')
- class UserInput(graphene.InputObjectType):
- _id = graphene.String(name='_id')
- avatar = graphene.Field(ImageInput)
- name = graphene.String()
- nick = graphene.String()
- acl = graphene.List(graphene.String)
- username = graphene.String()
- password = graphene.String()
- class Query(graphene.ObjectType):
- UserFind = graphene.List(UserType,query = graphene.String())
- UserFindOne = graphene.Field(UserType,query = graphene.String())
- def resolve_UserFind(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 = int(additional_params.get("skip",0))
- limit = int(additional_params.get("limit",20))
- order_by = additional_params.get("orderBy","_id")
- query_set = User.objects.all()
- if len(filter_params):
- query_set = query_set.filter(reduce(operator.or_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
- query_set = query_set.order_by(order_by)[skip:skip+limit]
- return query_set
- def resolve_UserFindOne(self,info,query = "[{}]"):
- user = info.context.user
- 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")
- if user.is_superuser:
- query_set = User.objects.all()
- else:
- query_set = User.objects.filter(pk=user._id)
- if len(filter_params):
- query_set = query_set.filter(reduce(operator.and_,(Q(**d) for d in [dict([i]) for i in filter_params.items()])))
- return query_set.first()
- class UserUpsert(graphene.Mutation):
- class Arguments:
- user = UserInput(required=True)
- Output =UserType
- @staticmethod
- def mutate(root,info,user):
- new_user={}
- password = None
- ava = None
- acl = []
- if "password" in user and len(user["password"]) < 3:
- raise Exception("Не вірні дані (пароль)")
- elif "password" in user:
- password = str(user.pop("password"))
- if "acl" in user:
- acl = user.get("acl", [])
- user.pop("acl")
- if "avatar" in user:
- if user.get("avatar") == "null":
- ava = user.pop("avatar")
- else:
- try:
- ava = Image.objects.get(_id = user.pop("avatar")["_id"])
- except:
- raise Exception("Не вірні дані (аватар)")
- try:
- _id = user._id
- new_user = User.objects.get(_id = _id)
- if not info.context.user.is_superuser:
- raise Exception("Authentication credentials were not provided")
- user.pop("_id",None)
- new_user.__dict__.update(**user)
- if password:
- new_user.set_password(password)
-
- except Exception as e:
- if info.context.user.is_authenticated:
- try:
- new_user = User.objects.get(username = info.context.user.username)
- new_user.__dict__.update(**user)
- if password:
- new_user.set_password(password)
- except:
- raise Exception("Не вірні дані")
- else:
- try:
- User.objects.get(username = user.username)
- raise Exception("Username вже зайнятий")
- except:
- pass
- new_user = User.objects.create_user(username = user.username,password=user.password)
- if ava:
- if ava == "null":
- new_user.avatar = None
- else:
- new_user.avatar = ava
- if len(acl):
- if not info.context.user.is_superuser:
- raise Exception("Authentication credentials were not provided")
-
- new_user.is_active = "active" in acl
- new_user.is_superuser = "admin" in acl
- new_user.save()
- user_data = {key: new_user.__dict__[key] for key in new_user.__dict__.keys() & {"username","_id","name","avatar","nick"}}
- user_data["_id"] = new_user._id
- return UserType(**user_data)
- # class GoodDelete(graphene.Mutation):
- # class Arguments:
- # good = GoodInput(required=True)
- # Output =GoodType
- # @staticmethod
- # def mutate(root,info,good):
- # user = info.context.user
- # if not user.is_superuser:
- # raise Exception("Authentication credentials were not provided")
- # try:
- # _id = good._id
- # good_to_delete = Good.objects.get(_id=_id)
- # good_data = model_to_dict(good_to_delete)
- # good_data["_id"] = new_good._id
- # good_to_delete.delete()
- # except:
- # raise Exception("Не вірні дані")
- # return GoodType(**good_data)
- class Mutations(graphene.ObjectType):
- token_auth = graphql_jwt.ObtainJSONWebToken.Field()
- verify_token = graphql_jwt.Verify.Field()
- refresh_token = graphql_jwt.Refresh.Field()
- UserUpsert = UserUpsert.Field()
- # GoodDelete = GoodDelete.Field()
|