# Path: yithlibraryserver/user/utils.py
# def register_or_update(request, provider, user_id, info, default_url='/'):
#     provider_key = get_provider_key(provider)
#     user = user_from_provider_id(request.db, provider, user_id)
#     if user is None:
# 
#         new_info = {'provider': provider, provider_key: user_id}
#         for attribute in ('screen_name', 'first_name', 'last_name', 'email'):
#             if attribute in info:
#                 new_info[attribute] = info[attribute]
#             else:
#                 new_info[attribute] = ''
# 
#         request.session['user_info'] = new_info
#         if 'next_url' not in request.session:
#             request.session['next_url'] = default_url
#         return HTTPFound(location=request.route_path('register_new_user'))
#     else:
#         changes = {'last_login': request.datetime_service.utcnow()}
# 
#         ga = request.google_analytics
#         if ga.is_in_session():
#             if not ga.is_stored_in_user(user):
#                 changes.update(ga.get_user_attr(ga.show_in_session()))
#             ga.clean_session()
# 
#         update_user(request.db, user, info, changes)
# 
#         if 'next_url' in request.session:
#             next_url = request.session['next_url']
#             del request.session['next_url']
#         else:
#             next_url = default_url
# 
#         request.session['current_provider'] = provider
#         remember_headers = remember(request, str(user['_id']))
#         return HTTPFound(location=next_url, headers=remember_headers)


from yithlibraryserver.user.utils import register_or_update
# Yith Library Server is a password storage server.
# Copyright (C) 2013 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# This file is part of Yith Library Server.
#
# Yith Library Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.



def facebook_callback(request, user_id, info):
    return register_or_update(request, 'facebook', user_id, info,
                              request.route_path('home'))


def google_callback(request, user_id, new_info):

# Path: yithlibraryserver/user/utils.py
# def register_or_update(request, provider, user_id, info, default_url='/'):
#     provider_key = get_provider_key(provider)
#     user = user_from_provider_id(request.db, provider, user_id)
#     if user is None:
# 
#         new_info = {'provider': provider, provider_key: user_id}
#         for attribute in ('screen_name', 'first_name', 'last_name', 'email'):
#             if attribute in info:
#                 new_info[attribute] = info[attribute]
#             else:
#                 new_info[attribute] = ''
# 
#         request.session['user_info'] = new_info
#         if 'next_url' not in request.session:
#             request.session['next_url'] = default_url
#         return HTTPFound(location=request.route_path('register_new_user'))
#     else:
#         changes = {'last_login': request.datetime_service.utcnow()}
# 
#         ga = request.google_analytics
#         if ga.is_in_session():
#             if not ga.is_stored_in_user(user):
#                 changes.update(ga.get_user_attr(ga.show_in_session()))
#             ga.clean_session()
# 
#         update_user(request.db, user, info, changes)
# 
#         if 'next_url' in request.session:
#             next_url = request.session['next_url']
#             del request.session['next_url']
#         else:
#             next_url = default_url
# 
#         request.session['current_provider'] = provider
#         remember_headers = remember(request, str(user['_id']))
#         return HTTPFound(location=next_url, headers=remember_headers)


# Path: yithlibraryserver/sna_callbacks.py
from yithlibraryserver.user.utils import register_or_update

# Yith Library Server is a password storage server.
# Copyright (C) 2013 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# This file is part of Yith Library Server.
#
# Yith Library Server is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Yith Library Server is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.



def facebook_callback(request, user_id, info):
    return register_or_update(request, 'facebook', user_id, info,
                              request.route_path('home'))


def google_callback(request, user_id, new_info):

    return register_or_update(request, 'google', user_id, new_info,