Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API API Protection HTTP Basic Auth

Nathan Chin
Nathan Chin
10,179 Points

Error in put method in reviews.py

In reviews.py, I'm getting an error for the put method on the line

args = self.reqparse.parse_args()

, saying "Instance of 'Review' has no 'reqparse' member. pylint(no-member)." I'm also using VS Code for this so I'm not sure if it's simply a VS Code issue or something else.

2 Answers

Anthony Kimberly
Anthony Kimberly
11,049 Points

Can you post the rest of your code? I just checked my reviews.py and I don't have a line with "args = "

Thanks, Anthony

Nathan Chin
Nathan Chin
10,179 Points

reviews.py

import json

from flask import jsonify, Blueprint, abort, g, make_response

from flask.ext.restful import (Resource, Api, reqparse, inputs, fields,
                               url_for, marshal, marshal_with)

from auth import auth
import models

review_fields = {
    'id': fields.Integer,
    'for_course': fields.String,
    'rating': fields.Integer,
    'comment': fields.String(default=''),
    'created_at': fields.DateTime
}

def review_or_404(review_id):
    try:
        review = models.Review.get(models.Review.id==review_id)
    except models.Review.DoesNotExist:
        abort(404)
    else:
        return review


def add_course(review):
    review.for_course = url_for('resources.courses.course', id=review.course.id)
    return review


class ReviewList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'course',
            type=inputs.positive,
            required=True,
            help='No course provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'rating',
            type=inputs.int_range(1, 5),
            required=True,
            help='No rating provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'comment',
            required=False,
            nullable=True,
            location=['form', 'json'],
            default=''
        )
        super().__init__()

    def get(self):
        return {'reviews': [
            marshal(add_course(review), review_fields)
            for review in models.Review.select()
        ]}

    @marshal_with(review_fields)
    @auth.login_required
    def post(self):
        args = self.reqparse.parse_args()
        review = models.Review.create(
            created_by=g.user,
            **args
        )
        return (add_course(review), 201, {
                'Location': url_for('resources.reviews.review', id=review.id)
               })

class Review(Resource):
    @marshal_with(review_fields)
    def get(self, id):
        return add_course(review_or_404(id))

    @marshal_with(review_fields)
    @auth.login_required
    def put(self, id):
        args = self.reqparse.parse_args()
        try:
            review = models.Review.select().where(
                models.Review.created_by==g.user,
                models.Review.id==id
            ).get()
        except models.Review.DoesNotExist:
            return make_response(json.dumps(
                    {'error': 'That review does not exist or is not editable'}
                ), 403)
        query = review.update(**args)
        query.execute()
        review = add_course(review_or_404(id))
        return (review, 200, {
                'Location': url_for('resources.reviews.review', id=id)
               })

    @auth.login_required
    def delete(self, id):
        try:
            review = models.Review.select().where(
                models.Review.created_by==g.user,
                models.Review.id==id
            ).get()
        except models.Review.DoesNotExist:
            return make_response(json.dumps(
                    {'error': 'That review does not exist or is not editable'}
                ), 403)
        query = review.delete()
        query.execute()
        return '', 204, {'Location': url_for('resources.reviews.reviews')}


reviews_api = Blueprint('resources.reviews', __name__)
api = Api(reviews_api)
api.add_resource(
    ReviewList,
    '/reviews',
    endpoint='reviews'
)
api.add_resource(
    Review,
    '/reviews/<int:id>',
    endpoint='review'
)

In the video at 6:35 it shows that line of code when he adds the args = line.