def inspect(self, request, *args, **kwargs): token = request.auth access_token = encode(token, settings.PRIVATE_KEYS['auth'], algorithm='RS256') if request.user.role == User.CLIENT: landing = { 'id': request.user.channels.filter(type=Channel.ROUTINE).first().id } else: landing = { 'id': request.user.channels.filter(type=Channel.PUBLIC).first().id } return Response({ 'access_token': access_token, 'expires_at': token['expires_at'], 'landing': landing, 'user': UserSerializer(request.user, context=self.get_serializer_context()).data, })
def create(self, validated_data): token = { 'expires_at': (timezone.now() + timedelta(minutes=30)).isoformat().replace( '+00:00', 'Z'), 'ipv4': self.context['request'].ipv4, 'user': { 'id': validated_data['user'].id, 'role': validated_data['user'].role, } } access_token = encode(token, settings.PRIVATE_KEYS['auth'], algorithm='RS256') if validated_data['user'].role == User.CLIENT: landing = { 'id': validated_data['user'].channels.filter( type=Channel.ROUTINE).first().id } else: landing = { 'id': validated_data['user'].channels.filter( type=Channel.PUBLIC).first().id } return { 'access_token': access_token, 'expires_at': token['expires_at'], 'landing': landing, 'user': UserSerializer(validated_data['user']).data, }
def setting(self, request, *args, **kwargs): context = self.get_serializer_context() serializer = UserSerializer(request.user, data=request.data, context=context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def create(self, validated_data): validated_data['channel'] = self.context['channel'] validated_data['created_by'] = self.context['request'].user if validated_data.get('event', {}).get('type', None) == 'invoice': payer_id = validated_data['data'].get('payer', None) if not payer_id: raise exceptions.ValidationError({ 'payer': ['This field is required.'] }) if not User.objects.filter(id=payer_id).exists(): raise exceptions.NotFound(detail='User not found.') payer = User.objects.get(id=payer_id) validated_data['data']['payer'] = UserSerializer(payer, context=self.context).data validated_data['data']['amount'] = sum([item.get('amount', 0) for item in validated_data.get('data', {}).get('items', [])]) return super(DialogueSerializer, self).create(validated_data)
def _pre_create(self, *args, **kwargs): kwargs['designer'] = UserSerializer(kwargs['designed_by']).data derivatives = {} return args, kwargs, derivatives
def create(self, validated_data): res = requests.get('http://{domain}/account/getTokenByTicket'.format( domain=settings.OAUTH_API_DOMAIN), params={'ticket': validated_data['ticket']}) if not res.ok: raise exceptions.AuthenticationFailed( detail='Oauth server cannot recognize the ticket.') oauth_access_token = res.json().get('data', None) if oauth_access_token is None: raise exceptions.AuthenticationFailed( detail='Oauth server response is invalid.') res = requests.get('http://{domain}/account/getUserInfoByToken'.format( domain=settings.OAUTH_API_DOMAIN), params={'token': oauth_access_token}) if not res.ok: raise exceptions.AuthenticationFailed( detail='Oauth server cannot recognize the token.') oauth_user_data = res.json().get('data', {}) try: oauth_user_id = str(oauth_user_data.get('userId')) except: raise exceptions.AuthenticationFailed( detail='Oauth server respond with invalid user id.') params = {'oauth_id': oauth_user_id} user_count = User.objects.filter(**params).count() if user_count == 1: user = User.objects.get(**params) elif user_count == 0: params['is_oauth_enabled'] = True params['password'] = os.urandom(20).encode('hex') params['role'] = User.CLIENT avatar = oauth_user_data.get('avatarUrl', None) if isinstance(avatar, (str, unicode)): params['avatar'] = avatar email = oauth_user_data.get('email', None) if isinstance(avatar, (str, unicode)): params['email'] = email tel = oauth_user_data.get('mobile', None) if isinstance(tel, (str, unicode)): params['tel'] = tel realname = oauth_user_data.get('realName', None) nickname = oauth_user_data.get('nickName', None) if realname is not None and len(realname) > 0: params['first_name'] = realname elif nickname is not None and len(nickname) > 0: params['first_name'] = nickname else: params['first_name'] = tel[-4:] user = User.objects.create(**params) else: raise exceptions.AuthenticationFailed( detail='Duplicate oauth user_id found in database.') token = { 'expires_at': (timezone.now() + timedelta(minutes=30)).isoformat().replace( '+00:00', 'Z'), 'ipv4': self.context['request'].ipv4, 'user': { 'id': user.id, 'role': user.role, } } access_token = encode(token, settings.PRIVATE_KEYS['auth'], algorithm='RS256') if user.role == User.CLIENT: landing = { 'id': user.channels.filter(type=Channel.ROUTINE).first().id } else: landing = { 'id': user.channels.filter(type=Channel.PUBLIC).first().id } return { 'access_token': access_token, 'expires_at': token['expires_at'], 'landing': landing, 'user': UserSerializer(user).data, }