Source

sketch / sketch / users.py

Full commit

import sketch
import oauth

from google.appengine.ext import db

class Logins(sketch.Model):
    username = db.StringProperty()


class User(sketch.Model):
    username = db.StringProperty(required=True)
    name = db.StringProperty()
    email = db.EmailProperty()
    picture = db.StringProperty()
    password = db.StringProperty()
    admin = db.BooleanProperty(default = False)

    tw_token = db.StringProperty()
    tw_secret = db.StringProperty()
    tw_username = db.StringProperty()
    tw_picture = db.StringProperty()
    tw_name = db.StringProperty()
    tw_id = db.IntegerProperty()

    fb_token = db.StringProperty()
    fb_secret = db.StringProperty()
    fb_username = db.StringProperty()
    fb_picture = db.StringProperty()

    @classmethod
    def create(self, username, email, password, image = None):
        user = User(
            username = name,
            email = email,
            image = image,
            password = password
        )
        return user.put()

    @classmethod
    def fetch(self, username = None, email = None, tw_username = None, fb_username = None):
        query_field = None
        if email:
            query_field = "email"
            query_value = email
        if username:
            query_field = "username"
            query_value = username
        if tw_username:
            query_field = "tw_username"
            query_value = tw_username
        if fb_username:
            query_field = "fb_username"
            query_value = fb_username
        if not query_field:
            raise ModelException, "Require one of email, username or linked account to query user"

        query = db.GqlQuery("SELECT * from User where %s = :1" % query_field, query_value)
        result = query.fetch(1)

        if result:
            return result[0]
        else:
            return False



class LoginHandler(sketch.BaseController):
    tw_consumer_key = "CdkFQYSXbud586od8N0Q"
    tw_consumer_secret = "5e9b3a9wtXwx8ClHhHEKw5xTMjSKiNxLY9V3bm7ko"
    tw_callback_url = "%s/login/twitter/callback"

    def create_user(self, profile):
        return True

    def twitter_login(self):
        client = oauth.TwitterClient(self.tw_consumer_key, self.tw_consumer_secret, self.tw_callback_url % self.request.host_url)
        logging.info("Twitter redir URL: %s" % client.get_authorization_url())
        # self.redirect(client.get_authorization_url())
        return client.get_authorization_url()

    def twitter_callback(self):
        client = oauth.TwitterClient(self.tw_consumer_key, self.tw_consumer_secret, self.tw_callback_url % self.request.host_url)
        auth_token = self.request.get("oauth_token")
        auth_verifier = self.request.get("oauth_verifier")
        user_info = client.get_user_info(auth_token, auth_verifier=auth_verifier)
        return user_info

    def get(self, arg):
        if arg == "/twitter":
            url = self.twitter_login()
            return self.redirect(url)
        elif arg =="/twitter/callback":
            content = self.twitter_callback()
            if content.has_key('username'):
                user = User.fetch(tw_username = content['username'])
                logging.info(user)
                # existing user
                if user:
                    self.session['user'] = user.username
                    self.session['auth'] = True
                    self.session.save()
                    return self.redirect('/posts')
                # new user
                else:
                    user = User(
                        username = content['username'],
                        tw_username = content['username'],
                        picture = content['picture'],
                        tw_picture = content['picture'],
                        name = content['name'],
                        tw_secret = content['secret'],
                        tw_token = content['token'],
                        tw_id = content['id']
                    )
                    r = user.put()
                    if r:
                        self.session['user'] = user.username
                        self.session['auth'] = True
                        self.session.save()
                        return self.redirect('/posts?newuser')
                    else:
                        return self.redirect('/?error=new_user_error')
            else:
                return self.redirect('/index?loginerror')
        elif arg == "/clear":
            self.session.invalidate()
            self.redirect('/?loggedout')
        else:
            return self.render('index', {"error": "not implemented"})