from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.views.generic import FormView, TemplateView
from django.utils.datastructures import MultiValueDictKeyError
from incuna.utils import get_class_from_path

from profiles.models import Profile
from profiles.utils import class_view_decorator

try:
    ProfileForm = get_class_from_path(settings.PROFILE_FORM_CLASS)
except AttributeError:
    from forms import ProfileForm


@class_view_decorator(login_required)
class ProfileView(TemplateView):
    template_name = 'profiles/profile.html'


@class_view_decorator(login_required)
class ProfileEdit(FormView):
    form_class = ProfileForm
    template_name = 'profiles/profile_form.html'

    def form_valid(self, form):
        instance = super(ProfileEdit, self).form_valid(form)
        self.request.user.message_set.create(message='Your profile has been updated.')
        return instance

    def get_context_data(self, **kwargs):
        context = super(ProfileEdit, self).get_context_data(**kwargs)
        context['site'] = Site.objects.get_current()
        return context

    def get_object(self):
        if isinstance(self.request.user, Profile):
            return self.request.user
        return self.request.user.profile

    def get_success_url(self):
        try:
            return self.request.GET['next']
        except MultiValueDictKeyError:
            return reverse('profile')


from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDictKeyError
from django.views.generic import TemplateView, UpdateView
from incuna.utils import get_class_from_path

from profiles.models import Profile
from profiles.utils import class_view_decorator

try:
    ProfileForm = get_class_from_path(settings.PROFILE_FORM_CLASS)
except AttributeError:
    from forms import ProfileForm


@class_view_decorator(login_required)
class ProfileView(TemplateView):
    template_name = 'profiles/profile.html'


@class_view_decorator(login_required)
class ProfileEdit(UpdateView):
    form_class = ProfileForm
    template_name = 'profiles/profile_form.html'

    def form_valid(self, form):
        instance = super(ProfileEdit, self).form_valid(form)
        self.request.user.message_set.create(message='Your profile has been updated.')
        return instance

    def get_context_data(self, **kwargs):
        context = super(ProfileEdit, self).get_context_data(**kwargs)
        context['site'] = Site.objects.get_current()
        return context

    def get_object(self):
        if isinstance(self.request.user, Profile):
            return self.request.user
        return self.request.user.profile

    def get_success_url(self):
        try:
            return self.request.GET['next']
        except MultiValueDictKeyError:
            return reverse('profile')


Use an update view instead of form view
Use an update view instead of form view

  from django.conf import settings
  from django.contrib.auth.decorators import login_required
  from django.contrib.sites.models import Site
  from django.core.urlresolvers import reverse
- from django.views.generic import FormView, TemplateView
  from django.utils.datastructures import MultiValueDictKeyError
+ from django.views.generic import TemplateView, UpdateView
  from incuna.utils import get_class_from_path
  
  from profiles.models import Profile
  from profiles.utils import class_view_decorator
  
  try:
      ProfileForm = get_class_from_path(settings.PROFILE_FORM_CLASS)
  except AttributeError:
      from forms import ProfileForm
  
  
  @class_view_decorator(login_required)
  class ProfileView(TemplateView):
      template_name = 'profiles/profile.html'
  
  
  @class_view_decorator(login_required)
- class ProfileEdit(FormView):
+ class ProfileEdit(UpdateView):
      form_class = ProfileForm
      template_name = 'profiles/profile_form.html'
  
      def form_valid(self, form):
          instance = super(ProfileEdit, self).form_valid(form)
          self.request.user.message_set.create(message='Your profile has been updated.')
          return instance
  
      def get_context_data(self, **kwargs):
          context = super(ProfileEdit, self).get_context_data(**kwargs)
          context['site'] = Site.objects.get_current()
          return context
  
      def get_object(self):
          if isinstance(self.request.user, Profile):
              return self.request.user
          return self.request.user.profile
  
      def get_success_url(self):
          try:
              return self.request.GET['next']
          except MultiValueDictKeyError:
              return reverse('profile')
  
  
Use an update view instead of form view
## Code Before:
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.views.generic import FormView, TemplateView
from django.utils.datastructures import MultiValueDictKeyError
from incuna.utils import get_class_from_path

from profiles.models import Profile
from profiles.utils import class_view_decorator

try:
    ProfileForm = get_class_from_path(settings.PROFILE_FORM_CLASS)
except AttributeError:
    from forms import ProfileForm


@class_view_decorator(login_required)
class ProfileView(TemplateView):
    template_name = 'profiles/profile.html'


@class_view_decorator(login_required)
class ProfileEdit(FormView):
    form_class = ProfileForm
    template_name = 'profiles/profile_form.html'

    def form_valid(self, form):
        instance = super(ProfileEdit, self).form_valid(form)
        self.request.user.message_set.create(message='Your profile has been updated.')
        return instance

    def get_context_data(self, **kwargs):
        context = super(ProfileEdit, self).get_context_data(**kwargs)
        context['site'] = Site.objects.get_current()
        return context

    def get_object(self):
        if isinstance(self.request.user, Profile):
            return self.request.user
        return self.request.user.profile

    def get_success_url(self):
        try:
            return self.request.GET['next']
        except MultiValueDictKeyError:
            return reverse('profile')


## Instruction:
Use an update view instead of form view
## Code After:
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDictKeyError
from django.views.generic import TemplateView, UpdateView
from incuna.utils import get_class_from_path

from profiles.models import Profile
from profiles.utils import class_view_decorator

try:
    ProfileForm = get_class_from_path(settings.PROFILE_FORM_CLASS)
except AttributeError:
    from forms import ProfileForm


@class_view_decorator(login_required)
class ProfileView(TemplateView):
    template_name = 'profiles/profile.html'


@class_view_decorator(login_required)
class ProfileEdit(UpdateView):
    form_class = ProfileForm
    template_name = 'profiles/profile_form.html'

    def form_valid(self, form):
        instance = super(ProfileEdit, self).form_valid(form)
        self.request.user.message_set.create(message='Your profile has been updated.')
        return instance

    def get_context_data(self, **kwargs):
        context = super(ProfileEdit, self).get_context_data(**kwargs)
        context['site'] = Site.objects.get_current()
        return context

    def get_object(self):
        if isinstance(self.request.user, Profile):
            return self.request.user
        return self.request.user.profile

    def get_success_url(self):
        try:
            return self.request.GET['next']
        except MultiValueDictKeyError:
            return reverse('profile')

