librelist archives

« back to archive

Pluggable Views - Assertion Error

Pluggable Views - Assertion Error

From:
Anthony Ford
Date:
2013-06-29 @ 06:49
Hey All,

Can anyone help me with this? I'm attempting to resurrect an old project of
mine originally written using flask 0.7 or 0.8. I'm attempting to update it
to flask 0.10, but I'm running into an issue I haven't seen before.

I'm using pluggable views, and more specifically, the method views for APIs
(as laid out at http://flask.pocoo.org/docs/views/#method-views-for-apis),
in pretty much a verbatim manner.

However, I keep getting an Assertion Error telling me that one of my views
is attempting to map to an existing endpoint. If I comment out two of the
three add_url_rules in my route registration function, the error goes away,
but that means that I am missing functionality.

Anybody have any suggestions/solutions/hints?

Code and traceback below, thanks in advance for any help:



My methodview class:

class OwnerAPI(MethodView):
    """ Class views for Owner. RESTful API.
+--- 10 lines: Behavior is as follows:-------------
    """

    def get(self, id):
        if id is None:
            return jsonify( owners = [x.serialize()
                            for x in Owner.query.all()] )
        else:
            query = Owner.query.filter_by(id=id)
            if query.count() != 0:
                return jsonify(query.first().serialize())
            else:
                abort(404)

    def post(self):
        """ Creates a new Owner. """
        lastname = request.form['lastname']
        firstname = request.form.get('firstname',None)
        phone = request.form.get('phone',None)

        owner = Owner(lastname,phone,firstname)
        db.session.add(owner)
        db.session.commit()
        confirm = {'owner_id':owner.id}

        resp = jsonify(confirm)
        resp.status_code = 201
        return resp

    def delete(self, id):
        """ Deletes given Owner. """
        if Owner.query.filter_by(id=id).delete():
            db.session.commit()
            return make_status(204)
        else:
            abort(404)

and the api blueprint creation:

api = Blueprint('api', __name__)


and my route registration method:

def register_api(view,endpoint,url,var='id',var_type='int'):
    api.add_url_rule(url, view_func=view.as_view(endpoint),methods=['POST'])
    api.add_url_rule(url, defaults={var: None},
view_func=view.as_view(endpoint),
            methods=['GET'])
    api.add_url_rule('%s<%s:%s>'%(url,var_type,var),
            view_func=view.as_view(endpoint),methods=['GET','PUT','DELETE'])


and the actual registration of the methodview:

register_api(OwnerAPI,'owner_api','/owner/')


And finally, the traceback that I get when I attempt to run my app:


File "manage.py", line 7, in <module>
    from projectD import app, models
  File "/home/ajford/project/projectD/__init__.py", line 41, in <module>
    app.register_blueprint(api, url_prefix='/api')
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/app.py",
line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/app.py",
line 889, in register_blueprint
    blueprint.register(self, options, first_registration)
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/blueprints.py",
line 153, in register
    deferred(state)
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/blueprints.py",
line 172, in <lambda>
    s.add_url_rule(rule, endpoint, view_func, **options))
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/blueprints.py",
line 76, in add_url_rule
     view_func, defaults=defaults, **options)
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/app.py",
line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File

"/home/ajford/.virtualenvs/project/local/lib/python2.7/site-packages/flask/app.py",
line 984, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint
function: api.owner_api




Anthony Ford,
KF5IBN,
Research Assistant,
Center for Advanced Radio Astronomy,
University of Texas at Brownsville
ford.anthonyj@gmail.com