Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of https://github.com/j2labs/brubeck

  • Loading branch information...
commit 78d574b320de2a8e6bac0d2252e5abefb52b5ccd 2 parents 84444a1 + b8c1bfd
Ben Beecher authored
1  brubeck/request_handling.py
View
@@ -714,7 +714,6 @@ def post(self, item_ids=""):
if invalid:
return self.render(status_code=400)
if item_ids == "":
-
created, updated, failed = self.create(shields)
return self._create_response(updated, failed, created)
else:
10 demos/demo_apiable.py
View
@@ -1,5 +1,6 @@
#!/usr/bin/env python
+
from brubeck.request_handling import Brubeck, AutoAPIBase, WebMessageHandler
from brubeck.queryset import DictQueryset
from brubeck.templating import Jinja2Rendering, load_jinja2_env
@@ -7,8 +8,7 @@
from dictshield.document import Document
from dictshield.fields import (StringField,
- BooleanField,
- )
+ BooleanField)
class Todo(Document):
@@ -29,10 +29,10 @@ class TodosAPI(AutoAPIBase):
queries = DictQueryset(db_conn={})
model = Todo
-class BaseHandler(WebMessageHandler):
+class BaseHandler(Jinja2Rendering):
pass
-class TodosHandler(BaseHandler, Jinja2Rendering):
+class TodosHandler(BaseHandler):
def get(self):
"""A list display matching the parameters of a user's dashboard. The
parameters essentially map to the variation in how `load_listitems` is
@@ -54,7 +54,7 @@ def get(self):
config = {
'mongrel2_pair': ('ipc://127.0.0.1:9999', 'ipc://127.0.0.1:9998'),
'handler_tuples': handler_tuples,
- 'template_loader': load_jinja2_env('.'),
+ 'template_loader': load_jinja2_env('./templates/apiable'),
}
# Instantiate app instance
12 demos/demo_longpolling.py
View
@@ -7,9 +7,12 @@
import datetime
import time
-import eventlet
+try:
+ import eventlet
+except:
+ import gevent
-class DemoHandler(WebMessageHandler, Jinja2Rendering):
+class DemoHandler(Jinja2Rendering):
def get(self):
name = self.get_argument('name', 'dude')
self.set_body('Take five, %s!' % name)
@@ -18,7 +21,10 @@ def get(self):
class FeedHandler(WebMessageHandler):
def get(self):
- eventlet.sleep(5) # simple way to demo long polling :)
+ try:
+ eventlet.sleep(5) # simple way to demo long polling :)
+ except:
+ gevent.sleep(5)
self.set_body('The current time is: %s' % datetime.datetime.now(),
headers={'Content-Type': 'text/plain'})
return self.render()
1  demos/templates/apiable/errors.html
View
@@ -0,0 +1 @@
+Drat. Something broke.
143 demos/templates/apiable/todos.html
View
@@ -0,0 +1,143 @@
+<html>
+<head>
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script type="text/javascript">
+function RealTypeOf(v) {
+ if (typeof(v) == "object") {
+ if (v === null) return "null";
+ if (v.constructor == (new Array).constructor) return "array";
+ if (v.constructor == (new Date).constructor) return "date";
+ if (v.constructor == (new RegExp).constructor) return "regex";
+ return "object";
+ }
+ return typeof(v);
+}
+
+function FormatJSON(oData, sIndent) {
+ if (arguments.length < 2) {
+ var sIndent = "";
+ }
+ var sIndentStyle = " ";
+ var sDataType = RealTypeOf(oData);
+
+ // open object
+ if (sDataType == "array") {
+ if (oData.length == 0) {
+ return "[]";
+ }
+ var sHTML = "[";
+ } else {
+ var iCount = 0;
+ $.each(oData, function() {
+ iCount++;
+ return;
+ });
+ if (iCount == 0) { // object is empty
+ return "{}";
+ }
+ var sHTML = "{";
+ }
+
+ // loop through items
+ var iCount = 0;
+ $.each(oData, function(sKey, vValue) {
+ if (iCount > 0) {
+ sHTML += ",";
+ }
+ if (sDataType == "array") {
+ sHTML += ("\n" + sIndent + sIndentStyle);
+ } else {
+ sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": ");
+ }
+
+ // display relevant data type
+ switch (RealTypeOf(vValue)) {
+ case "array":
+ case "object":
+ sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
+ break;
+ case "boolean":
+ case "number":
+ sHTML += vValue.toString();
+ break;
+ case "null":
+ sHTML += "null";
+ break;
+ case "string":
+ sHTML += ("\"" + vValue + "\"");
+ break;
+ default:
+ sHTML += ("TYPEOF: " + typeof(vValue));
+ }
+
+ // loop
+ iCount++;
+ });
+
+ // close object
+ if (sDataType == "array") {
+ sHTML += ("\n" + sIndent + "]");
+ } else {
+ sHTML += ("\n" + sIndent + "}");
+ }
+
+ // return
+ return sHTML;
+}
+
+$(document).ready(function(){
+ var url = "http://localhost:6767/todo/"
+ function get_url(){
+ var ids = $(".ids").val()
+ if (ids === ""){
+ return url
+ }
+ return url + ids + "/"
+ }
+ function update(data){
+ var text = ""
+ text += "Status code: " + data['status_code'] +"<br>"
+ for (item in data['data']){
+ text += "<pre>" + FormatJSON(data['data'][item])+ "</pre>"
+ }
+ $("#content").html(text)
+ }
+ $('.post').click(function(){
+ var url = get_url()
+ data = $(".postbody").val()
+ $.post(url, {data:data}, update)
+ })
+ $('.put').click(function(){
+ var url = get_url()
+ data = $(".postbody").val()
+ $.ajax({url:url, type:"PUT", data:{data:data}, success:update})
+ })
+ $('.get').click(function(){
+ var url = get_url()
+ $.get(url, update)
+ })
+ $('.delete').click(function(){
+ var the_url = get_url()
+ if (the_url == url)
+ return
+ $.ajax(url, "DELETE", update)
+ })
+ });
+ </script>
+</head>
+<body>
+ Resource ID: <input type"text" class="ids" value="437b4bb7-55f5-441b-ba25-c7a4fd99442c" size=80 /><br>
+ <br>
+ Payload content:
+ <br>
+ <textarea class="postbody" cols=100 rows=8>[{"id": "437b4bb7-55f5-441b-ba25-c7a4fd99442c", "title": "watch more ds9"}]</textarea>
+ <br>
+ HTTP Methods:
+ <input type="button" class="post" value="Post">
+ <input type="button" class="put" value="Put">
+ <input type="button" class="get" value="Get">
+ <input type="button" class="delete" value="Delete">
+ <div id="content">
+ </div>
+</body>
+
Please sign in to comment.
Something went wrong with that request. Please try again.