# Path: candidates/forms.py
# class UserForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('first_name', 'last_name', 'email')
# 
# class CandidateForm(forms.ModelForm):
#     class Meta:
#         model = Candidate
#         fields = ('phone', 'resume', 'resume_url', 'website')
# 
# Path: candidates/models.py
# class Candidate(models.Model):
#     LEVEL_CHOICES = (
#         ("IN", "Intern"),
#         ("NG", "New grad"),
#         ("CL", "Career level"),
#         ("SR", "Senior"),
#         ("PR", "Principal"),
#     )
#     STATUS_CHOICES = (
#         ("NEW", "New"),
#         ("PHO", "Phone Screening"),
#         ("ON", "Onsite Interview"),
#         ("OF", "Offer"),
#         ("CL", "Closed"),
#         ("AC", "Accepted"),
#     )
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     level = models.CharField(max_length=3, choices=LEVEL_CHOICES, blank=True, null=True)
#     status = models.CharField(max_length=3, choices=STATUS_CHOICES, default="NEW")
#     phone = models.CharField(max_length=50)
#     referrer = models.CharField(max_length=100, blank=True, null=True)
# 
#     resume = models.FileField(blank=True,
#                               null=True,
#                               upload_to=determine_upload_location,
#                               validators=[FileExtensionValidator(['pdf', 'txt', 'doc', 'docx'])])
#     resume_url = models.URLField(blank=True, null=True)
#     website = models.URLField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.name()
# 
#     def name(self):
#         n = []
#         if self.user.first_name: n.append( self.user.first_name )
#         if self.user.last_name: n.append( self.user.last_name )
#         return " ".join(n) if n else str(self.user)


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _
from .forms import UserForm, CandidateForm
from .models import Candidate



@login_required
def update_candidate(request):
    if request.method == 'POST':
        print(request.FILES)
        user_form = UserForm(request.POST, instance=request.user)
        candidate_form = CandidateForm(request.POST, request.FILES,
                                       instance=request.user.candidate)
        if user_form.is_valid() and candidate_form.is_valid():
            user_form.save()
            candidate_form.save()
            messages.success(request,
                             _('Your profile was successfully updated!'))
            return redirect('account-profile')
        else:
            messages.error(request, _('Please correct the error below.'))
    else:
        user_form = UserForm(instance=request.user)

        candidate, created = Candidate.objects.get_or_create(user=request.user)


# Path: candidates/forms.py
# class UserForm(forms.ModelForm):
#     class Meta:
#         model = User
#         fields = ('first_name', 'last_name', 'email')
# 
# class CandidateForm(forms.ModelForm):
#     class Meta:
#         model = Candidate
#         fields = ('phone', 'resume', 'resume_url', 'website')
# 
# Path: candidates/models.py
# class Candidate(models.Model):
#     LEVEL_CHOICES = (
#         ("IN", "Intern"),
#         ("NG", "New grad"),
#         ("CL", "Career level"),
#         ("SR", "Senior"),
#         ("PR", "Principal"),
#     )
#     STATUS_CHOICES = (
#         ("NEW", "New"),
#         ("PHO", "Phone Screening"),
#         ("ON", "Onsite Interview"),
#         ("OF", "Offer"),
#         ("CL", "Closed"),
#         ("AC", "Accepted"),
#     )
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     level = models.CharField(max_length=3, choices=LEVEL_CHOICES, blank=True, null=True)
#     status = models.CharField(max_length=3, choices=STATUS_CHOICES, default="NEW")
#     phone = models.CharField(max_length=50)
#     referrer = models.CharField(max_length=100, blank=True, null=True)
# 
#     resume = models.FileField(blank=True,
#                               null=True,
#                               upload_to=determine_upload_location,
#                               validators=[FileExtensionValidator(['pdf', 'txt', 'doc', 'docx'])])
#     resume_url = models.URLField(blank=True, null=True)
#     website = models.URLField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.name()
# 
#     def name(self):
#         n = []
#         if self.user.first_name: n.append( self.user.first_name )
#         if self.user.last_name: n.append( self.user.last_name )
#         return " ".join(n) if n else str(self.user)


# Path: candidates/views.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _
from .forms import UserForm, CandidateForm
from .models import Candidate




@login_required
def update_candidate(request):
    if request.method == 'POST':
        print(request.FILES)
        user_form = UserForm(request.POST, instance=request.user)
        candidate_form = CandidateForm(request.POST, request.FILES,
                                       instance=request.user.candidate)
        if user_form.is_valid() and candidate_form.is_valid():
            user_form.save()
            candidate_form.save()
            messages.success(request,
                             _('Your profile was successfully updated!'))
            return redirect('account-profile')
        else:
            messages.error(request, _('Please correct the error below.'))
    else:
        user_form = UserForm(instance=request.user)

        candidate, created = Candidate.objects.get_or_create(user=request.user)


        candidate_form = CandidateForm(instance=candidate)