Beispiel #1
0
    def bucketlist():
        """ Return a JSON response with all bucketlists """
        user_id = auth.get_current_user()
        results_data = None
        if request.method == 'GET':
            # pagination limit
            results = BucketList.get_all(user_id)
            limit = request.args.get('limit', 20)
            q = request.args.get('q')
            page = request.args.get('page', 1)
            if page < 1:
                raise NotFound('Please specify a valid page (positive number)')
            if results.all():
                results_data = results
                limit = 100 if int(limit) > 100 else limit
                if q:
                    results_data = results.filter(
                        BucketList.name.ilike('%{0}%'.format(q)))
                # serialize result objects to json
                result_list = []
                for item in results_data.paginate(
                        page, int(limit), False).items:
                    if callable(getattr(item, 'to_json')):
                        result = item.to_json()
                        result_list.append(result)
                return {'message': result_list}

            raise NotFound('User has no bucketlist')

        else:
            name = request.form.get("name")
            bucketlist = BucketList(created_by=user_id, name=name)
            bucketlist.save()
            return {
                "message": "Bucketlist was created successfully",
                "bucketlist": bucketlist.to_json()}, 201