0

Below is the view function of property view, and I would like to add pagination to this.

class PropertyView(Resource):

 def get(self):
    try:
        data = request.json
        verifyJwt= request.headers.get('Authorization')
        token = jwt.decode(verifyJwt,"secret",algorithm="HS256")
        print(token.get('email'))        
        data = request.json
        user_email= User.query.filter_by(email=token.get('email')).first()  
        print(user_email,"TEST")
        if user_email != "null" :
            property_data = Property.query.order_by(Property.id.desc()).all()
            property_list = []
            for property_obj in property_data:
                property_list.append({'id': property_obj.id, 'address': property_obj.address, "latitude": property_obj.latitude, "longitude":property_obj.longitude, "cdr_date": property_obj.cdr_date, "building_grossfloor_area": property_obj.building_grossfloor_area, "multi_family": property_obj.multi_family, "artist_studios": property_obj.artist_studios, "max_building_height": property_obj.max_building_height, "stories": property_obj.stories, "retail_commercial_sf": property_obj.retail_commercial_sf, "parking_spaces": property_obj.parking_spaces, "bike_spaces": property_obj.bike_spaces, "architect": property_obj.architect, "developer": property_obj.developer, "site_area": property_obj.site_area, "picture_1": property_obj.picture_1, "picture_2": property_obj.picture_2, "original_name": property_obj.original_name})
        
            response_data = property_list
            return ResponseGenerator(data=response_data,
                                         status=status.HTTP_201_CREATED).make_success_response()

    except Exception as e:
        logger.exception(e)
        return ResponseGenerator(message='Exception: {}'.format(e),
                                 status=status.HTTP_400_BAD_REQUEST).make_error_response()

1 Answers1

0

So after searching google and making so many correction, I was able to apply proper pagination in a simple way possible also with search filter. Pasting so that it might help others.

class PropertyView(Resource):

def post(self):
    try:
            value = data.get('per_page')
            per_page = value
            value1 = data.get('page_num')
            page_num = value1
            search = data.get('search')
            search = "%{}%".format(search)
            property_data = Property.query.order_by(Property.id.desc()).all()
            total = len(property_data)

            page = request.args.get('page', 1, type=int)
            if search !='':
                property_data = Property.query.filter(or_(Property.address.like(search), Property.architect.like(search), Property.developer.like(search))).order_by(Property.id.desc()).paginate(page=page_num, per_page=per_page, error_out=True).items
                num = len(property_data)
                
            else:
                property_data = Property.query.order_by(Property.id.desc()).paginate(page=page_num, per_page=per_page, error_out=True).items
                num = len(property_data)
                
            

            (print("Check Pagination",property_data,page_num,per_page))
            property_list = []
            for property_obj in property_data:
                property_list.append({'id': property_obj.id, 'address': property_obj.address, "latitude": property_obj.latitude, "longitude":property_obj.longitude, "cdr_date": property_obj.cdr_date, "building_grossfloor_area": property_obj.building_grossfloor_area, "multi_family": property_obj.multi_family, "artist_studios": property_obj.artist_studios, "max_building_height": property_obj.max_building_height, "stories": property_obj.stories, "retail_commercial_sf": property_obj.retail_commercial_sf, "parking_spaces": property_obj.parking_spaces, "bike_spaces": property_obj.bike_spaces, "architect": property_obj.architect, "developer": property_obj.developer, "site_area": property_obj.site_area, "picture_1": property_obj.picture_1, "picture_2": property_obj.picture_2, "original_name": property_obj.original_name, "p_status": property_obj.p_status})


        else:
            print("Not authorised to view this data.")