# 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
                    data['error'] = "Some required fields are missing"
                    data['field_errors'] = form.errors
                    data['error_html'] = core.utils.build_readable_errors(form.errors)

            else:
                # Method not POST
                raise MethodUnsupported("This endpoint only accepts POSTs.")
        else:
            # Non-AJAX request.  Disallow for now.
            raise NonAjaxRequest( \
                "Remote API calls aren't allowed right now. " + \
                "This might change some day.")

    except NonAjaxRequest, e:
        logger.warning("Non-AJAX API call to %s" % (request.path))
        status = 403 # Forbidden
        data['error'] = "%s" % e

    except MethodUnsupported, error:
        logger.warning("API call to %s with unsupported method %s" % \
                    (request.path, request.method))
        status = 405 # Method not allowed
        data['error'] = "%s" % error

    except BadUsernameOrPassword, error:
        logger.warning("Authentication failure for user %s" % \
                    (request.user.username))
        status = 401 # unauthorized
        data['error'] = "%s" % error


# 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

                    data['error'] = "Some required fields are missing"
                    data['field_errors'] = form.errors
                    data['error_html'] = core.utils.build_readable_errors(form.errors)

            else:
                # Method not POST
                raise MethodUnsupported("This endpoint only accepts POSTs.")
        else:
            # Non-AJAX request.  Disallow for now.
            raise NonAjaxRequest( \
                "Remote API calls aren't allowed right now. " + \
                "This might change some day.")

    except NonAjaxRequest, e:
        logger.warning("Non-AJAX API call to %s" % (request.path))
        status = 403 # Forbidden
        data['error'] = "%s" % e

    except MethodUnsupported, error:
        logger.warning("API call to %s with unsupported method %s" % \
                    (request.path, request.method))
        status = 405 # Method not allowed
        data['error'] = "%s" % error

    except BadUsernameOrPassword, error:
        logger.warning("Authentication failure for user %s" % \
                    (request.user.username))
        status = 401 # unauthorized
        data['error'] = "%s" % error


    except UserAccountDisabled, error: