# Path: della/user_manager/models.py
# class UserProfile(TimeStampMixin):
#     """
#     To manage users and stuff. From default `User` we will use `username`,
#     `email`, `password`, `first_name` and `last_name`.
#     """
#     bio = models.TextField(null=True)
#     address = models.TextField(null=True)
#     shipping_instructions = models.TextField(null=True)
#     is_enabled_exchange = models.BooleanField(default=False)
#     preferences = models.TextField(null=True)
#     fb_profile_url = models.URLField(null=True)
#     twitter_profile_url = models.URLField(null=True)
#     website_url = models.URLField(null=True)
#     wishlist_url = models.URLField(null=True)
#     avatar = models.ImageField(
#         upload_to=avatar_file_name, default='avatar.png')
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     santee = models.OneToOneField(User, on_delete=models.CASCADE,  related_name='santa', null=True)
# 
#     def __str__(self):
#         return self.user.username


from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.core.validators import RegexValidator, validate_email
from django.conf import settings
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Reset, Layout, Fieldset, HTML
from .models import UserProfile
    def clean_invite_code(self):
        error_message = 'Invalid invite code'
        invite_code = self.cleaned_data.get('invite_code')
        if not invite_code == settings.INVITE_CODE:
            raise forms.ValidationError(error_message)
        return invite_code

    def clean_email(self):
        error_message = 'An user with that email already exists'
        email = self.cleaned_data.get('email')
        if email and User.objects.filter(email=email).exists():
                raise forms.ValidationError(error_message)
        return email

    class Meta:
        model = User
        fields = ['email', 'username', ]


class UserProfileForm(ModelForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # make some fields optional
        self.fields['shipping_instructions'].required = False
        self.fields['fb_profile_url'].required = False
        self.fields['avatar'].required = False

# Path: della/user_manager/models.py
# class UserProfile(TimeStampMixin):
#     """
#     To manage users and stuff. From default `User` we will use `username`,
#     `email`, `password`, `first_name` and `last_name`.
#     """
#     bio = models.TextField(null=True)
#     address = models.TextField(null=True)
#     shipping_instructions = models.TextField(null=True)
#     is_enabled_exchange = models.BooleanField(default=False)
#     preferences = models.TextField(null=True)
#     fb_profile_url = models.URLField(null=True)
#     twitter_profile_url = models.URLField(null=True)
#     website_url = models.URLField(null=True)
#     wishlist_url = models.URLField(null=True)
#     avatar = models.ImageField(
#         upload_to=avatar_file_name, default='avatar.png')
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     santee = models.OneToOneField(User, on_delete=models.CASCADE,  related_name='santa', null=True)
# 
#     def __str__(self):
#         return self.user.username


# Path: della/user_manager/forms.py
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.core.validators import RegexValidator, validate_email
from django.conf import settings
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Reset, Layout, Fieldset, HTML
from .models import UserProfile

    def clean_invite_code(self):
        error_message = 'Invalid invite code'
        invite_code = self.cleaned_data.get('invite_code')
        if not invite_code == settings.INVITE_CODE:
            raise forms.ValidationError(error_message)
        return invite_code

    def clean_email(self):
        error_message = 'An user with that email already exists'
        email = self.cleaned_data.get('email')
        if email and User.objects.filter(email=email).exists():
                raise forms.ValidationError(error_message)
        return email

    class Meta:
        model = User
        fields = ['email', 'username', ]


class UserProfileForm(ModelForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # make some fields optional
        self.fields['shipping_instructions'].required = False
        self.fields['fb_profile_url'].required = False
        self.fields['avatar'].required = False

        self.fields['twitter_profile_url'].required = False