Ejemplo n.º 1
0
    def bucketlist():
        """Retrieves all bucketlists for a logged in user and returns a
         JSON response for (GET request) to /bucketlists.
        POST requests to this route creates a bucketlist for the logged
         in user.
        """
        user_id = auth.get_current_user_id()
        if request.method == "GET":
            query = BucketList.query.filter_by(created_by=user_id)
            limit = request.args.get("limit", 20)
            q = request.args.get("q")
            page = request.args.get("page", 1)
            result_data = None
            if query.all():
                if not 0 <= int(limit) <= 100:
                    raise NotAcceptable("Maximum limit per page is 100.")
                else:
                    result_data = query
                if q:
                    result_data = query.filter(BucketList.name.ilike("%{0}%".format(q)))
                result_data = list_object_transform(result_data.paginate(page, int(limit), False).items)
                return {"message": result_data}
            raise NotFound("There are no bucketlist for this user")

        else:
            name = request.form.get("name")
            a_bucketlist = BucketList(created_by=user_id, name=name)
            a_bucketlist.save()
            return {"message": "Bucketlist was created successfully", "bucketlist": a_bucketlist.to_json()}, 201
Ejemplo n.º 2
0
    def bucketlist():
        '''Retrieves all bucketlists for a logged in user and returns a
         JSON response for (GET request) to /bucketlists.
        POST requests to this route creates a bucketlist for the logged
         in user.
        '''
        user_id = auth.get_current_user_id()
        if request.method == "GET":
            query = BucketList.query.filter_by(created_by=user_id)
            limit = request.args.get('limit', 20)
            q = request.args.get('q')
            page = request.args.get('page', 1)
            result_data = None
            if query.all():
                if not 0 <= int(limit) <= 100:
                    raise NotAcceptable('Maximum limit per page is 100.')
                else:
                    result_data = query
                if q:
                    result_data = query.filter(
                        BucketList.name.ilike('%{0}%'.format(q)))
                result_data = list_object_transform(
                    result_data.paginate(page, int(limit), False).items)
                return {'message': result_data}
            raise NotFound('There are no bucketlist for this user')

        else:
            name = request.form.get("name")
            a_bucketlist = BucketList(created_by=user_id, name=name)
            a_bucketlist.save()
            return {
                "message": "Bucketlist was created successfully",
                "bucketlist": a_bucketlist.to_json()
            }, 201
Ejemplo n.º 3
0
    def to_json(self):
        """Converts model object into dict to ease Serialization
        """
        jsonData = dict()
        for prop in class_mapper(self.__class__).iterate_properties:

            if prop.key == 'user':
                continue

            if prop.key == 'bucketlistitems':
                prop_items = getattr(self, prop.key)
                jsonData[prop.key] = list_object_transform(prop_items)
                continue

            jsonData[prop.key] = getattr(self, prop.key)
        return jsonData
Ejemplo n.º 4
0
    def to_json(self):
        """Converts model object into dict to ease Serialization
        """
        jsonData = dict()
        for prop in class_mapper(self.__class__).iterate_properties:

            if prop.key == 'user':
                continue

            if prop.key == 'bucketlistitems':
                prop_items = getattr(self, prop.key)
                jsonData[prop.key] = list_object_transform(
                    prop_items)
                continue

            jsonData[prop.key] = getattr(self, prop.key)
        return jsonData