# Path: yithlibraryserver/user/models.py
# class User(dict):
# 
#     def __unicode__(self):
#         result = self.get('screen_name', '')
#         if result:
#             return result
# 
#         result = ' '.join([self.get('first_name', ''),
#                            self.get('last_name', '')])
#         result = result.strip()
#         if result:
#             return result
# 
#         result = self.get('email', '')
#         if result:
#             return result
# 
#         return text_type(self['_id'])
# 
#     # py3 compatibility
#     def __str__(self):
#         return self.__unicode__()


import bson
from pyramid.security import authenticated_userid, unauthenticated_userid
from pyramid.httpexceptions import HTTPFound
from yithlibraryserver.user.models import User
# 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 get_user(request):
    user_id = unauthenticated_userid(request)
    if user_id is None:
        return user_id

    try:
        user = request.db.users.find_one(bson.ObjectId(user_id))
    except bson.errors.InvalidId:
        return None

    return User(user)


def assert_authenticated_user_is_registered(request):
    user_id = authenticated_userid(request)
    try:
        user = request.db.users.find_one(bson.ObjectId(user_id))
    except bson.errors.InvalidId:
        raise HTTPFound(location=request.route_path('register_new_user'))
    else:

# Path: yithlibraryserver/user/models.py
# class User(dict):
# 
#     def __unicode__(self):
#         result = self.get('screen_name', '')
#         if result:
#             return result
# 
#         result = ' '.join([self.get('first_name', ''),
#                            self.get('last_name', '')])
#         result = result.strip()
#         if result:
#             return result
# 
#         result = self.get('email', '')
#         if result:
#             return result
# 
#         return text_type(self['_id'])
# 
#     # py3 compatibility
#     def __str__(self):
#         return self.__unicode__()


# Path: yithlibraryserver/user/security.py
import bson
from pyramid.security import authenticated_userid, unauthenticated_userid
from pyramid.httpexceptions import HTTPFound
from yithlibraryserver.user.models import User

# 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 get_user(request):
    user_id = unauthenticated_userid(request)
    if user_id is None:
        return user_id

    try:
        user = request.db.users.find_one(bson.ObjectId(user_id))
    except bson.errors.InvalidId:
        return None

    return User(user)


def assert_authenticated_user_is_registered(request):
    user_id = authenticated_userid(request)
    try:
        user = request.db.users.find_one(bson.ObjectId(user_id))
    except bson.errors.InvalidId:
        raise HTTPFound(location=request.route_path('register_new_user'))
    else:

        return User(user)