def get_instance(cls, user, address_id): """ fetching the profile instance on profile id """ try: return Address.objects.select_related('user').get(user=user, id=address_id) except Address.DoesNotExist: raise ResourceNotFoundException(BAD_REQUEST)
def adminlist(self, request): """ Return user profile data and groups """ try: page = self.paginate_queryset(self.get_queryset()) serialiser = self.get_serializer(page,many=True) return self.get_paginated_response(serialiser.data) except Exception as e: raise ResourceNotFoundException(BAD_REQUEST)
def admin(self, request): """ Return user profile data and groups """ input_id = request.user.id try: d = UserService.get_instance(input_id) data = self.get_serializer(d).data return Response(data, status.HTTP_200_OK) except Exception as e: raise ResourceNotFoundException(BAD_REQUEST)
def my_profile(self, request): """ Return user profile data and groups """ try: # print(request.user.email) d = ProfileSevice.get_instance(request.user.id) data = self.get_serializer(d).data return Response(data, status.HTTP_200_OK) except Exception as e: print(str(e)) raise ResourceNotFoundException(BAD_REQUEST)
def updaterate(self, request, format='json'): try: data = request.data instance = self.model.objects.get(id=data["id"]) serializer = self.get_serializer(instance, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: raise ParseException(BAD_REQUEST, serializer.errors) except self.model.DoesNotExist: raise ResourceNotFoundException(BAD_REQUEST) except: raise ParseException(BAD_REQUEST)
def parse_data_from_response(self, response, method): if method in ('HEAD', 'head'): return response if is_success(response.status_code): if response.status_code == requests.codes.no_content: return {} if self.do_log: logger.info("Success Response: {}".format(response.text)) response_type = response.headers.get('Content-Type') if 'text' in response_type: return response.text return response.json() logger.error('Failure Response: {}'.format(response.text)) logger_message = { 'url': response.url, 'response': response.text } # map response to corresponding exception, add exceptions here as required if response.status_code == requests.codes.bad_request: raise BadRequestException(logger_message) if response.status_code == requests.codes.not_found: raise ResourceNotFoundException(logger_message) if response.status_code == requests.codes.conflict: raise ResourceConflictException(logger_message) if is_client_error(response.status_code): # 4XX except above raise ParseException(logger_message) # for everything else raise NetworkException(logger_message)