REST controller example: microBLOG

3 views
Skip to first unread message

Miguel Sanchez

unread,
Apr 17, 2008, 6:52:49 PM4/17/08
to Google App Engine
I'm testing GAE and i'm very interested in use REST controllers, so i
have coded a small example:

http://github.com/miguel/rest-microblog/

TODOs:
* Make template methods.
* Better error handling.
* Extend the RequestHandler class with the aux methods (get, post,
getAction,...), so you only have to define the REST methods (list,
show,...) inside of the controller.

I hope you like it :)

Miguel.

Miguel Sanchez

unread,
Apr 18, 2008, 3:17:19 AM4/18/08
to Google App Engine
You can download all the source code directly from here:

http://github.com/miguel/rest-microblog/tarball/master

for test:
cd miguel-rest-microblog*
GAEpath/dev_app_server.py .

I use this route to access the controller:
('/messages(|/.*)',MessagesController

and then in MessagesController there are the methods needed for REST
actions. In the get and post methods the program selects what action
has to be done:

#shows all the messages
#route: GET /messages
def list(self):
messages = Message.all()
messages.order("-date")
self.render('templates/index.html', messages=messages)

#shows the given message
#route: GET /messages/message_name
def show(self,message):
self.render("templates/showMessage.html", message)

#shows the new form
#route: GET /messages/new
def new(self):
self.render("templates/newMessage.html")

#shows the edit form
#route: GET /messages/:message_name/edit
def edit(self,message):
self.render("templates/editMessage.html", message)

#creates the message and puts it into the datastore
#route: POST /messages
def create(self):
if self.request.get('name'):
message=Message(name = self.request.get('name') ) #new
message
if users.get_current_user():
message.user = users.get_current_user()
message.content = self.request.get('content')
message.put()
self.redirect('/messages/'+message.name) #redirects to
list
else:
self.response.out.write('<div class="message"><b>ERROR:</
b> message name is empty.</div>')

#updates the given message
#route: PUT /messages/message_id
def update(self,message):
message.name = self.request.get('name')
message.content = self.request.get('content')
message.put()
self.redirect('/messages/'+message.name) #redirects to show

#deletes the given message
#route: PUT /messages/message_id
def destroy(self,message):
message.delete();
self.redirect('/messages') #redirects to list

I don't have an account, so i can't test in the real environment.

Miguel.
Reply all
Reply to author
Forward
0 new messages