Created
May 18, 2010 16:32
-
-
Save geokat/405195 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import urllib | |
| from django.utils import simplejson | |
| from google.appengine.ext import webapp | |
| from google.appengine.ext.webapp.util import run_wsgi_app | |
| from google.appengine.ext import db | |
| from google.appengine.api import urlfetch | |
| from google.appengine.ext.webapp import template | |
| from gaesessions import get_current_session | |
| # Are we running on appspot or localhost? | |
| ON_LOCALHOST = ("Development" == os.environ["SERVER_SOFTWARE"][0:11]) | |
| if ON_LOCALHOST: | |
| if os.environ["SERVER_PORT"] == "80": | |
| BASE_URL = "localhost" | |
| else: | |
| BASE_URL = "localhost:%s" % os.environ["SERVER_PORT"] | |
| else: | |
| BASE_URL = "any2rpx.appspot.com" | |
| FIRST_LOGIN_MSG = "Hello %s, thanks for signing in! You can logout using the link below." | |
| REPEAT_LOGIN_MSG = "Hello %s, welcome back. You can logout using the link below." | |
| FIRST_ANON_MSG = "Welcome to any2rpx demo site. This seems to be your first visit. You can login with your openid using the widget below." | |
| REPEAT_ANON_MSG = "Hello anonymous visitor, welcome back. You can login with your openid using the widget below." | |
| RPX_LOGIN_WIDGET = '<iframe src="http://any2rpx.rpxnow.com/openid/embed?token_url=http%3A%2F%2F' + BASE_URL + '%2Frpx_token_handler" scrolling="no" frameBorder="no" allowtransparency="true" style="width:400px;height:240px"></iframe>' | |
| DSQ_EMBED_CODE = """ | |
| <div id="disqus_thread"></div> | |
| <script type="text/javascript"> | |
| /** | |
| * var disqus_identifier; [Optional but recommended: Define a unique identifier (e.g. post id or slug) for this thread] | |
| */ | |
| (function() { | |
| var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; | |
| dsq.src = 'http://any2rpx.disqus.com/embed.js'; | |
| (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); | |
| })(); | |
| </script> | |
| <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript=any2rpx">comments powered by Disqus.</a></noscript> | |
| <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a> | |
| """ | |
| DSQ_COMMENT_COUNT_CODE = """ | |
| <script type="text/javascript"> | |
| //<![CDATA[ | |
| (function() { | |
| var links = document.getElementsByTagName('a'); | |
| var query = '?'; | |
| for(var i = 0; i < links.length; i++) { | |
| if(links[i].href.indexOf('#disqus_thread') >= 0) { | |
| query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&'; | |
| } | |
| } | |
| document.write('<script charset="utf-8" type="text/javascript" src="http://disqus.com/forums/any2rpx/get_num_replies.js' + query + '"></' + 'script>'); | |
| })(); | |
| //]]> | |
| </script> | |
| """ | |
| DSQ_PERMALINK = "#disqus_thread" | |
| class User(db.Model): | |
| email = db.EmailProperty() | |
| display_name = db.TextProperty() | |
| first_time = db.BooleanProperty() | |
| class MainPage(webapp.RequestHandler): | |
| def get(self): | |
| session = get_current_session() | |
| template_values = dict(login_widget = RPX_LOGIN_WIDGET) | |
| if session.is_active(): | |
| if session.has_key("openid"): | |
| # user is logged in | |
| user = User.get_by_key_name(session["openid"]) | |
| if user.first_time: | |
| greeting = FIRST_LOGIN_MSG % user.display_name | |
| user.first_time = False | |
| user.put() | |
| else: | |
| greeting = REPEAT_LOGIN_MSG % user.display_name | |
| template_values['logout_url'] = "/logout" | |
| template_values['logout_url_linktext'] = "Logout" | |
| else: | |
| greeting = REPEAT_ANON_MSG | |
| else: | |
| greeting = FIRST_ANON_MSG | |
| # doesn't matter what we store, just store something to | |
| # start the session | |
| session['anonymous'] = True | |
| template_values['greeting'] = greeting | |
| template_values['dsq_embed_code'] = DSQ_EMBED_CODE | |
| path = os.path.join(os.path.dirname(__file__), 'index.html') | |
| self.response.out.write(template.render(path, template_values)) | |
| class RPXTokenHandler(webapp.RequestHandler): | |
| def post(self): | |
| token = self.request.get('token') | |
| url = "http://rpxnow.com/api/v2/auth_info" | |
| args = { | |
| 'format': 'json', | |
| 'apiKey': '9eef4c7ebf92c0ba88b43c3da8b08a6ea0a4ceb0', | |
| 'token': token | |
| } | |
| r_json = urlfetch.fetch(url=url, | |
| payload=urllib.urlencode(args), | |
| method=urlfetch.POST, | |
| headers={'Content-Type':'application/x-www-form-urlencoded'}) | |
| r = simplejson.loads(r_json.content) | |
| # close any active session the user has since he is trying to login | |
| session = get_current_session() | |
| if session.is_active(): | |
| session.terminate() | |
| if r['stat'] == "ok": | |
| # extract some useful fields | |
| info = r['profile'] | |
| openid = info['identifier'] | |
| email = info.get('email', '') | |
| try: | |
| display_name = info['displayName'] | |
| except KeyError: | |
| display_name = email.partition('@')[0] | |
| user = User.get_or_insert(openid, email=email, display_name=display_name, first_time=True) | |
| # start a session for the user (old one was terminated) | |
| session['openid'] = openid | |
| else: | |
| session['anonymous'] = True | |
| self.redirect("/") | |
| class LogoutPage(webapp.RequestHandler): | |
| def get(self): | |
| session = get_current_session() | |
| if session.has_key('openid'): | |
| session.terminate() | |
| # start the new anonymous session | |
| session['anonymous'] = True | |
| self.redirect("/") | |
| application = webapp.WSGIApplication( | |
| [('/', MainPage), | |
| ('/rpx_token_handler', RPXTokenHandler), | |
| ('/logout', LogoutPage), | |
| ]) | |
| def main(): | |
| 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