def post(self, request, *args, **kwargs): populate_user_info_request(request, False, False) request.data['sender'] = request.user.id if is_company(request.user): request.data['sender_type'] = 'Company' company_obj = Company.objects.filter( user_id=request.user.id).first() request.data['sender_company'] = company_obj.name else: request.data['sender_type'] = 'Professional' pro_obj = Professional.objects.filter( user_id=request.user.id).first() request.data['sender_pro'] = pro_obj.id user_obj = User.objects.filter(id=request.data['receiver']).first() if is_company(user_obj): request.data['receiver_type'] = 'Company' company_obj = Company.objects.filter(user_id=user_obj.id).first() request.data['receiver_company'] = company_obj.name else: request.data['receiver_type'] = 'Professional' pro_obj = Professional.objects.filter(user_id=user_obj.id).first() request.data['receiver_pro'] = pro_obj.id return super(MessageCreate, self).post(request, *args, **kwargs)
def authenticate(self, request): result = super(CompanyAuthentication, self).authenticate(request) if result is not None: user = result[0] if is_company(user): return result return None
def company_signin(request): email = request.data['email'] password = request.data['password'] try: user = User.objects.get(email=email) if not user.is_active: raise AuthenticationFailed(INACTIVE_COMPANY) elif not check_password(password, user.password): raise AuthenticationFailed(INCORRECT_PASSWORD_COMPANY) elif not is_company(user): raise AuthenticationFailed() except User.DoesNotExist: raise AuthenticationFailed(NO_SUCH_COMPANY) if 'device_id' in request.data and request.data['device_id']: RefreshToken.lifetime = DEVICE_REFRESH_TOKEN_LIFETIME AccessToken.lifetime = DEVICE_ACCESS_TOKEN_LIFETIME else: RefreshToken.lifetime = WEB_REFRESH_TOKEN_LIFETIME AccessToken.lifetime = WEB_ACCESS_TOKEN_LIFETIME token = RefreshToken.for_user(user) data = {} data['username'] = user.username data['access'] = str(token.access_token) data['refresh'] = str(token) company = Company.objects.get(user_id=user.id) data['user'] = {'id': user.id, 'email': email, 'type': 'company'} data['company'] = CompanySerializer(company, many=False).data data['token_lifetime'] = SIMPLE_JWT return Response(data)
def post(self, request): payload = { 'access_token': request.data.get("token") } # validate the token r = requests.get('https://www.googleapis.com/oauth2/v2/userinfo', params=payload) data = json.loads(r.text) if 'error' in data: content = { 'message': 'wrong google token / this google token is already expired.' } return Response(content) # create user if not exist try: user = User.objects.get(email=data['email']) except User.DoesNotExist: raise AuthenticationFailed() if not is_company(user): raise AuthenticationFailed() token = RefreshToken.for_user( user) # generate token without username & password data = {} data['username'] = user.username data['access'] = str(token.access_token) data['refresh'] = str(token) company = Company.objects.get(user_id=user.id) data['user'] = {'id': user.id, 'email': user.email, 'type': 'company'} data['company'] = CompanySerializer(company, many=False).data data['token_lifetime'] = SIMPLE_JWT return Response(data)
def has_permission(self, request, view): return bool(request.user and request.user.is_authenticated and is_company(request.user))