Пример #1
0
 def put(self, request, pk):
     """
     Function is used for modify comment info
     :param request: request header with required info.
     :return: comment info or send proper error status
     """
     try:
         comment = Comment.objects.get(id=pk)
     except Wall.DoesNotExist as e:
         logger.exception(e)
         api_response = ApiResponse(
             status=0,
             message=constants.COMMENT_DOES_NOT_EXIST,
             http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
     serializer = CommentSerializer(comment,
                                    data=request.data,
                                    partial=True,
                                    context={'request': request})
     if serializer.is_valid():
         serializer.save()
         api_response = ApiResponse(
             status=1,
             data=serializer.data,
             message=constants.UPDATE_COMMENT_SUCCESS,
             http_status=status.HTTP_201_CREATED)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=get_error_message(serializer),
                                http_status=status.HTTP_400_BAD_REQUEST)
     return api_response.create_response()
Пример #2
0
 def put(self, request):
     """
     Function is used for modify the password
     :param request: request header with required info.
     :return: user info or proper error message
     """
     user = request.user
     if user:
         data = {"password": request.data['password']}
         serializer = UserSerializer(user, data=data, partial=True)
         if serializer.is_valid():
             serializer.save()
             api_response = ApiResponse(
                 status=1,
                 data=serializer.data,
                 message=constants.CHANGE_PASSWORD_SUCCESS,
                 http_status=status.HTTP_201_CREATED)
             return api_response.create_response()
         api_response = ApiResponse(status=0,
                                    message=get_error_message(serializer),
                                    http_status=status.HTTP_400_BAD_REQUEST)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=constants.USER_DOES_NOT_EXIST,
                                http_status=status.HTTP_404_NOT_FOUND)
     return api_response.create_response()
Пример #3
0
 def put(self, request, pk):
     """
     Function is used for modify user info
     :param request: request header with required info.
     :param pk: primary key of a object.
     :return: user info or send proper error status
     """
     try:
         user = User.objects.get(id=pk)
     except User.DoesNotExist as e:
         logger.exception(e)
         api_response = ApiResponse(status=0,
                                    message=constants.USER_DOES_NOT_EXIST,
                                    http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
     serializer = UserSerializer(user, data=request.data, partial=True)
     if serializer.is_valid():
         serializer.save()
         api_response = ApiResponse(status=1,
                                    data=serializer.data,
                                    message=constants.UPDATE_USER_SUCCESS,
                                    http_status=status.HTTP_201_CREATED)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=get_error_message(serializer),
                                http_status=status.HTTP_400_BAD_REQUEST)
     return api_response.create_response()
Пример #4
0
 def post(self, request):
     """
     Function is used to create new object or value in table and return status.
     :param request: request header with user info for creating new object.
     :return: user info
     """
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         context = {
             'username': serializer.data['username'],
             'site_url': constants.SITE_URL + constants.LOGIN
         }
         # render email html
         email_html_message = render_to_string(
             'email_templates/register_success.html', context)
         utils.send_email(subject="WELCOME!",
                          recipient=[serializer.data['email']],
                          body=email_html_message)
         api_response = ApiResponse(status=1,
                                    data=serializer.data,
                                    message=constants.CREATE_USER_SUCCESS,
                                    http_status=status.HTTP_201_CREATED)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=get_error_message(serializer),
                                http_status=status.HTTP_400_BAD_REQUEST)
     return api_response.create_response()
Пример #5
0
 def post(self, request):
     """
     Function is used to create new object or value in table and return status.
     :param request: request header with user info for creating new object.
     :return: user info
     """
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         holiday_flag = False
         holiday_details = []
         abstract_ip_geolocation_api = importlib.import_module(
             "abstract-ip-geolocation-api")
         geolocation_api = abstract_ip_geolocation_api.v1(
             geolocation_api_key)
         location_data = geolocation_api.geolocate()
         current_time = datetime.datetime.now()
         if 'ip_address' in location_data:
             geolocation_data = location_data
             country = location_data['country_code']
             response = requests.get(
                 'https://holidays.abstractapi.com/v1/',
                 params={
                     'api_key': holiday_api_key,
                     'country': country,
                     'year': current_time.year,
                     'month': current_time.month,
                     'day': current_time.day
                 },
             )
             if response.status_code == 200:
                 if response.json():
                     holiday_flag = True
                     holiday_details = response.json()
         serializer.save(is_holiday=holiday_flag,
                         holiday_details=holiday_details,
                         geolocation_data=geolocation_data)
         context = {
             'username': serializer.data['username'],
             'site_url': constants.SITE_URL + constants.LOGIN
         }
         # render email html
         email_html_message = render_to_string(
             'email_templates/register_success.html', context)
         utils.send_email(subject="WELCOME!",
                          recipient=[serializer.data['email']],
                          body=email_html_message)
         api_response = ApiResponse(status=1,
                                    data=serializer.data,
                                    message=constants.CREATE_USER_SUCCESS,
                                    http_status=status.HTTP_201_CREATED)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=get_error_message(serializer),
                                http_status=status.HTTP_400_BAD_REQUEST)
     return api_response.create_response()
Пример #6
0
 def post(self, request):
     """
     Function is used to create new object or value in table and return status.
     :param request: request header with user info for creating new object.
     :return: wall info
     """
     serializer = WallSerializer(data=request.data,
                                 context={'request': request})
     if serializer.is_valid():
         serializer.save()
         api_response = ApiResponse(status=1,
                                    data=serializer.data,
                                    message=constants.CREATE_WALL_SUCCESS,
                                    http_status=status.HTTP_201_CREATED)
         return api_response.create_response()
     api_response = ApiResponse(status=0,
                                message=serializer.errors,
                                http_status=status.HTTP_400_BAD_REQUEST)
     return api_response.create_response()
