# Path: raggregate/models/user.py
# class User(Base):
#     __tablename__ = 'users'
#     id = Column(GUID, primary_key=True)
#     name = Column(Unicode(255), unique=True)
#     password = Column(UnicodeText)
#     email = Column(UnicodeText)
#     real_name = Column(UnicodeText)
#     temporary = Column(Boolean, default=False)
#     about_me = Column(UnicodeText)
#     picture = Column(GUID, ForeignKey('user_pictures.id'))
#     karma = Column(Integer, default=0)
#     notify_by_mail = Column(Boolean, default=True)
#     twitter_origination = Column(Boolean, default=False)
#     twitter_oauth_key = Column(UnicodeText)
#     twitter_oauth_secret = Column(UnicodeText)
#     facebook_origination = Column(Boolean, default=False)
#     facebook_user_id = Column(UnicodeText)
#     is_admin = Column(Boolean, default=False)
#     lost_password_token = Column(GUID)
#     password_token_claim_date = Column(DateTime(timezone=True))
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
#     added_by = Column(GUID)
# 
#     followed = relationship("User", secondary=follows, primaryjoin=id==follows.c.followed, secondaryjoin=id==follows.c.follower, backref="follows")
#     saved = relationship("Submission", secondary=users_saves)
#     picture_ref = relationship("UserPicture")
# 
#     def update_karma(self):
#         #@TODO: find out if we want to allow negative general karma scores
#         tally = 0
#         try:
#             for s in self.submissions:
#                 tally += s.points
#         except:
#             pass
#         try:
#             for c in self.comments:
#                 tally += c.points
#         except:
#             pass
#         if self.karma != tally:
#             self.karma = tally
#         return tally
# 
#     def display_name(self):
#         if self.real_name:
#             return self.real_name
#         else:
#             return self.name
# 
#     # need an adaptable default picture getter method
#     #def display_picture_filename(self):
#     #    if self.picture_ref:
#     #        return self.picture_ref.filename
#     #    else:
#     #        from raggregate import queries
#     #        return queries.get_default_picture_filename()
# 
#     def hash_pw(self, pw):
#         return crypt.encode(pw)
# 
#     def verify_pw(self, pw):
#         return crypt.check(self.password, pw)
# 
#     def is_user_admin(self):
#         # this is simplistic for now, but one day should use a real roles / permissions system
#         return self.is_admin
# 
#     def is_user_notified(self, target_id):
#         from raggregate.queries import notify
#         return notify.is_user_notified(self.id, target_id)
# 
#     def __init__(self, name, password, email = None, real_name = None,
#                   temporary = False, notify_by_mail = True):
#         self.name = name
#         self.password = self.hash_pw(password)
#         self.email = email
#         if real_name:
#             self.real_name = real_name
#         if temporary:
#             self.temporary = True
#         self.notify_by_mail = notify_by_mail
# 
# Path: raggregate/queries/users.py
# def get_user_by_id(id):
# def get_user_by_name(name):
# def get_user_by_email(email):
# def get_user_by_token(token):
# def is_user_allowed_admin_action(user_id, target_id, request = None, target_class = 'user_post',):
# def get_followed_users(id):
# def get_user_votes(user_id, vote_type, submission_id=None):
# def create_temp_user(initial_pw = None):
# def fuzzify_date(d):
# def create_user(**kwargs):
# def login_user(request, u, password, bypass_password = False):
# def add_user_picture(orig_filename, new_prefix, up_dir, image_file):
# def send_email_to_user(request, to_email, title, body):
# def send_lost_password_verify_email(request, user):
# def generate_new_password(request, user):


import sqlalchemy
import sqlahelper
        import urllib2
from raggregate.models.user import User
from raggregate.queries import users
from twython import Twython
def start_auth(request):
    app_twit = request.registry.app_twit
    auth_toks = app_twit.get_authentication_tokens()
    return auth_toks

def complete_auth(request, auth_toks):
    # create a twython object with our request-specific tokens
    # these tokens are used only to learn if the user accepted our request
    # for permissions.
    tmp_twit = Twython(twitter_token = request.registry.settings['twitter.app_key'],
                       twitter_secret = request.registry.settings['twitter.app_secret'],
                       oauth_token = auth_toks['oauth_token'],
                       oauth_token_secret = auth_toks['oauth_token_secret'])

    final_toks = tmp_twit.get_authorized_tokens()

    # create a session-permanent twython object containing the permanent tokens for this user.
    # this object must be used to read any data from the user's account.
    u_twit = Twython(twitter_token = request.registry.settings['twitter.app_key'],
                       twitter_secret = request.registry.settings['twitter.app_secret'],
                       oauth_token = final_toks['oauth_token'],
                       oauth_token_secret = final_toks['oauth_token_secret'])

    request.session['u_twit'] = u_twit

    username = "twit-{0}".format(final_toks['oauth_token'])
    screen_name = final_toks['screen_name']

    # check if user already exists; if not, please create
    try:

