# Path: profiles/models.py
# class Profile(User):
#     objects = UserManager()
#     _extensions_imported = False
# 
#     @classmethod
#     def remove_field(cls, f_name):
#         # Removes the field form local fields list
#         cls._meta.local_fields = [f for f in cls._meta.local_fields if f.name != f_name]
# 
#         # Removes the field setter if exists
#         if hasattr(cls, f_name):
#             delattr(cls, f_name)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ProfileAdmin)
# 
#     @classmethod
#     def register_extensions(cls, *extensions):
#         if not hasattr(cls, '_profile_extensions'):
#             cls._profile_extensions = set()
# 
#         here = cls.__module__.split('.')[:-1]
#         here_path = '.'.join(here + ['extensions'])
# 
#         for ext in extensions:
#             if ext in cls._profile_extensions:
#                 continue
# 
#             try:
#                 if isinstance(ext, basestring):
#                     try:
#                         fn = get_callable(ext + '.register', False)
#                     except ImportError:
#                         fn = get_callable('%s.%s.register' % (here_path, ext), False)
#                 # Not a string, so take our chances and just try to access "register"
#                 else:
#                     fn = ext.register
# 
#                 cls.register_extension(fn)
#                 cls._profile_extensions.add(ext)
#             except Exception:
#                 raise
# 
#     def save(self, *args, **kwargs):
#         # Remove leading and trailing whitespace from name.
#         try:
#             self.first_name = self.first_name.strip()
#             self.last_name = self.last_name.strip()
#         except AttributeError:
#             pass
# 
#         if not self.username:
#             self.username = generate_id(first_name=self.first_name,
#                                         last_name=self.last_name, email=self.email)
#         super(Profile, self).save(*args, **kwargs)
# 
#     def get_full_name(self):
#         full_name = super(Profile, self).get_full_name()
#         if not full_name:
#             return self.username
#         return full_name
# 
#     def get_profile(self):
#         return self
# 
# Path: profiles/signals.py

# 
# Path: profiles/utils.py
# def class_view_decorator(function_decorator):
#     """Convert a function based decorator into a class based decorator usable
#     on class based Views.
# 
#     Follows the general idea from https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-the-class.
# 
#     Can't subclass the `View` as it breaks inheritance (super in particular),
#     so we monkey-patch instead.
#     """
# 
#     def simple_decorator(View):
#         View.dispatch = method_decorator(function_decorator)(View.dispatch)
#         return View
# 
#     return simple_decorator
# 
# def generate_id(first_name='', last_name='', email=''):
#     valid_id = False
#     gen = _id_generator(first_name, last_name, email)
#     test_name = gen.next()
#     while valid_id is False:
#         try:
#             User.objects.get(username=test_name)
#         except User.DoesNotExist:
#             valid_id = True
#         else:
#             test_name = gen.next()
#     return test_name


from django.conf import settings
from django.contrib import messages
from django.contrib.auth import login, authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse, get_callable
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView, CreateView, UpdateView
from profiles.models import Profile
from profiles.signals import user_registered, user_updated
from profiles.utils import class_view_decorator, generate_id
            from .forms import ProfileForm

        self.authenticate_new_user(self.username, self.password)

        return super(RegisterView, self).form_valid(form)

    def get_password(self, data):
        # try to get the password
        for fname in ['password', 'password1', 'password2', 'plain_password']:
            if fname in data:
                return data[fname]
        return None

    def get_username(self, data):
        if not 'username' in data:
            # auto generate a username
            generate_kwargs = {}
            for fname in ['first_name', 'last_name', 'email']:
                if fname in data:
                    generate_kwargs[fname] = data[fname]
            return generate_id(**generate_kwargs)

    def send_signals(self, form, user):
        user_registered.send(sender=self.__class__, user=user, request=self.request, form=form)

    def update_user_object(self, user):
        if self.password:
            user.set_password(self.password)
        return user



