Convert transformation views to CBV.
This commit is contained in:
@@ -2,10 +2,15 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .views import (
|
||||
TransformationCreateView, TransformationDeleteView, TransformationEditView,
|
||||
TransformationListView
|
||||
)
|
||||
|
||||
urlpatterns = patterns(
|
||||
'converter.views',
|
||||
url(r'^create_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$', 'transformation_create', name='transformation_create'),
|
||||
url(r'^list_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$', 'transformation_list', name='transformation_list'),
|
||||
url(r'^delete/(?P<object_id>\d+)/$', 'transformation_delete', name='transformation_delete'),
|
||||
url(r'^edit/(?P<object_id>\d+)/$', 'transformation_edit', name='transformation_edit'),
|
||||
url(r'^create_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$', TransformationCreateView.as_view(), name='transformation_create'),
|
||||
url(r'^list_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$', TransformationListView.as_view(), name='transformation_list'),
|
||||
url(r'^delete/(?P<pk>\d+)/$', TransformationDeleteView.as_view(), name='transformation_delete'),
|
||||
url(r'^edit/(?P<pk>\d+)/$', TransformationEditView.as_view(), name='transformation_edit'),
|
||||
)
|
||||
|
||||
@@ -2,20 +2,20 @@ from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import logging
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from acls.models import AccessControlList
|
||||
from common.utils import encapsulate
|
||||
from common.views import (
|
||||
SingleObjectCreateView, SingleObjectDeleteView, SingleObjectEditView,
|
||||
SingleObjectListView
|
||||
)
|
||||
from permissions import Permission
|
||||
|
||||
from .forms import TransformationForm
|
||||
from .models import Transformation
|
||||
from .permissions import (
|
||||
permission_transformation_create, permission_transformation_delete,
|
||||
@@ -25,117 +25,144 @@ from .permissions import (
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def transformation_list(request, app_label, model, object_id):
|
||||
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
|
||||
class TransformationDeleteView(SingleObjectDeleteView):
|
||||
model = Transformation
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.transformation = get_object_or_404(Transformation, pk=self.kwargs['pk'])
|
||||
|
||||
try:
|
||||
content_object = content_type.get_object_for_this_type(pk=object_id)
|
||||
except content_type.model_class().DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_transformation_view])
|
||||
Permission.check_permissions(request.user, [permission_transformation_delete])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_transformation_view, request.user, content_object)
|
||||
AccessControlList.objects.check_access(permission_transformation_delete, request.user, self.transformation.content_object)
|
||||
|
||||
context = {
|
||||
'object_list': Transformation.objects.get_for_model(content_object),
|
||||
'content_object': content_object,
|
||||
'navigation_object_list': ['content_object'],
|
||||
'title': _('Transformations for: %s') % content_object,
|
||||
'extra_columns': [
|
||||
{'name': _('Order'), 'attribute': 'order'},
|
||||
{'name': _('Transformation'), 'attribute': encapsulate(lambda x: unicode(x))},
|
||||
{'name': _('Arguments'), 'attribute': 'arguments'}
|
||||
],
|
||||
'hide_link': True,
|
||||
'hide_object': True,
|
||||
return super(TransformationDeleteView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_post_action_redirect(self):
|
||||
return reverse('converter:transformation_list', args=[self.transformation.content_type.app_label, self.transformation.content_type.model, self.transformation.object_id])
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'content_object': self.transformation.content_object,
|
||||
'navigation_object_list': ['content_object', 'transformation'],
|
||||
'previous': reverse('converter:transformation_list', args=[self.transformation.content_type.app_label, self.transformation.content_type.model, self.transformation.object_id]),
|
||||
'title': _('Delete transformation "%(transformation)s" for: %(content_object)s?') % {
|
||||
'transformation': self.transformation,
|
||||
'content_object': self.transformation.content_object},
|
||||
'transformation': self.transformation,
|
||||
}
|
||||
return render_to_response(
|
||||
'appearance/generic_list.html', context, context_instance=RequestContext(request)
|
||||
)
|
||||
|
||||
|
||||
def transformation_create(request, app_label, model, object_id):
|
||||
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
|
||||
class TransformationCreateView(SingleObjectCreateView):
|
||||
fields = ('name', 'arguments', 'order')
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
content_type = get_object_or_404(ContentType, app_label=self.kwargs['app_label'], model=self.kwargs['model'])
|
||||
|
||||
try:
|
||||
content_object = content_type.get_object_for_this_type(pk=object_id)
|
||||
self.content_object = content_type.get_object_for_this_type(pk=self.kwargs['object_id'])
|
||||
except content_type.model_class().DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_transformation_create])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_transformation_create, request.user, content_object)
|
||||
AccessControlList.objects.check_access(permission_transformation_create, request.user, self.content_object)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = TransformationForm(request.POST, initial={'content_object': content_object})
|
||||
if form.is_valid():
|
||||
return super(TransformationCreateView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
instance = form.save(commit=False)
|
||||
instance.content_object = content_object
|
||||
instance.save()
|
||||
messages.success(request, _('Transformation created successfully.'))
|
||||
return HttpResponseRedirect(reverse('converter:transformation_list', args=[app_label, model, object_id]))
|
||||
else:
|
||||
form = TransformationForm(initial={'content_object': content_object})
|
||||
|
||||
return render_to_response('appearance/generic_form.html', {
|
||||
'form': form,
|
||||
'content_object': content_object,
|
||||
'navigation_object_list': ['content_object'],
|
||||
'title': _('Create new transformation for: %s') % content_object,
|
||||
}, context_instance=RequestContext(request))
|
||||
|
||||
|
||||
def transformation_delete(request, object_id):
|
||||
transformation = get_object_or_404(Transformation, pk=object_id)
|
||||
|
||||
instance.content_object = self.content_object
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_transformation_delete])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_transformation_delete, request.user, transformation.content_object)
|
||||
instance.full_clean()
|
||||
instance.save()
|
||||
except:
|
||||
return super(TransformationCreateView, self).form_invalid(form)
|
||||
else:
|
||||
return super(TransformationCreateView, self).form_valid(form)
|
||||
|
||||
if request.method == 'POST':
|
||||
transformation.delete()
|
||||
messages.success(request, _('Transformation deleted successfully.'))
|
||||
return HttpResponseRedirect(reverse('converter:transformation_list', args=[transformation.content_type.app_label, transformation.content_type.model, transformation.object_id]))
|
||||
def get_post_action_redirect(self):
|
||||
return reverse('converter:transformation_list', args=[self.kwargs['app_label'], self.kwargs['model'], self.kwargs['object_id']])
|
||||
|
||||
return render_to_response('appearance/generic_confirm.html', {
|
||||
'content_object': transformation.content_object,
|
||||
'delete_view': True,
|
||||
'navigation_object_list': ['content_object', 'transformation'],
|
||||
'previous': reverse('converter:transformation_list', args=[transformation.content_type.app_label, transformation.content_type.model, transformation.object_id]),
|
||||
'title': _('Are you sure you wish to delete transformation "%(transformation)s" for: %(content_object)s') % {
|
||||
'transformation': transformation,
|
||||
'content_object': transformation.content_object},
|
||||
'transformation': transformation,
|
||||
}, context_instance=RequestContext(request))
|
||||
def get_queryset(self):
|
||||
return Transformation.objects.get_for_model(self.content_object)
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'content_object': self.content_object,
|
||||
'navigation_object_list': ['content_object'],
|
||||
'title': _('Create new transformation for: %s') % self.content_object,
|
||||
}
|
||||
|
||||
|
||||
def transformation_edit(request, object_id):
|
||||
transformation = get_object_or_404(Transformation, pk=object_id)
|
||||
class TransformationEditView(SingleObjectEditView):
|
||||
fields = ('name', 'arguments', 'order')
|
||||
model = Transformation
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.transformation = get_object_or_404(Transformation, pk=self.kwargs['pk'])
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_transformation_edit])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_transformation_edit, request.user, transformation.content_object)
|
||||
AccessControlList.objects.check_access(permission_transformation_edit, request.user, self.transformation.content_object)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = TransformationForm(request.POST, instance=transformation)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, _('Transformation edited successfully.'))
|
||||
return HttpResponseRedirect(reverse('converter:transformation_list', args=[transformation.content_type.app_label, transformation.content_type.model, transformation.object_id]))
|
||||
return super(TransformationEditView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
instance = form.save(commit=False)
|
||||
try:
|
||||
instance.full_clean()
|
||||
instance.save()
|
||||
except:
|
||||
return super(TransformationEditView, self).form_invalid(form)
|
||||
else:
|
||||
form = TransformationForm(instance=transformation)
|
||||
return super(TransformationEditView, self).form_valid(form)
|
||||
|
||||
return render_to_response('appearance/generic_form.html', {
|
||||
'content_object': transformation.content_object,
|
||||
'form': form,
|
||||
def get_post_action_redirect(self):
|
||||
return reverse('converter:transformation_list', args=[self.transformation.content_type.app_label, self.transformation.content_type.model, self.transformation.object_id])
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'content_object': self.transformation.content_object,
|
||||
'navigation_object_list': ['content_object', 'transformation'],
|
||||
'title': _('Edit transformation "%(transformation)s" for: %(content_object)s') % {
|
||||
'transformation': transformation,
|
||||
'content_object': transformation.content_object},
|
||||
'transformation': transformation,
|
||||
}, context_instance=RequestContext(request))
|
||||
'transformation': self.transformation,
|
||||
'content_object': self.transformation.content_object},
|
||||
'transformation': self.transformation,
|
||||
}
|
||||
|
||||
|
||||
class TransformationListView(SingleObjectListView):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
content_type = get_object_or_404(ContentType, app_label=self.kwargs['app_label'], model=self.kwargs['model'])
|
||||
|
||||
try:
|
||||
self.content_object = content_type.get_object_for_this_type(pk=self.kwargs['object_id'])
|
||||
except content_type.model_class().DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
try:
|
||||
Permission.check_permissions(request.user, [permission_transformation_view])
|
||||
except PermissionDenied:
|
||||
AccessControlList.objects.check_access(permission_transformation_view, request.user, self.content_object)
|
||||
|
||||
return super(TransformationListView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_queryset(self):
|
||||
return Transformation.objects.get_for_model(self.content_object)
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'content_object': self.content_object,
|
||||
'extra_columns': [
|
||||
{'name': _('Order'), 'attribute': 'order'},
|
||||
{'name': _('Transformation'), 'attribute': encapsulate(lambda transformation: unicode(transformation))},
|
||||
{'name': _('Arguments'), 'attribute': 'arguments'}
|
||||
],
|
||||
'hide_link': True,
|
||||
'hide_object': True,
|
||||
'navigation_object_list': ['content_object'],
|
||||
'title': _('Transformations for: %s') % self.content_object,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user