# -*- coding: utf-8 -*-
# Import the reverse lookup function
from django.core.urlresolvers import reverse

# view imports
from django.views.generic import DetailView
from django.views.generic import RedirectView
from django.views.generic import UpdateView
from django.views.generic import ListView
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required

# Only authenticated users can access views using this.
from braces.views import LoginRequiredMixin

# Import the form from users/forms.py
from .forms import UserForm

# Import the customized User model
from .models import User


class UserDetailView(DetailView):
    model = User
    # These next two lines tell the view to index lookups by username
    slug_field = "username"
    slug_url_kwarg = "username"


class UserRedirectView(LoginRequiredMixin, RedirectView):
    permanent = False

    def get_redirect_url(self):
        return reverse(
            "users:detail",
            kwargs={"username": self.request.user.username})


@login_required()
def user_update_view(request, template='users/user_form.html'):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():

            form.save()

            success_url = reverse(
                "users:detail",
                kwargs={"username": request.user.username})

            return redirect(success_url)

        return render(
            request,
            template,
            {'form': form}
        )

    else:  # Get request
        form = UserForm()

        return render(
            request,
            template,
            {'form': form}
        )


class UserUpdateView(LoginRequiredMixin, UpdateView):

    form_class = UserForm

    # we already imported User in the view code above, remember?
    model = User

    # send the user back to their own page after a successful update
    def get_success_url(self):
        return reverse(
            "users:detail",
            kwargs={"username": self.request.user.username})

    def get_object(self):
        # Only get the User record for the user making the request
        return User.objects.get(username=self.request.user.username)


class UserListView(ListView):
    model = User
    # These next two lines tell the view to index lookups by username
    slug_field = "username"
    slug_url_kwarg = "username"

@@ -1119,16 +1119,81 @@
 html'):%0A
+    instance = User.objects.get(username=request.user.username)%0A%0A
     if r
@@ -1253,16 +1253,35 @@
 est.POST
+, instance=instance
 )%0A      
@@ -1502,112 +1502,8 @@
 l)%0A%0A
-        return render(%0A            request,%0A            template,%0A            %7B'form': form%7D%0A        )%0A%0A
     
@@ -1547,23 +1547,36 @@
 serForm(
-)%0A%0A    
+instance=instance)%0A%0A
     retu
@@ -1586,20 +1586,16 @@
 render(%0A
-    
         
@@ -1611,20 +1611,16 @@
         
-    
 template
@@ -1621,20 +1621,16 @@
 mplate,%0A
-    
         
@@ -1652,555 +1652,8 @@
     
-   
- )%0A%0A%0Aclass UserUpdateView(LoginRequiredMixin, UpdateView):%0A%0A    form_class = UserForm%0A%0A    # we already imported User in the view code above, remember?%0A    model = User%0A%0A    # send the user back to their own page after a successful update%0A    def get_success_url(self):%0A        return reverse(%0A            %22users:detail%22,%0A            kwargs=%7B%22username%22: self.request.user.username%7D)%0A%0A    def get_object(self):%0A        # Only get the User record for the user making the request%0A        return User.objects.get(username=self.request.user.username
 )%0A%0A%0A

fix the update view so the data gets saved