1
0
Fork 0
mirror of https://github.com/shouptech/humulus.git synced 2026-02-03 20:49:44 +00:00

Add design documents during building of couch.

This commit is contained in:
Emma 2019-06-27 21:13:07 -06:00
parent cc4ed2bef9
commit 93fe0cce2d
7 changed files with 87 additions and 18 deletions

View file

@ -1,3 +1,4 @@
graft src/humulus/static graft src/humulus/static
graft src/humulus/templates graft src/humulus/templates
graft src/humulus/designs
global-exclude *.pyc global-exclude *.pyc

View file

@ -14,9 +14,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from datetime import datetime
import json import json
import uuid import uuid
from datetime import datetime
from pathlib import Path
import click import click
from cloudant import CouchDB from cloudant import CouchDB
@ -54,6 +55,7 @@ def build_couch():
couch = get_couch() couch = get_couch()
dbname = current_app.config['COUCH_DATABASE'] dbname = current_app.config['COUCH_DATABASE']
couch.create_database(dbname, throw_on_exists=False) couch.create_database(dbname, throw_on_exists=False)
put_designs()
@click.command('build-couch') @click.command('build-couch')
@ -121,11 +123,32 @@ def get_doc_or_404(id):
return doc return doc
def put_doc_from_file(filename): def put_designs():
"""Loads filename and stores it in couch. """Loads all design docs from the designs/ folder and puts them.
The file must be valid JSON. If any documents are missing an '_id' field, a KeyError exception will have
been raised. Not all documents will have been loaded.
""" """
with open(filename, 'r') as doc: here = Path(__file__).parent
response = put_doc(json.load(doc))
return response for filename in here.glob('designs/*.json'):
with open(filename, 'r') as fp:
data = json.load(fp)
# See if document already exists
if data['_id'] in get_db():
doc = get_doc(data['_id'])
# Popping off the revision and storing it. Then compare
rev = doc.pop('_rev')
doc.pop('created', None)
if data == doc:
get_db().clear()
return
# Copy the values of data to doc.
for k in data:
doc[k] = data[k]
doc['_rev'] = rev # Add the revision back
print(doc)
doc.save()
else:
put_doc(data)

View file

@ -0,0 +1,12 @@
{
"_id": "_design/recipes",
"language": "javascript",
"views": {
"by-date": {
"map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.created && doc.name) {\n emit(doc.created, doc.name)\n }\n}"
}
},
"lists": {},
"indexes": {},
"shows": {}
}

View file

@ -0,0 +1,12 @@
{
"_id": "_design/recipes",
"language": "javascript",
"views": {
"by-date": {
"map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.created && doc.name) {\n emit(doc.created, doc.name)\n }\n}"
}
},
"lists": {},
"indexes": {},
"shows": {}
}

View file

@ -0,0 +1,8 @@
{
"_id": "_design/recipes",
"language": "javascript",
"views": {},
"lists": {},
"indexes": {},
"shows": {}
}

View file

@ -1,4 +0,0 @@
{
"_id": "testfile",
"foo": "bar"
}

View file

@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import pathlib
import uuid import uuid
from pathlib import Path
from humulus.couch import get_doc, put_doc, update_doc, put_doc_from_file from humulus.couch import get_doc, put_doc, update_doc, put_designs
def test_put_doc(app): def test_put_doc(app):
@ -67,9 +67,26 @@ def test_get_doc(app):
assert get_doc('foobar')['data'] == 'test' assert get_doc('foobar')['data'] == 'test'
def test_put_doc_from_file(app): def test_put_designs(app, monkeypatch):
here = pathlib.Path(__file__).parent # Always return the path to our test assets
testpath = Path(__file__).parent
with app.app_context(): with app.app_context():
response = put_doc_from_file(here/'assets'/'test_doc.json') # Test initial load of designs
assert response['_id'] == 'testfile' monkeypatch.setattr(Path, 'parent', testpath/'assets/initial')
assert response['foo'] == 'bar' put_designs()
recipes = get_doc('_design/recipes')
assert 'language' in recipes
rev = recipes['_rev']
# Try again, make sure nothing changed.
put_designs()
recipes = get_doc('_design/recipes')
assert recipes['_rev'] == rev
# Test that changes can be loaded
monkeypatch.setattr(Path, 'parent', testpath/'assets/changed')
put_designs()
recipes = get_doc('_design/recipes')
assert 'by-date' in recipes['views']