Skip to content

Commit

Permalink
use our form class and try redirect to model detail after create
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkuty committed Jul 5, 2015
1 parent be6b990 commit 4cee82d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions horizon_contrib/forms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
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 django.forms.models import model_to_dict

from horizon_contrib.forms.forms import SelfHandlingModelForm

ADD_TO_FIELD_HEADER = "HTTP_X_HORIZON_ADD_TO_FIELD"

# TODO inherit from SelfhandlingForm


class ModalFormMixin(object):

def get_template_names(self):
if self.request.is_ajax():
if not hasattr(self, "ajax_template_name"):
Expand Down Expand Up @@ -68,6 +69,7 @@ def get_help_text(self):


class ModalFormView(ModalFormMixin, generic.FormView):

"""The main view class from which all views which handle forms in Horizon
should inherit. It takes care of all details with processing
:class:`~horizon.forms.base.SelfHandlingForm` classes, and modal concerns
Expand Down Expand Up @@ -119,7 +121,8 @@ def form_valid(self, form):
else:
success_url = self.get_success_url()
if self.request.META.get("HTTP_REFERER") != self.request.build_absolute_uri():
response = http.HttpResponseRedirect(self.request.META.get('HTTP_REFERER', success_url))
response = http.HttpResponseRedirect(
self.request.META.get('HTTP_REFERER', success_url))
else:
response = http.HttpResponseRedirect(success_url)
# TODO(gabriel): This is not a long-term solution to how
Expand All @@ -145,7 +148,7 @@ def object(self):

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

Expand All @@ -170,7 +173,8 @@ 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)

return model_forms.modelform_factory(model, form=SelfHandlingModelForm)


class CreateView(ModelFormMixin, ModalFormView, ContextMixin):
Expand All @@ -194,12 +198,19 @@ def get_success_url(self):

def form_valid(self, form):
try:
form.save()
obj = form.save()
# maybe will be overwritten
success_url = self.get_success_url()
response = http.HttpResponseRedirect(success_url)
response['X-Horizon-Location'] = success_url
except Exception as e:
raise e
else:
# optionaly use model for redirect to detail
try:
success_url = obj.get_absolute_url()
except:
pass
response = http.HttpResponseRedirect(success_url)
response['X-Horizon-Location'] = success_url

return response

Expand Down

0 comments on commit 4cee82d

Please sign in to comment.