def login(request, response: Response, data: dict): if not bool(request.session.get('user')): if 'username' in data and 'password' in data: username = data['username'] password = data['password'] if User.objects.filter(username=username).exists(): user = User.objects.get(username=username) if user.check_password(password): request.session['user'] = { 'username': user.username, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, 'login_at': str(datetime.datetime.now()), } if not request.session.session_key: request.session.create() response.set_cookie('sessionid', request.session.session_key) response.data = {'login': '******', 'user': { 'username': user.username, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, }, } else: response.data = {'login': '******'} else: response.data = {'login': '******'} return response
def custom_exception_handler(exc, context): # Call REST framework's default exception handler first, # to get the standard error response. response = exception_handler(exc, context) if isinstance(exc, SQLDecodeError) and response is None: response = Response( { 'message': 'duplication error' }, status=status.HTTP_400_BAD_REQUEST ) elif response is not None: data = response.data response.data = {} errors = [] try: for field, value in data.items(): errors.append("{} : {}".format(field, " ".join(value))) response.data['errors'] = errors response.data['status'] = False response.data['exception'] = str(exc) except: response = exception_handler(exc, context) return response return response
def custom_exception_handler(exc, context): response = exception_handler(exc, context) # custom exception processing if response is None: response = Response({"detail": str(exc)}) if isinstance(exc, exceptions.APIException): if isinstance(exc, exceptions.ValidationError): detail = format_ValidationError(exc) response.data = {"detail": detail, "code": "ValidationError"} else: response.data = { "detail": str(exc), "code": exc.detail.code, } else: exception_logger.exception(exc) response.status_code = int(status.HTTP_500_INTERNAL_SERVER_ERROR) return response
def custom_exception_handler(exc, context): response = exception_handler(exc, context) if isinstance(exc, ConcurrentUpdate): tmp_response = Response(status=status.HTTP_400_BAD_REQUEST) tmp_response.data = {} tmp_response.data[ 'Error'] = 'The current record was modified by others since last retrieval' return tmp_response elif isinstance(exc, MissingLastModificationDate): tmp_response = Response(status=status.HTTP_400_BAD_REQUEST) tmp_response.data = {} tmp_response.data[ 'Error'] = 'Current record has no modification time recorded' return tmp_response elif isinstance(exc, UserBeingUpdated): tmp_response = Response(status=status.HTTP_400_BAD_REQUEST) tmp_response.data = {} tmp_response.data[ 'Error'] = 'Current user is being updated by another request' return tmp_response return response
def get(self, request, format=None): query = request.GET.get('q') q_type = request.GET.get('type', 'all') data = {"result": []} if query: if q_type != 'all': res = self.perform_search(query, q_type) else: res = self.perform_search( query, 'users') + self.perform_search(query, 'cases') data['result'] = res resp = Response() resp.data = json.dumps(data) resp.status = 200 return resp
def logout(request, response: Response, data: dict): if not request.session.is_empty(): request.session.delete('user') response.delete_cookie('sessionid') response.data = {'login': '******'} return response