"""Implementation of profile views."""

from django.contrib.auth.models import User
from imager_images.models import Photo, Album
from django.views.generic import TemplateView, UpdateView
from django.shortcuts import redirect
from imager_profile.forms import EditProfileForm
from imager_profile.models import ImagerProfile


class HomeView(TemplateView):
    """View for the home page."""

    template_name = 'imagersite/home.html'

    def get_context_data(self, name=''):
        """Get context for home view."""
        return {'name': name}


class ProfileView(TemplateView):
    """Class implementation of profile view."""

    template_name = '../templates/profile.html'

    def get_context_data(self, username=None):
        """View for profile page."""
        if not username:
            username = self.request.user.username
        user_profile = User.objects.get(username=username).profile
        photos = Photo.objects.all().filter(photographer=user_profile.user_id)
        albums = Album.objects.all().filter(owner=user_profile)
        data = {
            'public_photos': photos.filter(published='PUBLIC'),
            'private_photos': photos.filter(published='PRIVATE'),
            'shared_photos': photos.filter(published='SHARED'),
            'photo_count': len(photos),
            'album_count': len(albums)
        }
        return {'user_profile': user_profile, 'data': data}


class EditProfileView(UpdateView):
    """Edit profile view."""

    model = ImagerProfile
    form_class = EditProfileForm
    template_name = '../templates/edit_profile_form.html'

    def get(self, request, **kwargs):
        self.object = User.objects.get(username=self.request.user)
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return redirect('profile', username=self.object.user.username)

@@ -185,46 +185,8 @@
 iew%0A
-from django.shortcuts import redirect%0A
 from
@@ -278,16 +278,98 @@
 Profile%0A
+from django.http import HttpResponseRedirect%0Afrom django.urls import reverse_lazy%0A
 %0A%0Aclass 
@@ -1493,20 +1493,22 @@
  %22%22%22
-Edit
+Update
  profile
  vie
@@ -1495,37 +1495,32 @@
 %22%22Update profile
- view
 .%22%22%22%0A%0A    model 
@@ -1517,62 +1517,29 @@
     
-model = ImagerProfile%0A    form_class = EditProfileForm
+login_required = True
 %0A   
@@ -1570,18 +1570,18 @@
 plates/e
-d
 i
+d
 t_profil
@@ -1597,95 +1597,194 @@
 ml'%0A
-%0A
     
-def get(self, request, **kwargs):%0A        self.object = User.objects.get(username=
+success_url = reverse_lazy('profile')%0A    form_class = EditProfileForm%0A    model = ImagerProfile%0A%0A    def get_object(self):%0A        %22%22%22Define what profile to edit.%22%22%22%0A        return 
 self
@@ -1800,431 +1800,463 @@
 user
-)%0A  
+.profile%0A%0A
     
- 
+def
  form_
-class = self.get_form_class()%0A        form = self.get_form(form_class)%0A        context = self.get_context_data(object=self.object, form=form)%0A        return self.render_to_response(context)%0A%0A    def form_valid(self, form):%0A        self.object = form.save(commit=False)%0A        self.object.user = self.request.user%0A        self.object.save()%0A        return redirect('profile', username=self.object.user.username
+valid(self, form):%0A        %22%22%22If form post is successful, set the object's owner.%22%22%22%0A        self.object = form.save()%0A        self.object.user.first_name = form.cleaned_data%5B'First Name'%5D%0A        self.object.user.last_name = form.cleaned_data%5B'Last Name'%5D%0A        self.object.user.email = form.cleaned_data%5B'Email'%5D%0A        self.object.user.save()%0A        self.object.save()%0A        return HttpResponseRedirect(self.get_success_url()
 )%0A

edit view, kinda, not really