from django.shortcuts import render
from .forms import Profile_Form


IMAGE_FILE_TYPES = ['png', 'jpg', 'jpeg']

def create_profile(request):
    form = Profile_Form()
    if request.method == 'POST':
        form = Profile_Form(request.POST, request.FILES)
        if form.is_valid():
            user_pr = form.save(commit=False)
            user_pr.display_picture = request.FILES['display_picture']
            file_type = user_pr.display_picture.url.split('.')[-1]
            file_type = file_type.lower()
            if file_type not in IMAGE_FILE_TYPES:
                return render(request, 'profileapp/error.html')
            user_pr.save()
            return render(request, 'profileapp/details.html', {'user_pr': user_pr})
    context = {"form": form,}
    return render(request, 'profileapp/create.html', context)