mirror of
https://github.com/shouptech/humulus.git
synced 2026-02-03 18:19:42 +00:00
Add design documents during building of couch.
This commit is contained in:
parent
cc4ed2bef9
commit
93fe0cce2d
7 changed files with 87 additions and 18 deletions
|
|
@ -1,3 +1,4 @@
|
|||
graft src/humulus/static
|
||||
graft src/humulus/templates
|
||||
graft src/humulus/designs
|
||||
global-exclude *.pyc
|
||||
|
|
|
|||
|
|
@ -14,9 +14,10 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
from cloudant import CouchDB
|
||||
|
|
@ -54,6 +55,7 @@ def build_couch():
|
|||
couch = get_couch()
|
||||
dbname = current_app.config['COUCH_DATABASE']
|
||||
couch.create_database(dbname, throw_on_exists=False)
|
||||
put_designs()
|
||||
|
||||
|
||||
@click.command('build-couch')
|
||||
|
|
@ -121,11 +123,32 @@ def get_doc_or_404(id):
|
|||
return doc
|
||||
|
||||
|
||||
def put_doc_from_file(filename):
|
||||
"""Loads filename and stores it in couch.
|
||||
def put_designs():
|
||||
"""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:
|
||||
response = put_doc(json.load(doc))
|
||||
return response
|
||||
here = Path(__file__).parent
|
||||
|
||||
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)
|
||||
|
|
|
|||
12
src/humulus/designs/recipes.json
Normal file
12
src/humulus/designs/recipes.json
Normal 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": {}
|
||||
}
|
||||
12
tests/assets/changed/designs/recipes.json
Normal file
12
tests/assets/changed/designs/recipes.json
Normal 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": {}
|
||||
}
|
||||
8
tests/assets/initial/designs/recipes.json
Normal file
8
tests/assets/initial/designs/recipes.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"_id": "_design/recipes",
|
||||
"language": "javascript",
|
||||
"views": {},
|
||||
"lists": {},
|
||||
"indexes": {},
|
||||
"shows": {}
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"_id": "testfile",
|
||||
"foo": "bar"
|
||||
}
|
||||
|
|
@ -12,10 +12,10 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import pathlib
|
||||
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):
|
||||
|
|
@ -67,9 +67,26 @@ def test_get_doc(app):
|
|||
assert get_doc('foobar')['data'] == 'test'
|
||||
|
||||
|
||||
def test_put_doc_from_file(app):
|
||||
here = pathlib.Path(__file__).parent
|
||||
def test_put_designs(app, monkeypatch):
|
||||
# Always return the path to our test assets
|
||||
testpath = Path(__file__).parent
|
||||
|
||||
with app.app_context():
|
||||
response = put_doc_from_file(here/'assets'/'test_doc.json')
|
||||
assert response['_id'] == 'testfile'
|
||||
assert response['foo'] == 'bar'
|
||||
# Test initial load of designs
|
||||
monkeypatch.setattr(Path, 'parent', testpath/'assets/initial')
|
||||
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']
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue