mirror of
https://github.com/shouptech/humulus.git
synced 2026-02-03 20:59:41 +00:00
Add timestamps to docs
This commit is contained in:
parent
d5e159c9b2
commit
e9b0117027
3 changed files with 32 additions and 4 deletions
|
|
@ -14,7 +14,7 @@
|
||||||
# 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 time
|
from datetime import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
@ -90,9 +90,22 @@ def put_doc(doc):
|
||||||
# Use a UUID for name
|
# Use a UUID for name
|
||||||
doc['_id'] = str(uuid.uuid4())
|
doc['_id'] = str(uuid.uuid4())
|
||||||
|
|
||||||
|
# Add a created timestamp
|
||||||
|
# Timestamps are written to couchdb in ISO-8601 format
|
||||||
|
doc['created'] = datetime.utcnow().isoformat()
|
||||||
|
|
||||||
return db.create_document(doc, throw_on_exists=True)
|
return db.create_document(doc, throw_on_exists=True)
|
||||||
|
|
||||||
|
|
||||||
|
def update_doc(doc):
|
||||||
|
"""Update a doc.
|
||||||
|
|
||||||
|
Adds an 'updated' field representing the current time the doc was updated.
|
||||||
|
"""
|
||||||
|
doc['updated'] = datetime.utcnow().isoformat()
|
||||||
|
doc.save()
|
||||||
|
|
||||||
|
|
||||||
def get_doc(id):
|
def get_doc(id):
|
||||||
"""Gets a doc from CouchDB and returns it."""
|
"""Gets a doc from CouchDB and returns it."""
|
||||||
return get_db()[id]
|
return get_db()[id]
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ from wtforms import (Form, StringField, DecimalField, TextAreaField, FieldList,
|
||||||
FormField, SelectField)
|
FormField, SelectField)
|
||||||
from wtforms.validators import DataRequired, Optional
|
from wtforms.validators import DataRequired, Optional
|
||||||
|
|
||||||
from humulus.couch import get_doc_or_404, put_doc
|
from humulus.couch import get_doc_or_404, put_doc, update_doc
|
||||||
|
|
||||||
bp = Blueprint('recipes', __name__, url_prefix='/recipes')
|
bp = Blueprint('recipes', __name__, url_prefix='/recipes')
|
||||||
|
|
||||||
|
|
@ -222,7 +222,7 @@ def update(id):
|
||||||
# Copy values from submitted form to the existing recipe and save
|
# Copy values from submitted form to the existing recipe and save
|
||||||
for key, value in form.doc.items():
|
for key, value in form.doc.items():
|
||||||
recipe[key] = value
|
recipe[key] = value
|
||||||
recipe.save()
|
update_doc(recipe)
|
||||||
|
|
||||||
flash('Updated recipe: {}'.format(form.name.data), 'success')
|
flash('Updated recipe: {}'.format(form.name.data), 'success')
|
||||||
return redirect(url_for('recipes.info', id=id))
|
return redirect(url_for('recipes.info', id=id))
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from humulus.couch import get_doc, put_doc
|
from humulus.couch import get_doc, put_doc, update_doc
|
||||||
|
|
||||||
|
|
||||||
def test_put_doc(app):
|
def test_put_doc(app):
|
||||||
|
|
@ -22,6 +22,7 @@ def test_put_doc(app):
|
||||||
data = {'foo': 'bar'}
|
data = {'foo': 'bar'}
|
||||||
response = put_doc(data)
|
response = put_doc(data)
|
||||||
assert '_id' in response
|
assert '_id' in response
|
||||||
|
assert 'created' in response
|
||||||
|
|
||||||
response = put_doc({'name': 'test'})
|
response = put_doc({'name': 'test'})
|
||||||
assert response['_id'] == 'test'
|
assert response['_id'] == 'test'
|
||||||
|
|
@ -33,6 +34,20 @@ def test_put_doc(app):
|
||||||
assert response['_id'] == 'test-2'
|
assert response['_id'] == 'test-2'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_doc(app):
|
||||||
|
with app.app_context():
|
||||||
|
doc = get_doc('awesome-lager')
|
||||||
|
rev = doc['_rev']
|
||||||
|
doc['test'] = 'update'
|
||||||
|
update_doc(doc)
|
||||||
|
|
||||||
|
updated_doc = get_doc('awesome-lager')
|
||||||
|
assert doc['_id'] == updated_doc['_id']
|
||||||
|
assert rev < updated_doc['_rev']
|
||||||
|
assert updated_doc['test'] == 'update'
|
||||||
|
assert 'updated' in updated_doc
|
||||||
|
|
||||||
|
|
||||||
def test_build_couch_command(runner, monkeypatch):
|
def test_build_couch_command(runner, monkeypatch):
|
||||||
class Recorder(object):
|
class Recorder(object):
|
||||||
called = False
|
called = False
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue