# Path: pycon/profile/forms.py
# class ProfileForm(forms.ModelForm):
#     
#     class Meta:
#         model = Profile
#     
#     def __init__(self, *args, **kwargs):
#         super(ProfileForm, self).__init__(*args, **kwargs)
#         
#         self.fields["first_name"].required = True
#         self.fields["last_name"].required = True
#         
#         self.fields.keyOrder = [
#             "first_name",
#             "last_name",
#             "phone",
#         ]


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Profile
from .forms import ProfileForm




@login_required
def profile_edit(request):
    
    next = request.GET.get("next")
    profile, created = Profile.objects.get_or_create(
        user=request.user,
        defaults={
            "first_name": request.user.first_name,
            "last_name": request.user.last_name,
        }
    )
    
    if request.method == "POST":
        form = ProfileForm(request.POST, instance=profile)
        if form.is_valid():
            profile = form.save()
            request.user.first_name = form.cleaned_data["first_name"]
            request.user.last_name = form.cleaned_data["last_name"]
            messages.add_message(request, messages.SUCCESS,
                "Successfully updated profile."
            )
            if next:
                return redirect(next)
    else:

# Path: pycon/profile/forms.py
# class ProfileForm(forms.ModelForm):
#     
#     class Meta:
#         model = Profile
#     
#     def __init__(self, *args, **kwargs):
#         super(ProfileForm, self).__init__(*args, **kwargs)
#         
#         self.fields["first_name"].required = True
#         self.fields["last_name"].required = True
#         
#         self.fields.keyOrder = [
#             "first_name",
#             "last_name",
#             "phone",
#         ]


# Path: pycon/profile/views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .models import Profile
from .forms import ProfileForm





@login_required
def profile_edit(request):
    
    next = request.GET.get("next")
    profile, created = Profile.objects.get_or_create(
        user=request.user,
        defaults={
            "first_name": request.user.first_name,
            "last_name": request.user.last_name,
        }
    )
    
    if request.method == "POST":
        form = ProfileForm(request.POST, instance=profile)
        if form.is_valid():
            profile = form.save()
            request.user.first_name = form.cleaned_data["first_name"]
            request.user.last_name = form.cleaned_data["last_name"]
            messages.add_message(request, messages.SUCCESS,
                "Successfully updated profile."
            )
            if next:
                return redirect(next)
    else:

        form = ProfileForm(instance=profile)