Reputation: 21
I'm doing some dockerized code in Python (3.5) and flask (1.1.1) working against a CouchDB database (2.3.1) using the cloudant python extension (2.12.0) which seems to be the most up to date library to work against CouchDB.
I'm trying to fetch and use a view from the database, but it is not working. I can fetch documents, and work with the database normally, but I can't use the view.
I've added a print statement for the object that should hold the design document at the program start, and I see that the document shows as having no views (or anything at all) AND the CouchDB log shows NO requests for the design document being made.
I also tried to both get the design document and use the view via curl using the same URL and username/password, and both actions work successfully.
Here's sample code that fails:
from flask import Flask, render_template , request, g
from cloudant.client import CouchDB
from cloudant.view import View
from cloudant.design_document import DesignDocument
import requests
application = Flask(__name__)
application.config.from_pyfile("config.py")
couch = CouchDB(application.config['COUCHDB_USER'], application.config['COUCHDB_PASSWORD'], url=application.config['COUCHDB_SERVER'], connect=True, auto_renew=True)
database = couch[application.config['COUCHDB_DATABASE']]
views = DesignDocument(database, '_design/vistas')
print(views)
print(views.list_views())
@application.route("/", methods=['GET', 'POST'])
def index():
for pelicula in View(views,'titulos_peliculas'):
titulos.append({ "id": pelicula['id'], "titulo": pelicula['key'] })
return render_template('menu.html',titulos=titulos)
In that code, the print of the design document (views) returns:
{'lists': {}, 'indexes': {}, 'views': {}, 'shows': {}, '_id': '_design/vistas'}
With empty views as show... And the CouchDB log only shows the login to the database and getting the DB info:
couchdb:5984 172.23.0.4 undefined POST /_session 200 ok 69
couchdb:5984 172.23.0.4 vmb_web HEAD //peliculas 200 ok 232
No other queries at all.
No errors in the app log either. Even when I call the routed use of the views:
[pid: 21|app: 0|req: 1/1] 172.23.0.1 () {52 vars in 1225 bytes} [Mon Aug 5 15:03:24 2019] POST / => generated 1148 bytes in 56 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)
And, as I said, I can get, and use the document:
curl http://vmb_web:[email protected]:999/peliculas/_design/vistas
{"_id":"_design/vistas","_rev":"1-e8108d41a6627ea61b9a89a637f574eb","language":"javascript","views":{"peliculas":{"map":"function(doc) { if (doc.schema == 'pelicula') { emit(doc.titulo, null); for(i=0;i<doc.titulos_alt.length;i++) { emit(doc.titulos_alt[i],null); } for(i=0;i<doc.directores.length;i++) { emit(doc.directores[i].nombre,null); } for(i=0;i<doc.actores.length;i++) { emit(doc.actores[i].nombre,null); } for(i=0;i<doc.escritores.length;i++) { emit(doc.escritores[i].nombre,null); } for(i=0;i<doc.etiquetas.length;i++) { emit(doc.etiquetas[i],null); } } }"},"titulos_peliculas":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, null); } }"},"archivos_peliculas":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, doc.archivo); } }"},"titulo_rev":{"map":"function(doc) { if ((doc.schema == 'pelicula') && (doc.titulo)) { emit(doc.titulo, doc._rev); } }"}}}
Upvotes: 1
Views: 473
Reputation: 4364
I believe the code posted above creates a new DesignDocument object, and does not search for an existing DesignDocument. After creating that object, it looks like you need to call its fetch() method and then check its views property. HTH.
p.s. promoting my comment to an answer, hope that's cool in SO land these days :)
Upvotes: 0
Reputation: 21
I'm answering my own question, in case someone in the future stumbles upon this. I got the answer from Esteban Laver in the github for python-cloudant and it is what @chrisinmtown mentions in a response up there.
I was failing to call fetch() on the design document before using it.
Another good suggestion was to use the get_view_result helper method for the database object which takes care of fetching the design document and instantiating the View object from the selected view all at once.
Upvotes: 1