# Path: raggregate/models/user.py
# class User(Base):
#     __tablename__ = 'users'
#     id = Column(GUID, primary_key=True)
#     name = Column(Unicode(255), unique=True)
#     password = Column(UnicodeText)
#     email = Column(UnicodeText)
#     real_name = Column(UnicodeText)
#     temporary = Column(Boolean, default=False)
#     about_me = Column(UnicodeText)
#     picture = Column(GUID, ForeignKey('user_pictures.id'))
#     karma = Column(Integer, default=0)
#     notify_by_mail = Column(Boolean, default=True)
#     twitter_origination = Column(Boolean, default=False)
#     twitter_oauth_key = Column(UnicodeText)
#     twitter_oauth_secret = Column(UnicodeText)
#     facebook_origination = Column(Boolean, default=False)
#     facebook_user_id = Column(UnicodeText)
#     is_admin = Column(Boolean, default=False)
#     lost_password_token = Column(GUID)
#     password_token_claim_date = Column(DateTime(timezone=True))
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
#     added_by = Column(GUID)
# 
#     followed = relationship("User", secondary=follows, primaryjoin=id==follows.c.followed, secondaryjoin=id==follows.c.follower, backref="follows")
#     saved = relationship("Submission", secondary=users_saves)
#     picture_ref = relationship("UserPicture")
# 
#     def update_karma(self):
#         #@TODO: find out if we want to allow negative general karma scores
#         tally = 0
#         try:
#             for s in self.submissions:
#                 tally += s.points
#         except:
#             pass
#         try:
#             for c in self.comments:
#                 tally += c.points
#         except:
#             pass
#         if self.karma != tally:
#             self.karma = tally
#         return tally
# 
#     def display_name(self):
#         if self.real_name:
#             return self.real_name
#         else:
#             return self.name
# 
#     # need an adaptable default picture getter method
#     #def display_picture_filename(self):
#     #    if self.picture_ref:
#     #        return self.picture_ref.filename
#     #    else:
#     #        from raggregate import queries
#     #        return queries.get_default_picture_filename()
# 
#     def hash_pw(self, pw):
#         return crypt.encode(pw)
# 
#     def verify_pw(self, pw):
#         return crypt.check(self.password, pw)
# 
#     def is_user_admin(self):
#         # this is simplistic for now, but one day should use a real roles / permissions system
#         return self.is_admin
# 
#     def is_user_notified(self, target_id):
#         from raggregate.queries import notify
#         return notify.is_user_notified(self.id, target_id)
# 
#     def __init__(self, name, password, email = None, real_name = None,
#                   temporary = False, notify_by_mail = True):
#         self.name = name
#         self.password = self.hash_pw(password)
#         self.email = email
#         if real_name:
#             self.real_name = real_name
#         if temporary:
#             self.temporary = True
#         self.notify_by_mail = notify_by_mail
# 
# Path: raggregate/queries/users.py
# def get_user_by_id(id):
# def get_user_by_name(name):
# def get_user_by_email(email):
# def get_user_by_token(token):
# def is_user_allowed_admin_action(user_id, target_id, request = None, target_class = 'user_post',):
# def get_followed_users(id):
# def get_user_votes(user_id, vote_type, submission_id=None):
# def create_temp_user(initial_pw = None):
# def fuzzify_date(d):
# def create_user(**kwargs):
# def login_user(request, u, password, bypass_password = False):
# def add_user_picture(orig_filename, new_prefix, up_dir, image_file):
# def send_email_to_user(request, to_email, title, body):
# def send_lost_password_verify_email(request, user):
# def generate_new_password(request, user):


# Path: raggregate/login_adapters/twitter.py
import sqlalchemy
import sqlahelper
        import urllib2
from raggregate.models.user import User
from raggregate.queries import users
from twython import Twython

def start_auth(request):
    app_twit = request.registry.app_twit
    auth_toks = app_twit.get_authentication_tokens()
    return auth_toks

def complete_auth(request, auth_toks):
    # create a twython object with our request-specific tokens
    # these tokens are used only to learn if the user accepted our request
    # for permissions.
    tmp_twit = Twython(twitter_token = request.registry.settings['twitter.app_key'],
                       twitter_secret = request.registry.settings['twitter.app_secret'],
                       oauth_token = auth_toks['oauth_token'],
                       oauth_token_secret = auth_toks['oauth_token_secret'])

    final_toks = tmp_twit.get_authorized_tokens()

    # create a session-permanent twython object containing the permanent tokens for this user.
    # this object must be used to read any data from the user's account.
    u_twit = Twython(twitter_token = request.registry.settings['twitter.app_key'],
                       twitter_secret = request.registry.settings['twitter.app_secret'],
                       oauth_token = final_toks['oauth_token'],
                       oauth_token_secret = final_toks['oauth_token_secret'])

    request.session['u_twit'] = u_twit

    username = "twit-{0}".format(final_toks['oauth_token'])
    screen_name = final_toks['screen_name']

    # check if user already exists; if not, please create
    try:

        u = users.get_user_by_name(username)