content endpoints

This commit is contained in:
Kenneth Reitz
2013-09-26 18:19:27 -04:00
parent 035882384f
commit 87f4d31cee
2 changed files with 47 additions and 5 deletions
+42 -4
View File
@@ -7,6 +7,12 @@ from .models import User, Document, Content, NotFound
app = Flask(__name__)
def get_or_404(cls, key):
try:
return cls.get(key)
except NotFound:
abort(404)
@app.route('/')
def hello():
return 'Hello World!'
@@ -19,10 +25,7 @@ def page_not_found(e):
@app.route('/<path:slug>')
def get_document(slug):
try:
doc = Document.get(slug)
except NotFound:
abort(404)
doc = get_or_404(Document, slug)
document = {
'slug': doc.slug,
@@ -34,6 +37,41 @@ def get_document(slug):
return jsonify(document=document)
@app.route('/<path:slug>/text')
def get_document_text(slug):
doc = get_or_404(Document, slug)
return doc.text
@app.route('/<path:slug>/html')
def get_document_html(slug):
doc = get_or_404(Document, slug)
return doc.html
@app.route('/content/<hash>')
def get_content(hash):
doc = get_or_404(Content, hash)
document = {
'hash': doc.hash,
'text': doc.text,
}
return jsonify(content=document)
@app.route('/content/<hash>/text')
def get_content_text(hash):
doc = get_or_404(Content, hash)
return doc.text
@app.route('/content/<hash>/html')
def get_content_html(hash):
doc = get_or_404(Content, hash)
return doc.html
if __name__ == '__main__':
app.run()
+5 -1
View File
@@ -51,7 +51,7 @@ class Document(DynamoDBModel):
@property
def html(self):
return render(self.text)
return Content.get(self.content).html
@property
def owner(self):
@@ -98,6 +98,10 @@ class Content(DynamoDBModel):
u'text': unicode,
}
@property
def html(self):
return render(self.text)
@classmethod
def store(cls, text):
content = cls()