Skip to content

Commit

Permalink
support for handle and save in form_valid
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkuty committed Jul 5, 2015
1 parent 4cee82d commit d620cc7
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions horizon_contrib/forms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from django.conf import settings
from django.core.urlresolvers import reverse, reverse_lazy
from django.forms import models as model_forms
from django.forms.models import model_to_dict
from django.utils.translation import ugettext_lazy as _
from django.views import generic
from horizon import exceptions
from horizon_contrib.common import content_type as ct
from horizon_contrib.forms.forms import SelfHandlingModelForm
from django.forms.models import model_to_dict


ADD_TO_FIELD_HEADER = "HTTP_X_HORIZON_ADD_TO_FIELD"

Expand Down Expand Up @@ -148,7 +148,7 @@ def object(self):

try:
obj = self.model.objects.get(id=self.kwargs["id"])
except Exception as e:
except Exception, e:
raise e
return obj

Expand All @@ -173,8 +173,7 @@ def get_form_class(self):
# If this view is operating on a single object, use
# the class of that object
model = self.object.__class__

return model_forms.modelform_factory(model, form=SelfHandlingModelForm)
return model_forms.modelform_factory(model)


class CreateView(ModelFormMixin, ModalFormView, ContextMixin):
Expand All @@ -197,22 +196,24 @@ def get_success_url(self):
return super(CreateView, self).get_success_url()

def form_valid(self, form):
try:
obj = form.save()
# maybe will be overwritten
success_url = self.get_success_url()
except Exception as e:
raise e
else:
# optionaly use model for redirect to detail

handled = None
if hasattr(form, 'handle'):
try:
success_url = obj.get_absolute_url()
handled = super(CreateView, self).form_valid(form)
except:
pass
response = http.HttpResponseRedirect(success_url)
response['X-Horizon-Location'] = success_url
elif hasattr(form, 'save'):

try:
form.save()
success_url = self.get_success_url()
response = http.HttpResponseRedirect(success_url)
response['X-Horizon-Location'] = success_url
except Exception as e:
raise e

return response
return handled or response

def get_context_data(self, **kwargs):
context = super(CreateView, self).get_context_data(**kwargs)
Expand Down

0 comments on commit d620cc7

Please sign in to comment.