{"id":584,"date":"2009-01-12T11:18:39","date_gmt":"2009-01-12T15:18:39","guid":{"rendered":"http:\/\/www.dr-chuck.com\/wordpress\/?p=584"},"modified":"2011-12-17T12:29:18","modified_gmt":"2011-12-17T16:29:18","slug":"funny-use-of-google","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2009\/01\/funny-use-of-google\/","title":{"rendered":"Funny Use of Google"},"content":{"rendered":"<p>\nOver the weekend I was writing some code in Google AppEngine and had a need to loop through all of the incoming request parameters so automatically update the contents of a model when there were request parameters which had the same name as the model attributes.<\/p>\n<p>\nWhen I wrote the very first version of the AE chapters for the class &#8211; I had this bit about dumping parameters in one chapter.  In the rewrite to send the book to O&#8217;Reilly, I felt that this detail was too much so I cut it out about a month ago.  I also cleaned up old copies of the chapter on my hard drive and on the appenginelearn.com site.<\/p>\n<p>\nSo on Saturday, I am sitting here peeved that I had this all figured out once and had even written it up once &#8211; exactly what I wanted &#8211; and then lost it.<\/p>\n<p>\nI figured that I had researched this once before &#8211; so I could figure it out again.  I remember that there was no real documentation and I had to write stuff like dir() to hunt through methods.  It was painful but I figured I could do it in an hour.<\/p>\n<p>\nSo I typed the following into Google:<\/p>\n<p>\n<b>appengine looping through request variables<\/b><\/p>\n<p>\nI looked at the text of the second hit and it looked promising.   Then I noticed the word &#8220;Michigan&#8221; in the text and then I noticed that it was *my chapter*.  The lost chapter.  So I clicked on the link (the PDF) and scrolled down and had my answer in about 45 seconds!<\/p>\n<p>\nI had forgotten to remove the copy on my Michigan personal AFS space &#8211; which I had used while teaching the class &#8211; Google had found it, indexed it and knew *somehow* that it was what I needed.<\/p>\n<p>\nSweet!<\/p>\n<p><!--more--><\/p>\n<p>\nHere is the code I used to loop throught the attributes from a request and update model values automatically:<\/p>\n<pre>\n# Loop through the request keys and see if they can be put\n# into the model\ndef modelload(self, org, req, prefix = None):\nself.debug(\"ORM LOAD \"+str(org.__class__))\nfor key in req.params.keys():\nvalue = self.web.request.get(key)\nthetype = self.modeltype(org, key)\nif ( thetype == \"none\" and prefix != None ) :\nif ( not key.startswith(prefix) ) : continue\nkey = key[len(prefix):]\nthetype = self.modeltype(org, key)\nif ( thetype == \"none\" ) : continue\nself.debug(key+\" (\"+thetype+\") = \"+value)\nsetattr(org,key,value)\ndef modeltype(self, obj, key):\ntry:\nattr = str(type(getattr(obj.__class__, key)))\nexcept :\nreturn \"none\"\nif attr.find(\"ext.db.StringProperty\") > 0 : return \"string\"\nif attr.find(\"ext.db.ReferenceProperty\") > 0 : return \"reference\"\nif attr.find(\"ext.db.DateTimeProperty\")  > 0: return \"datetime\"\nif attr.find(\"ext.db.IntegerProperty\")  > 0: return \"int\"\nreturn \"none\"\n<\/pre>\n<p>Here is the entire bit of code for folks who might be interested.<\/p>\n<p>\n<a href=\"http:\/\/code.google.com\/p\/cloudcollab\/source\/browse\/trunk\/contrib\/drchuck\/wiscrowd\/imsglobal\/lti.py\" target=\"_new\">http:\/\/code.google.com\/p\/cloudcollab\/source\/browse\/trunk\/contrib\/drchuck\/wiscrowd\/imsglobal\/lti.py<\/a><\/p>\n<p>\n","protected":false},"excerpt":{"rendered":"<p>Over the weekend I was writing some code in Google AppEngine and had a need to loop through all of the incoming request parameters so automatically update the contents of a model when there were request parameters which had the same name as the model attributes. When I wrote the very first version of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":1,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":2699,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/584\/revisions\/2699"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}