0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Extract generic functionality from sites edit view

This commit is contained in:
Matt Westcott 2015-08-16 17:03:04 +02:00
parent bc0f46e5f7
commit 77adea6c51
2 changed files with 39 additions and 51 deletions

View File

@ -1,5 +1,5 @@
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.translation import ugettext as _
from django.views.generic.base import View
@ -69,3 +69,30 @@ class CreateView(PermissionCheckedView):
return render(self.request, self.template, {
'form': self.form,
})
class EditView(PermissionCheckedView):
def get(self, request, instance_id):
self.instance = get_object_or_404(self.model, id=instance_id)
self.form = self.form_class(instance=self.instance)
return self.render_to_response()
def post(self, request, instance_id):
self.instance = get_object_or_404(self.model, id=instance_id)
self.form = self.form_class(request.POST, instance=self.instance)
if self.form.is_valid():
self.form.save()
messages.success(request, self.success_message.format(self.instance), buttons=[
messages.button(reverse(self.edit_url_name, args=(self.instance.id,)), _('Edit'))
])
return redirect(self.index_url_name)
else:
messages.error(request, self.error_message)
return self.render_to_response()
def render_to_response(self):
return render(self.request, self.template, {
self.context_object_name: self.instance,
'form': self.form,
})

View File

@ -1,11 +1,10 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.translation import ugettext as _, ugettext_lazy as __
from django.core.urlresolvers import reverse
from wagtail.wagtailcore.models import Site
from wagtail.wagtailsites.forms import SiteForm
from wagtail.wagtailadmin import messages
from wagtail.wagtailadmin.views.generic import PermissionCheckedView, IndexView
from wagtail.wagtailadmin.views.generic import PermissionCheckedView, IndexView, CreateView, EditView
class Index(IndexView):
@ -15,7 +14,7 @@ class Index(IndexView):
template = 'wagtailsites/index.html'
class Create(PermissionCheckedView):
class Create(CreateView):
permission_required = 'wagtailcore.add_site'
form_class = SiteForm
success_message = __("Site '{0}' created.")
@ -23,55 +22,17 @@ class Create(PermissionCheckedView):
index_url_name = 'wagtailsites:index'
template = 'wagtailsites/create.html'
def get(self, request):
self.form = self.form_class()
return self.render_to_response()
def post(self, request):
self.form = self.form_class(request.POST)
if self.form.is_valid():
instance = self.form.save()
messages.success(request, self.success_message.format(instance), buttons=[
messages.button(reverse(self.edit_url_name, args=(instance.id,)), _('Edit'))
])
return redirect(self.index_url_name)
else:
return self.render_to_response()
def render_to_response(self):
return render(self.request, self.template, {
'form': self.form,
})
class Edit(PermissionCheckedView):
class Edit(EditView):
permission_required = 'wagtailcore.change_site'
def get(self, request, site_id):
self.site = get_object_or_404(Site, id=site_id)
self.form = SiteForm(instance=self.site)
return self.render_to_response()
def post(self, request, site_id):
self.site = get_object_or_404(Site, id=site_id)
self.form = SiteForm(request.POST, instance=self.site)
if self.form.is_valid():
site = self.form.save()
messages.success(request, _("Site '{0}' updated.").format(site.hostname), buttons=[
messages.button(reverse('wagtailsites:edit', args=(site.id,)), _('Edit'))
])
return redirect('wagtailsites:index')
else:
messages.error(request, _("The site could not be saved due to errors."))
return self.render_to_response()
def render_to_response(self):
return render(self.request, 'wagtailsites/edit.html', {
'site': self.site,
'form': self.form,
})
model = Site
form_class = SiteForm
success_message = __("Site '{0}' updated.")
error_message = __("The site could not be saved due to errors.")
edit_url_name = 'wagtailsites:edit'
index_url_name = 'wagtailsites:index'
context_object_name = 'site'
template = 'wagtailsites/edit.html'
class Delete(PermissionCheckedView):