# Path: authenticating/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     timezone = serializers.CharField()
#     membership = MembershipSerializer()
# 
#     class Meta:
#         model = User
#         fields = ('email', 'verified', 'timezone', 'membership')
# 
# class MembershipSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Membership
#         fields = ('admin', 'family_name', 'email', 'verified')
# 
# Path: authenticating/models.py
# class User(AbstractBaseUser):
#     PasswordsDontMatch = PasswordsDontMatch
#     IncorrectPassword = IncorrectPassword
# 
#     objects = UserManager()
# 
#     id = models.UUIDField(primary_key=True,
#                           unique=True,
#                           editable=False,
#                           default=uuid4)
# 
#     email = models.EmailField(unique=True)
#     verified = models.BooleanField(default=False)
#     verify_key = models.UUIDField(default=uuid4, editable=False, unique=True)
#     timezone = TimeZoneField(default=get_default_timezone)
#     is_staff = models.BooleanField(default=False)
#     is_active = models.BooleanField(default=True)
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = []
# 
#     def get_full_name(self):
#         return self.get_short_name()
# 
#     def get_short_name(self):
#         return self.email
# 
#     def has_perm(self, *args, **kwargs):
#         return self.is_staff
# 
#     def has_module_perms(self, *args):
#         return self.is_staff
# 
#     def activate_timezone(self):
#         timezone.activate(self.timezone)
# 
#     def send_verification_email(self):
#         if self.verified:
#             return False
#         VerifyUserEmail(user=self).send()
# 
#     def get_verify_link(self):
#         key = str(self.verify_key)
#         path = reverse('verify', args=[key])
#         return expand_url_path(path)
# 
#     def change_password(self, old, new):
#         new, new_copy = new
#         if not new == new_copy:
#             raise self.PasswordsDontMatch
# 
#         if not self.check_password(old):
#             raise self.IncorrectPassword
# 
#         self.set_password(new)
# 
#         self.save()
#         return self
# 
# class Membership(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     family = models.ForeignKey(Family, on_delete=models.CASCADE)
#     admin = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return '{0} Membership'.format(self.family.__unicode__())
# 
#     @property
#     def family_name(self):
#         return self.family.name
# 
#     @property
#     def email(self):
#         return self.user.email
# 
#     @property
#     def verified(self):
#         return self.user.verified
# 
# class Family(models.Model):
#     objects = FamilyManager()
# 
#     name = models.CharField(max_length=120)
#     members = models.ManyToManyField(User, through='Membership')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def invite_user_to_family(self, email=None):
#         user = User.objects.create(email=email, is_active=False)
#         Membership(user=user, family=self, admin=False).save()
#         return user.save()
# 
#     class Meta:
#         verbose_name_plural = 'Families'


from django.shortcuts import render, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import View
from django.contrib.auth import authenticate, login, update_session_auth_hash
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.decorators import list_route
from rest_framework import status
from authenticating.forms import UserCreationForm
from authenticating.serializers import UserSerializer, MembershipSerializer
from authenticating.models import (User,
                                   Membership,
                                   Family)
                            status=status.HTTP_401_UNAUTHORIZED)
        except user.PasswordsDontMatch:
            return Response('Passwords do not match',
                            status=status.HTTP_400_BAD_REQUEST)

        update_session_auth_hash(request, user)
        return Response('password updated')

    @list_route(methods=['post'], url_path='family')
    def create_family(self, request, **kwargs):
        name = request.data.get('name', None)

        if Membership.objects.filter(user=request.user).exists():
            return Response('User already in family', status=status.HTTP_400_BAD_REQUEST)

        if not name:
            return Response('Name required', status=status.HTTP_400_BAD_REQUEST)

        Family.objects.create_from_user(user=request.user, name=name)

        return Response('Family created', status=201)

    @list_route(methods=['post', 'get'], url_path='membership')
    def membership(self, request, **kwargs):
        if request.method == 'POST':
            email = request.data.get('email', None)

            if not email:
                return Response('email required', status=400)