# Path: profiles/models.py
# class Profile(User):
#     objects = UserManager()
#     _extensions_imported = False
# 
#     @classmethod
#     def remove_field(cls, f_name):
#         # Removes the field form local fields list
#         cls._meta.local_fields = [f for f in cls._meta.local_fields if f.name != f_name]
# 
#         # Removes the field setter if exists
#         if hasattr(cls, f_name):
#             delattr(cls, f_name)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ProfileAdmin)
# 
#     @classmethod
#     def register_extensions(cls, *extensions):
#         if not hasattr(cls, '_profile_extensions'):
#             cls._profile_extensions = set()
# 
#         here = cls.__module__.split('.')[:-1]
#         here_path = '.'.join(here + ['extensions'])
# 
#         for ext in extensions:
#             if ext in cls._profile_extensions:
#                 continue
# 
#             try:
#                 if isinstance(ext, basestring):
#                     try:
#                         fn = get_callable(ext + '.register', False)
#                     except ImportError:
#                         fn = get_callable('%s.%s.register' % (here_path, ext), False)
#                 # Not a string, so take our chances and just try to access "register"
#                 else:
#                     fn = ext.register
# 
#                 cls.register_extension(fn)
#                 cls._profile_extensions.add(ext)
#             except Exception:
#                 raise
# 
#     def save(self, *args, **kwargs):
#         # Remove leading and trailing whitespace from name.
#         try:
#             self.first_name = self.first_name.strip()
#             self.last_name = self.last_name.strip()
#         except AttributeError:
#             pass
# 
#         if not self.username:
#             self.username = generate_id(first_name=self.first_name,
#                                         last_name=self.last_name, email=self.email)
#         super(Profile, self).save(*args, **kwargs)
# 
#     def get_full_name(self):
#         full_name = super(Profile, self).get_full_name()
#         if not full_name:
#             return self.username
#         return full_name
# 
#     def get_profile(self):
#         return self
# 
# Path: profiles/signals.py

# 
# Path: profiles/utils.py
# def class_view_decorator(function_decorator):
#     """Convert a function based decorator into a class based decorator usable
#     on class based Views.
# 
#     Follows the general idea from https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-the-class.
# 
#     Can't subclass the `View` as it breaks inheritance (super in particular),
#     so we monkey-patch instead.
#     """
# 
#     def simple_decorator(View):
#         View.dispatch = method_decorator(function_decorator)(View.dispatch)
#         return View
# 
#     return simple_decorator
# 
# def generate_id(first_name='', last_name='', email=''):
#     valid_id = False
#     gen = _id_generator(first_name, last_name, email)
#     test_name = gen.next()
#     while valid_id is False:
#         try:
#             User.objects.get(username=test_name)
#         except User.DoesNotExist:
#             valid_id = True
#         else:
#             test_name = gen.next()
#     return test_name


# Path: profiles/views.py
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import login, authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse, get_callable
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView, CreateView, UpdateView
from profiles.models import Profile
from profiles.signals import user_registered, user_updated
from profiles.utils import class_view_decorator, generate_id
            from .forms import ProfileForm


        self.authenticate_new_user(self.username, self.password)

        return super(RegisterView, self).form_valid(form)

    def get_password(self, data):
        # try to get the password
        for fname in ['password', 'password1', 'password2', 'plain_password']:
            if fname in data:
                return data[fname]
        return None

    def get_username(self, data):
        if not 'username' in data:
            # auto generate a username
            generate_kwargs = {}
            for fname in ['first_name', 'last_name', 'email']:
                if fname in data:
                    generate_kwargs[fname] = data[fname]
            return generate_id(**generate_kwargs)

    def send_signals(self, form, user):
        user_registered.send(sender=self.__class__, user=user, request=self.request, form=form)

    def update_user_object(self, user):
        if self.password:
            user.set_password(self.password)
        return user



@class_view_decorator(login_required)