Пример #7
0
 def get(self, request, pk):
     """
     Function is used for get comment info with pk
     :param request: request header with required info.
     :return: comment info or send proper error status
     """
     try:
         comment = Comment.objects.get(id=pk)
     except Comment.DoesNotExist:
         api_response = ApiResponse(
             status=0,
             message=constants.COMMENT_DOES_NOT_EXIST,
             http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
     serializer = CommentSerializer(comment)
     api_response = ApiResponse(status=1,
                                data=serializer.data,
                                message=constants.GET_COMMENT_SUCCESS,
                                http_status=status.HTTP_200_OK)
     return api_response.create_response()
Пример #8
0
 def delete(self, request, pk):
     """
     Function is used for deleting user object
     :param request: request header with required info.
     :param pk: primary key of a object.
     :return: 200 ok or error message
     """
     try:
         user = User.objects.get(id=pk)
     except User.DoesNotExist as e:
         logger.exception(e)
         api_response = ApiResponse(status=0,
                                    message=constants.USER_DOES_NOT_EXIST,
                                    http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
     user.delete()
     api_response = ApiResponse(status=1,
                                message=constants.DELETE_USER_SUCCESS,
                                http_status=status.HTTP_200_OK)
     return api_response.create_response()
Пример #9
0
    def delete(self, request, pk):
        """
        Function is used for deleting wall object
        :param request: request header with required info.
        :param pk: primary field to delete wall info.
        :return: 200 ok or error message
        """

        try:
            wall = Wall.objects.get(id=pk)
        except Wall.DoesNotExist as e:
            logger.exception(e)
            api_response = ApiResponse(status=0,
                                       message=constants.WALL_DOES_NOT_EXIST,
                                       http_status=status.HTTP_404_NOT_FOUND)
            return api_response.create_response()
        wall.delete()
        api_response = ApiResponse(status=1,
                                   message=constants.DELETE_WALL_SUCCESS,
                                   http_status=status.HTTP_200_OK)
        return api_response.create_response()
Пример #10
0
 def get(self, request, pk):
     """
     Function is used for get wall info with pk
     :param request: request header with required info.
     :param pk: primary key of a object.
     :return: wall info or send proper error status
     """
     try:
         wall = Wall.objects.get(id=pk)
     except Wall.DoesNotExist as e:
         logger.exception(e)
         api_response = ApiResponse(status=0,
                                    message=constants.WALL_DOES_NOT_EXIST,
                                    http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
     serializer = WallSerializer(wall)
     api_response = ApiResponse(status=1,
                                data=serializer.data,
                                message=constants.GET_WALL_SUCCESS,
                                http_status=status.HTTP_200_OK)
     return api_response.create_response()
Пример #11
0
 def get(self, request):
     """
     Function is used to get all the user list.
     :param request: request header with required info.
     :return: user list
     """
     users = User.objects.all()
     serializer = UserSerializer(users, many=True)
     api_response = ApiResponse(status=1,
                                data=serializer.data,
                                message=constants.USERS_GET_SUCCESS,
                                http_status=status.HTTP_200_OK)
     return api_response.create_response()
Пример #12
0
 def get(self, request, wall_pk):
     """
     Function is used for get dislikes info with pk
     :param request: request header with required info.
     :return: comment info or send proper error status
     """
     try:
         wall = Wall.objects.get(id=wall_pk)
         if request.user in wall.dis_likes.users.all():
             wall.dis_likes.users.remove(request.user)
         else:
             wall.dis_likes.users.add(request.user)
             wall.likes.users.remove(request.user)
         api_response = ApiResponse(status=1,
                                    message=constants.GET_DISLIKE_SUCCESS,
                                    http_status=status.HTTP_200_OK)
         return api_response.create_response()
     except Wall.DoesNotExist as e:
         logger.exception(e)
         api_response = ApiResponse(status=0,
                                    message=constants.WALL_DOES_NOT_EXIST,
                                    http_status=status.HTTP_404_NOT_FOUND)
         return api_response.create_response()
Пример #13
0
    def get(self, request):
        """
        Function is used to get all the Wall list.
        :param request: request header with required info.
        :return: Wall list
        """
        page_number = self.request.query_params.get('page', 1)
        page_size = self.request.query_params.get('page_size', 10)
        sort_by = self.request.query_params.get('sort_by', 'created_on')
        order = self.request.query_params.get('order', 'desc')
        search = self.request.query_params.get('search', None)

        if order == 'desc':
            sort_by = '-' + sort_by

        if search:
            walls = Wall.objects.filter(
                title__icontains=search).order_by(sort_by)
        else:
            walls = Wall.objects.all().order_by(sort_by)

        paginator = Paginator(walls, page_size)

        count = paginator.count
        total_page = len(paginator.page_range)
        next = paginator.page(page_number).has_next()
        previous = paginator.page(page_number).has_previous()
        serializer = WallSerializer(paginator.page(page_number), many=True)
        api_response = ApiResponse(status=1,
                                   data=serializer.data,
                                   message=constants.WALLS_GET_SUCCESS,
                                   http_status=status.HTTP_200_OK,
                                   count=count,
                                   total_page=total_page,
                                   next=next,
                                   previous=previous)
        return api_response.create_response()