# Path: core/exceptions.py
# class MethodUnsupported(Exception):
#     """Exception thrown when an HTTP request method isn't supported by the REST
#        API"""
#     pass
# 
# class NonAjaxRequest(Exception):
#     """Exception thrown when an API call is made that is not an AJAX request
#        and we don't want to allow this kind of (potentially) cross-site 
#        call
#     
#     """
#     pass
# 
# Path: users/exceptions.py
# class BadUsernameOrPassword(Exception):
#    def __init__(self, msg='Bad username or password.'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserAccountDisabled(Exception):
#    def __init__(self, msg='User account has been disabled.'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserUsernameExists(Exception):
#    def __init__(self, msg='The username is taken, please try another'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserEmailExists(Exception):
#    def __init__(self, msg='This email already exists, please try another'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class NoFacebookUser(Exception):
#     def __init__(self, msg='No Facebook user detected.'):
#         self.msg = msg
#     
#     def __str__(self):
#         return self.msg


import json
import core.utils
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.conf import settings
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
from fbapi import facebook 
from core.exceptions import MethodUnsupported, NonAjaxRequest
from users.exceptions import BadUsernameOrPassword, UserAccountDisabled, \
                             UserUsernameExists, UserEmailExists, \
                             NoFacebookUser
from models import UserProfile
from models import User
from models import ActivationKeyValue
from forms import LoginForm, RegisterForm, InviteForm, ActivateUnactivatedForm
from registration.models import RegistrationProfile
                    facebook_user_id=fb_user['uid'])

            except UserProfile.DoesNotExist:
                #they're not, so we need to create them and move em along
                fb_graph = facebook.GraphAPI(fb_user['access_token'])
                fb_profile = fb_graph.get_object("me")
                username = fb_profile['first_name'] + fb_profile['last_name']
                password = fb_profile['id']
                base_user = User.objects.create_user(username=username,\
                                                     password=password, email='na')
                user_profile = UserProfile(user=base_user,\
                                               facebook_user_id=fb_profile['id'])
                user_profile.save()

            finally:
                # Log the user in without authenticating them
                # See http://zcentric.com/2010/05/12/django-fix-for-user-object-has-no-attribute-backend/
                user_profile.user.backend = \
                    'django.contrib.auth.backends.ModelBackend'
                login(request, user_profile.user)
                #logger.debug("User %s logged in." % (user_profile.user))

                # Set up our return data
                data['username'] = user_profile.user.username 
                data['uri'] = '/api/%s/users/%s/' % (output_format, \
                    user_profile.user.username)

        else:
            raise NoFacebookUser


# Path: core/exceptions.py
# class MethodUnsupported(Exception):
#     """Exception thrown when an HTTP request method isn't supported by the REST
#        API"""
#     pass
# 
# class NonAjaxRequest(Exception):
#     """Exception thrown when an API call is made that is not an AJAX request
#        and we don't want to allow this kind of (potentially) cross-site 
#        call
#     
#     """
#     pass
# 
# Path: users/exceptions.py
# class BadUsernameOrPassword(Exception):
#    def __init__(self, msg='Bad username or password.'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserAccountDisabled(Exception):
#    def __init__(self, msg='User account has been disabled.'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserUsernameExists(Exception):
#    def __init__(self, msg='The username is taken, please try another'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class UserEmailExists(Exception):
#    def __init__(self, msg='This email already exists, please try another'):
#        self.msg = msg 
# 
#    def __str__(self):
#        return self.msg
# 
# class NoFacebookUser(Exception):
#     def __init__(self, msg='No Facebook user detected.'):
#         self.msg = msg
#     
#     def __str__(self):
#         return self.msg


# Path: users/views.py
import json
import core.utils
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.conf import settings
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
from fbapi import facebook 
from core.exceptions import MethodUnsupported, NonAjaxRequest
from users.exceptions import BadUsernameOrPassword, UserAccountDisabled, \
                             UserUsernameExists, UserEmailExists, \
                             NoFacebookUser
from models import UserProfile
from models import User
from models import ActivationKeyValue
from forms import LoginForm, RegisterForm, InviteForm, ActivateUnactivatedForm
from registration.models import RegistrationProfile

                    facebook_user_id=fb_user['uid'])

            except UserProfile.DoesNotExist:
                #they're not, so we need to create them and move em along
                fb_graph = facebook.GraphAPI(fb_user['access_token'])
                fb_profile = fb_graph.get_object("me")
                username = fb_profile['first_name'] + fb_profile['last_name']
                password = fb_profile['id']
                base_user = User.objects.create_user(username=username,\
                                                     password=password, email='na')
                user_profile = UserProfile(user=base_user,\
                                               facebook_user_id=fb_profile['id'])
                user_profile.save()

            finally:
                # Log the user in without authenticating them
                # See http://zcentric.com/2010/05/12/django-fix-for-user-object-has-no-attribute-backend/
                user_profile.user.backend = \
                    'django.contrib.auth.backends.ModelBackend'
                login(request, user_profile.user)
                #logger.debug("User %s logged in." % (user_profile.user))

                # Set up our return data
                data['username'] = user_profile.user.username 
                data['uri'] = '/api/%s/users/%s/' % (output_format, \
                    user_profile.user.username)

        else:
            raise NoFacebookUser


    except NoFacebookUser as detail: