示例#1
0
 def create_bucketlist_item(id, **kwargs):
     """Creates a new bucketlist child item of a bucketlist.
     Returns a success message.
     """
     name = request.form.get("name")
     done = request.form.get("done")
     bucketlistitem = BucketListItem(bucketlist_id=id, name=name, done=done)
     bucketlistitem.save()
     return {"message": "Bucketlist item was created successfully", "bucketlistitem": bucketlistitem.to_json()}, 201
示例#2
0
 def create_bucketlist_item(id, **kwargs):
     '''Creates a new bucketlist child item of a bucketlist.
     Returns a success message.
     '''
     name = request.form.get('name')
     done = request.form.get('done')
     bucketlistitem = BucketListItem(bucketlist_id=id, name=name, done=done)
     bucketlistitem.save()
     return {
         "message": "Bucketlist item was created successfully",
         "bucketlistitem": bucketlistitem.to_json()
     }, 201
示例#3
0
 def create_bucketlist_item(id, **kwargs):
     """ Creates a new item under a given bucketlist """
     if request.method == 'POST':
         name = request.form.get('name')
         done = request.form.get('done')
         bucketlist_item = BucketListItem(
             bucketlist_id=id, name=name, done=done
         )
         bucketlist_item.save()
         return {
             "message": "Bucketlist item was successfully created",
             "bucketlistsitem": bucketlist_item.to_json()
         }, 201
     else:
         # Get the bucketlist items instead of a 500 internal server error
         bucketlist = BucketList.query.get(id)
         return bucketlist.to_json(), 200
示例#4
0
    def post(self, id, item_id=None):
        """
        request that handles bucketlist item creation
        """
        if item_id:
            response = jsonify({'message': 'Method not allowed(POST)'})
            response.status_code = 400
            return response

        bucketlist = BucketList.query.get(id)
        if bucketlist:
            if bucketlist.created_by != g.user.id:
                response = jsonify({
                    'message':
                    'You are not authorized to use the bucketlist'
                })
                response.status_code = 401
                return response
            parser = reqparse.RequestParser()
            parser.add_argument('name', type=str, help='A name is required')
            parser.add_argument('description', type=str, default='')
            parser.add_argument('is_done', type=bool, default=False)
            args = parser.parse_args()

            name = args["name"]
            description = args["description"]

            if not name:
                response = jsonify({
                    'message':
                    'Please provide a name for the bucketlist item'
                })
                response.status_code = 400
                return response
            is_done = args["is_done"]
            if not is_done or type(is_done) != bool:
                is_done == False

            item = BucketListItem(name=name,
                                  description=description,
                                  bucketlist_id=id,
                                  is_done=is_done,
                                  created_by=g.user.id)

            if not name:
                response = jsonify(
                    {'message': 'Please provide a name for the item'})
                response.status_code = 400
                return response

            try:
                BucketListItem.query.filter_by(name=name).one()
                response = jsonify(
                    {'message': 'That name is already taken, try again'})
                response.status_code = 400
                return response

            except:
                try:
                    db.session.add(item)
                    db.session.commit()
                    message = {
                        'message': 'Bucket List item added Successfully!'
                    }
                    response = marshal(item, items_serializer)
                    response.update(message)
                    return response, 201

                except:
                    response = jsonify(
                        {'message': 'There was an error saving the item'})
                    response.status_code = 400
                    return response
        else:
            response = jsonify({
                'message':
                'A bucketlist with the ID provided does not exist!'
            })
            response.status_code = 204
            return response