From 87f4d31cee744a6c6d1913c74b4e6f12b287c0b8 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 26 Sep 2013 18:19:27 -0400 Subject: [PATCH] content endpoints --- service/app.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- service/models.py | 6 +++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/service/app.py b/service/app.py index 0ac4d51..a84eddd 100644 --- a/service/app.py +++ b/service/app.py @@ -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('/') 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('//text') +def get_document_text(slug): + + doc = get_or_404(Document, slug) + return doc.text + +@app.route('//html') +def get_document_html(slug): + + doc = get_or_404(Document, slug) + return doc.html + +@app.route('/content/') +def get_content(hash): + + doc = get_or_404(Content, hash) + + document = { + 'hash': doc.hash, + 'text': doc.text, + } + + return jsonify(content=document) + +@app.route('/content//text') +def get_content_text(hash): + + doc = get_or_404(Content, hash) + return doc.text + +@app.route('/content//html') +def get_content_html(hash): + + doc = get_or_404(Content, hash) + return doc.html if __name__ == '__main__': app.run() \ No newline at end of file diff --git a/service/models.py b/service/models.py index f42554b..76aa40a 100644 --- a/service/models.py +++ b/service/models.py @@ -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()