Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving split char to request handler, rather than as a global

  • Loading branch information...
commit 84444a1fdd06d54c651afa8d12636a96d4d0bd25 1 parent e9dea12
@gone authored
View
13 brubeck/request_handling.py
@@ -579,11 +579,12 @@ def render(self, status_code=None, **kwargs):
return response
-MULTIPLE_ITEM_SEP = ','
+
class AutoAPIBase(JSONMessageHandler):
model = None
queries = None
+ multiple_item_sep = ','
###
### configuring input and output formats
@@ -680,7 +681,7 @@ def get(self, item_ids=""):
"""Handles read - either with a filter (item_ids) or a total list
"""
try:
- shields = self.read([v for v in item_ids.split(MULTIPLE_ITEM_SEP) if v])
+ shields = self.read([v for v in item_ids.split(self.multiple_item_sep) if v])
except FourOhFourException:
return self.render(status_code=404)
return self._create_response(shields)
@@ -717,7 +718,7 @@ def post(self, item_ids=""):
created, updated, failed = self.create(shields)
return self._create_response(updated, failed, created)
else:
- if not self.url_matches_body(item_ids.split(MULTIPLE_ITEM_SEP), shields):
+ if not self.url_matches_body(item_ids.split(self.multiple_item_sep), shields):
#TODO: add error message so client knows why the request failed
return self.render(status_code=400)
@@ -751,7 +752,7 @@ def put(self, item_ids):
shields, invalid = self._pre_alter_validation()
if invalid:
return self.render(status_code=400)
- if not self.url_matches_body(item_ids.split(MULTIPLE_ITEM_SEP), shields):
+ if not self.url_matches_body(item_ids.split(self.multiple_item_sep), shields):
#TODO: add error message so client knows why the request failed
return self.render(status_code=400)
successes, failures = self.update(shields)
@@ -761,7 +762,7 @@ def delete(self, item_ids):
""" Handles delete for 1 or many items. Since this doesn't take a postbody, and just
Item ids, pass those on directly to destroy
"""
- item_ids = item_ids.split(MULTIPLE_ITEM_SEP)
+ item_ids = item_ids.split(self.multiple_item_sep)
try:
successes, failures = self.destroy(item_ids)
except FourOhFourException:
@@ -992,7 +993,7 @@ def register_api(self, APIClass):
manifest_pattern = "/manifest.json"
self.add_route_rule(manifest_pattern, JsonSchemaMessageHandler)
- pattern = "/" + model_name + "/((?P<item_ids>[-\w\d%s]+)/|$)" % MULTIPLE_ITEM_SEP
+ pattern = "/" + model_name + "/((?P<item_ids>[-\w\d%s]+)/|$)" % APIClass.multiple_item_sep
self.add_route_rule(pattern, APIClass)
JsonSchemaMessageHandler.add_model(model)
View
143 demos/demo_apiable.html
@@ -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>
+ <input type"text" class="ids"><br>
+ <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">
+ <br>
+ try: [{"title":"watch more ds9"}]<br>
+ <textarea class="postbody" cols=100 rows=8>[]</textarea>
+
+ <div id="content">
+ </div>
+</body>
+
+
+
View
2  demos/demo_apiable.py
@@ -38,7 +38,7 @@ def get(self):
parameters essentially map to the variation in how `load_listitems` is
called.
"""
- return self.render_template('todos.html')
+ return self.render_template('demo_apiable.html')
###
Please sign in to comment.
Something went wrong with that request. Please try again.