3 # Copyright 2008 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Implements the server-side RPCs for the My Hangouts application.
19 Provides a simple RPC framework for making mostly abstract Python
20 function calls from JavaScript. Provides a few simple functions for
21 the My Hangouts application.
23 This application uses the lower level Datastore API to make datastore
24 calls and perform queries.
27 __author__
= 'Kevin Gibbs'
34 from google
.appengine
.api
import datastore
35 from google
.appengine
.api
import users
36 from google
.appengine
.ext
import webapp
37 from google
.appengine
.ext
.webapp
.util
import run_wsgi_app
38 from google
.appengine
.ext
.webapp
import template
40 # Renders the main template.
41 class MainPage(webapp
.RequestHandler
):
43 query
= datastore
.Query('User')
44 current_user
= users
.get_current_user()
46 for user
in query
.Get(100):
47 user_list
[user
['user'].email()] = user
['user']
49 user_list
[current_user
.email()] = current_user
50 display_user
= self
.request
.get('user')
52 display_user
= users
.User(display_user
+ os
.environ
['AUTH_DOMAIN'])
54 display_user
= current_user
55 if len(user_list
) > 0 and (not display_user
or
56 display_user
.email() not in user_list
):
57 display_user
= user_list
.values()[0]
59 'users': user_list
.values(),
60 'current_user': current_user
,
61 'display_user': display_user
,
62 'login_url': users
.create_login_url(self
.request
.uri
),
63 'logout_url': users
.create_logout_url(self
.request
.uri
),
65 path
= os
.path
.join(os
.path
.dirname(__file__
), "myhangouts.html")
66 self
.response
.out
.write(template
.render(path
, template_values
))
68 # This handler allows the functions defined in the RPCHandler class to
69 # be called automatically by remote code.
70 class RPCHandler(webapp
.RequestHandler
):
72 action
= self
.request
.get('action')
76 arg
= self
.request
.get('arg' + str(arg_counter
))
79 args
+= (simplejson
.loads(arg
),);
82 result
= getattr(self
, action
)(*args
)
83 self
.response
.out
.write(simplejson
.dumps((result
)))
85 # The RPCs exported to JavaScript follow here:
87 def AddLocation(self
, latd
, longd
, name
):
88 user
= users
.get_current_user()
89 query
= datastore
.Query('User')
90 query
['user ='] = user
91 if (query
.Count() == 0):
92 user_entity
= datastore
.Entity('User')
93 user_entity
['user'] = user
94 datastore
.Put(user_entity
)
95 location
= datastore
.Entity("Location")
96 location
['user'] = user
97 location
['latd'] = latd
98 location
['longd'] = longd
99 location
['name'] = name
100 datastore
.Put(location
)
101 # Add a few additional attributes so that they are available to
103 location
['user'] = location
['user'].email()
104 location
['key'] = str(location
.key())
107 def RemoveLocation(self
, key
):
108 datastore
.Delete(datastore
.Key(key
.encode('utf-8')))
111 def GetLocations(self
, user_email
=None):
113 user
= users
.User(user_email
)
115 user
= users
.get_current_user()
116 query
= datastore
.Query('Location')
119 query
['user ='] = user
121 for location
in query
.Get(100):
122 # Add a few additional attributes so that they are available to
124 location
['user'] = location
['user'].email()
125 location
['key'] = str(location
.key())
126 locations
+= [location
]
130 application
= webapp
.WSGIApplication([
132 ('/rpc', RPCHandler
),
134 run_wsgi_app(application
)
136 if __name__
== '__main__':