def check_token(self, request): """ check token """ token = self.get_token(request) if token: if not is_token_expired(token): return ApiResponse({'is_login': True}, msg='有效 token!') return ApiResponse({'is_login': False}, msg='无效 token!')
def login(self, request): """ login """ data = request.data username = data.get('username') password = data.get('password') hash_key = data.get('hash_key') captcha = data.get('captcha') if captcha is None: raise ApiException('请填写验证码!') if username is None or password is None: raise ApiException('请填写用户名密码!') if not CaptchaStore.objects.filter( response=captcha, hashkey=hash_key, expiration__gte=datetime.now()).exists(): raise ApiException('无效验证码') user = authenticate(request=request, username=username, password=password) if not user: raise ApiException('无效用户或密码错误!') token, created = Token.objects.get_or_create(user=user) if not created: is_expired = is_token_expired(token) if is_expired: token.delete() token = Token.objects.create(user=user) return ApiResponse({"token": token.key}, msg='登陆成功!')
def get_paginated_response(self, data): return ApiResponse( links={ "first": "1", "last": self.max_page_number, "prev": self.get_previous_link(), "next": self.get_next_link() }, meta={ "current_page": self.current_page_number, "from": (int(self.current_page_number) - 1) * self.page_size + 1, "last_page": self.max_page_number, "path": self.uri, "per_page": self.page_size, "to": min( int(self.current_page_number) * self.page_size, self.max_page_number), "total": self.count }, code=200, data=data)
def app_api_exception_handler(exc, context): """ supplier app api 异常处理 """ if isinstance(exc, Http404): exc = exceptions.NotFound() elif isinstance(exc, PermissionDenied): exc = exceptions.PermissionDenied() if isinstance(exc, exceptions.AuthenticationFailed): # shopify 登陆 return Response({ 'errorno': 2, 'message': "Please login.", 'redirect_url': '/login' }) if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait if isinstance(exc.detail, (list, dict)): data = exc.detail else: data = {'message': exc.detail} set_rollback() return Response(data, status=exc.status_code, headers=headers) if isinstance(exc, ApiException): # 处理抛出的业务错误 detail = exc.detail return ApiResponse(errno=1, msg=detail) if isinstance(exc, Exception): # 处理异常 if DEBUG is False: content = traceback.format_exc() notify_exception_message.delay(content) return ApiResponse(errno=2, msg='Server Error!') else: raise exc return None
def logout(self, request): token = self.get_token(request) if token: token.delete() return ApiResponse({}, msg='退出登陆成功!')
def captcha(self, request): hash_key = CaptchaStore.generate_key() captcha_url = captcha_image_url(hash_key) return ApiResponse({'captcha': captcha_url, 'hash_key': hash_key})
def hello(self, request): return ApiResponse({'speak': 'hello'})
def get_paginated_response(self, data): return ApiResponse(data=data, total=self.count)