<gh_stars>0
from django.shortcuts import render,redirect
from django.contrib import messages
from .forms import UserRegistrationForm,UserUpdateForm,ProfileUpdateForm
from django.contrib.auth.decorators import login_required
def register(request):
  if request.method == 'POST':
    form = UserRegistrationForm(request.POST)
    if form.is_valid():
      form.save()
      username = form.cleaned_data.get('username')
      messages.success(request,f'Your account has been created successfuly')
      return redirect('login')
  else:
    form = UserRegistrationForm()
  
  return render(request,'register.html',{"form":form})

@login_required
def profile(request):
  if request.method == 'POST':
    u_form = UserUpdateForm(request.POST,instance=request.user)
    p_form = ProfileUpdateForm(request.POST,request.FILES,instance=request.user.profiles)
    
    if u_form.is_valid() and p_form.is_valid():
      u_form.save()
      p_form.save()
      
      messages.success(request,f'Profile Updated successfuly')
      return redirect('profile')

  else:
    u_form = UserUpdateForm(instance=request.user)
    p_form = ProfileUpdateForm(instance=request.user.profiles)
  context = {
    'u_form':u_form,
    'p_form':p_form,
  }
  return render(request,'profile.html',context)
