Skip to content

Instantly share code, notes, and snippets.

@mnbi
Created October 9, 2010 09:27
Show Gist options
  • Save mnbi/618051 to your computer and use it in GitHub Desktop.
Save mnbi/618051 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
import datetime
import logging
from handler.view import ViewHandler
from handler.edit import EditHandler
from handler.delete import DeleteHandler
class Settings(db.Model):
user = db.UserProperty()
blog_id = db.StringProperty()
last_modified = db.DateTimeProperty(auto_now_add=True)
def get_settings(user_id):
key = db.Key.from_path('Settings', user_id)
settings = db.get(key)
if not settings:
settings = Settings(key_name=user_id)
return settings
class MainHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
self.response.out.write("<html>")
self.response.out.write("<head><title>User Settings</title></head>")
self.response.out.write("<body>")
# current settings
self.response.out.write("<h1>User Settings</h1>")
if user:
self.response.out.write("<ul>")
self.response.out.write("<li>User name: " + user.nickname() + "</li>")
settings = get_settings(user.user_id())
if settings.blog_id:
self.response.out.write("<li>Blog ID: " + settings.blog_id + "</li>")
if settings.last_modified:
self.response.out.write("<li>Last modified: " + str(settings.last_modified) + "</li>")
self.response.out.write("</ul>")
# form
self.response.out.write("""
<form action='/settings' method='post'>
<div>Blog ID: <input name='blog_id' type='text' cols='60' /></div>
<div><input type='submit' value='Save Settings' /></div>
</form>
"""
)
# navigation
self.response.out.write("<div>")
if not user:
self.response.out.write("<a href='" + users.create_login_url(self.request.uri) + "'>Login</a>")
else:
self.response.out.write("<a href='" + users.create_logout_url(self.request.uri) + "'>Logout</a>")
self.response.out.write("</div>")
self.response.out.write("</body></html>")
class SettingsHandler(webapp.RequestHandler):
def post(self):
user = users.get_current_user()
if user:
settings = get_settings(user.user_id())
settings.blog_id = self.request.get('blog_id')
settings.last_modified = datetime.datetime.now()
settings.put()
self.redirect('/')
else:
logging.debug("redirect to login panel.")
self.redirect(users.create_login_url('/'))
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/settings', SettingsHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment