Created
January 23, 2009 23:56
-
-
Save Arachnid/51269 to your computer and use it in GitHub Desktop.
This file contains 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 base | |
import config | |
import model | |
import simploid | |
import urllib | |
from openid.extensions import sreg | |
from google.appengine.ext.db import djangoforms | |
from django import newforms as forms | |
class RegisterForm(forms.Form): | |
nickname = forms.CharField() | |
email = forms.CharField() | |
session = forms.CharField(widget=forms.HiddenInput) | |
def createUser(form, openid_url): | |
data = form.clean_data | |
profile_key = model.Nickname.getUniqueName(data['nickname']) | |
user = model.User(key_name=openid_url, nickname=data['nickname'], | |
email=data['email'], profile_key=profile_key) | |
user.put() | |
return user | |
def finishLogin(handler, user): | |
handler.user = user | |
if 'return_url' in handler.session_args: | |
handler.redirect(handler.session_args['return_url']) | |
else: | |
handler.redirect('/') | |
handler.delete_session() | |
class LoginHandler(base.BaseHandler, simploid.LoginHandler): | |
TEMPLATE='login.html' | |
PATH='/openid/login' | |
def beginLogin(self): | |
self.session_args['return_url'] = self.request.headers.get('Referer', '/') | |
super(LoginHandler, self).beginLogin() | |
def openidError(self, message, ex): | |
super(LoginHandler, self).openidError(message, ex) | |
def getExtensions(self, openid_url): | |
extensions = [] | |
if not model.User.get_by_key_name(openid_url): | |
extensions.append(sreg.SRegRequest(required=['nickname', 'email'])) | |
return extensions | |
class FinishHandler(base.BaseHandler, simploid.FinishHandler): | |
PATH='/openid/finish' | |
def success(self, response): | |
openid_url = response.endpoint.claimed_id | |
user = model.User.get_by_key_name(openid_url) | |
if not user: | |
sreg_data = sreg.SRegResponse.fromSuccessResponse(response) | |
form_data = dict(sreg_data) | |
form_data['session'] = self.request.get('session') | |
form = RegisterForm(form_data) | |
if not form.is_valid(): | |
self.redirect('/openid/register?%s' % urllib.urlencode(form_data)) | |
self.session_args['openid_url'] = openid_url | |
self.save_session() | |
return | |
else: | |
user = createUser(form, openid_url) | |
finishLogin(self, user) | |
def failure(self, response, message=None): | |
super(FinishHandler, self).failure(response) | |
def cancelled(self, response): | |
super(FinishHandler, self).cancelled(response) | |
class RegisterHandler(base.BaseHandler, simploid.Handler): | |
TEMPLATE = 'register_user.html' | |
PATH='/openid/register' | |
def showRegisterForm(self, form): | |
view = self.getView() | |
view['form'] = form | |
view.render() | |
def get(self): | |
openid_url = self.session_args['openid_url'] | |
user = model.User.get_by_key_name(openid_url) | |
if user: | |
finishLogin(self, user) | |
else: | |
if 'nickname' in self.request.GET or 'email' in self.request.GET: | |
form = RegisterForm(self.request.GET) | |
if form.is_valid() and openid_url: | |
user = createUser(form, openid_url) | |
finishLogin(self, user) | |
return | |
else: | |
form = RegisterForm(initial={'session': self.request.GET['session']}) | |
self.showRegisterForm(form) | |
class LogoutHandler(base.BaseHandler): | |
PATH='/openid/logout' | |
def get(self): | |
del self.user | |
self.redirect(self.request.headers.get('Referer', '/')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment