Comment out the 'loadtest' backend in the 'counter' backend sample so that it does...
[gae-samples.git] / geochat / geochat.py
blobc591beb276612891df07080bf1553a0483a6340a
2 # Copyright 2008 Google Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """The main Geochat application.
18 Contains the MainHandler, which handles root requests to the server, along
19 with several other template-driven pages that don't have any significant DB
20 interaction.
22 MainHandler: Handles requests to /
23 SettingsHandler: Handles requests to /settings
24 HelpHandler: Handles requests to /help
25 """
27 import datetime
28 import logging
29 import os
30 import time
32 from google.appengine.api import users
33 from google.appengine.ext import db
34 from google.appengine.ext import webapp
35 from google.appengine.ext.webapp import template
36 from google.appengine.ext.webapp.util import run_wsgi_app
38 import datamodel
39 import json
40 import settings
43 class MainHandler(webapp.RequestHandler):
45 """Handles requests to /
47 MainHandler handles requests for the server root, presenting the main user
48 interface for Geochat. It relies on the geochat.html template, with most
49 of the heavy lifting occuring client-side through JavaScript linked there.
50 """
52 def get(self):
54 template_data = {}
56 user = users.get_current_user()
57 if user:
59 user_settings = settings.get(user)
60 if user_settings == None:
61 self.redirect('/settings')
62 return
64 template_data = {
65 'auth_url': users.CreateLogoutURL(self.request.uri),
66 'auth_text': 'Sign out',
67 'user_email': user.email(),
68 'user_nickname': user.nickname(),
69 'default_location': user_settings.default_location,
70 'initial_latitude': 37.4221,
71 'initial_longitude': -122.0837,
73 else:
74 template_data = {
75 'auth_url': users.CreateLoginURL(self.request.uri),
76 'auth_text': 'Sign in',
77 'user_email': '',
78 'user_nickname': '',
79 'initial_latitude': 37.4221,
80 'initial_longitude': -122.0837,
83 template_path = os.path.join(os.path.dirname(__file__), 'geochat.html')
84 self.response.headers['Content-Type'] = 'text/html'
85 self.response.out.write(template.render(template_path, template_data))
88 class SettingsHandler(webapp.RequestHandler):
90 """Handles requests to /settings
92 SettingsHandler handles requests to /settings, serving up the template
93 found in settings.html on GET requests, and saving user settings on
94 POST requests.
95 """
97 def get(self):
99 template_data = {}
101 user = users.get_current_user()
102 if user == None:
103 self.redirect(users.CreateLoginURL(self.request.uri))
104 return
106 user_settings = settings.get(user)
107 if user_settings:
108 template_data = {
109 'auth_url': users.CreateLogoutURL(self.request.uri),
110 'auth_text': 'Sign out',
111 'user_email': user.email(),
112 'user_nickname': user.nickname(),
113 'default_location': user_settings.default_location,
114 'default_zoom': user_settings.default_zoom,
116 else:
117 template_data = {
118 'auth_url': users.CreateLogoutURL(self.request.uri),
119 'auth_text': 'Sign out',
120 'user_email': user.email(),
121 'user_nickname': user.nickname(),
124 template_path = os.path.join(os.path.dirname(__file__), 'settings.html')
125 self.response.headers['Content-Type'] = 'text/html'
126 self.response.out.write(template.render(template_path, template_data))
128 def post(self):
130 user = users.get_current_user()
131 if user == None:
132 self.redirect(users.CreateLoginURL(self.request.uri))
133 return
135 location = self.request.get('location')
136 user_settings = settings.get(user)
138 if user_settings:
139 user_settings.default_location = location
140 user_settings.put()
141 else:
142 user_settings = settings.new(user, location)
144 self.redirect('/')
147 class HelpHandler(webapp.RequestHandler):
149 """Handles requests to /help
151 HelpHandler handles requests to /settings, serving up the template
152 found in help.html.
155 def get(self):
157 template_data = {}
159 user = users.get_current_user()
160 if user:
161 template_data = {
162 'auth_url': users.CreateLogoutURL(self.request.uri),
163 'auth_text': 'Sign out',
164 'user_email': user.email(),
166 else:
167 template_data = {
168 'auth_url': users.CreateLoginURL(self.request.uri),
169 'auth_text': 'Sign in',
170 'user_email': '',
173 template_path = os.path.join(os.path.dirname(__file__), 'help.html')
174 self.response.headers['Content-Type'] = 'text/html'
175 self.response.out.write(template.render(template_path, template_data))
178 if __name__ == '__main__':
179 application = webapp.WSGIApplication([('/', MainHandler),
180 ('/settings', SettingsHandler),
181 ('/help', HelpHandler),], debug = True)
183 run_wsgi_app(application)