# Path: authenticating/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     timezone = serializers.CharField()
#     membership = MembershipSerializer()
# 
#     class Meta:
#         model = User
#         fields = ('email', 'verified', 'timezone', 'membership')
# 
# class MembershipSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Membership
#         fields = ('admin', 'family_name', 'email', 'verified')
# 
# Path: authenticating/models.py
# class User(AbstractBaseUser):
#     PasswordsDontMatch = PasswordsDontMatch
#     IncorrectPassword = IncorrectPassword
# 
#     objects = UserManager()
# 
#     id = models.UUIDField(primary_key=True,
#                           unique=True,
#                           editable=False,
#                           default=uuid4)
# 
#     email = models.EmailField(unique=True)
#     verified = models.BooleanField(default=False)
#     verify_key = models.UUIDField(default=uuid4, editable=False, unique=True)
#     timezone = TimeZoneField(default=get_default_timezone)
#     is_staff = models.BooleanField(default=False)
#     is_active = models.BooleanField(default=True)
# 
#     USERNAME_FIELD = 'email'
#     REQUIRED_FIELDS = []
# 
#     def get_full_name(self):
#         return self.get_short_name()
# 
#     def get_short_name(self):
#         return self.email
# 
#     def has_perm(self, *args, **kwargs):
#         return self.is_staff
# 
#     def has_module_perms(self, *args):
#         return self.is_staff
# 
#     def activate_timezone(self):
#         timezone.activate(self.timezone)
# 
#     def send_verification_email(self):
#         if self.verified:
#             return False
#         VerifyUserEmail(user=self).send()
# 
#     def get_verify_link(self):
#         key = str(self.verify_key)
#         path = reverse('verify', args=[key])
#         return expand_url_path(path)
# 
#     def change_password(self, old, new):
#         new, new_copy = new
#         if not new == new_copy:
#             raise self.PasswordsDontMatch
# 
#         if not self.check_password(old):
#             raise self.IncorrectPassword
# 
#         self.set_password(new)
# 
#         self.save()
#         return self
# 
# class Membership(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     family = models.ForeignKey(Family, on_delete=models.CASCADE)
#     admin = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return '{0} Membership'.format(self.family.__unicode__())
# 
#     @property
#     def family_name(self):
#         return self.family.name
# 
#     @property
#     def email(self):
#         return self.user.email
# 
#     @property
#     def verified(self):
#         return self.user.verified
# 
# class Family(models.Model):
#     objects = FamilyManager()
# 
#     name = models.CharField(max_length=120)
#     members = models.ManyToManyField(User, through='Membership')
# 
#     def __unicode__(self):
#         return self.name
# 
#     def invite_user_to_family(self, email=None):
#         user = User.objects.create(email=email, is_active=False)
#         Membership(user=user, family=self, admin=False).save()
#         return user.save()
# 
#     class Meta:
#         verbose_name_plural = 'Families'


# Path: authenticating/views.py
from django.shortcuts import render, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import View
from django.contrib.auth import authenticate, login, update_session_auth_hash
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.decorators import list_route
from rest_framework import status
from authenticating.forms import UserCreationForm
from authenticating.serializers import UserSerializer, MembershipSerializer
from authenticating.models import (User,
                                   Membership,
                                   Family)

                            status=status.HTTP_401_UNAUTHORIZED)
        except user.PasswordsDontMatch:
            return Response('Passwords do not match',
                            status=status.HTTP_400_BAD_REQUEST)

        update_session_auth_hash(request, user)
        return Response('password updated')

    @list_route(methods=['post'], url_path='family')
    def create_family(self, request, **kwargs):
        name = request.data.get('name', None)

        if Membership.objects.filter(user=request.user).exists():
            return Response('User already in family', status=status.HTTP_400_BAD_REQUEST)

        if not name:
            return Response('Name required', status=status.HTTP_400_BAD_REQUEST)

        Family.objects.create_from_user(user=request.user, name=name)

        return Response('Family created', status=201)

    @list_route(methods=['post', 'get'], url_path='membership')
    def membership(self, request, **kwargs):
        if request.method == 'POST':
            email = request.data.get('email', None)

            if not email:
                return Response('email required', status=400)


            if User.objects.filter(email=email).exists():