Initial audit of the convert app
Add keyword arguments to call. Sort methods and arguments. Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -23,16 +23,14 @@ class ConverterApp(MayanAppConfig):
|
||||
def ready(self):
|
||||
super(ConverterApp, self).ready()
|
||||
|
||||
Transformation = self.get_model('Transformation')
|
||||
Transformation = self.get_model(model_name='Transformation')
|
||||
|
||||
SourceColumn(source=Transformation, label=_('Order'), attribute='order')
|
||||
SourceColumn(attribute='order', source=Transformation)
|
||||
SourceColumn(
|
||||
source=Transformation, label=_('Transformation'),
|
||||
func=lambda context: force_text(context['object'])
|
||||
)
|
||||
SourceColumn(
|
||||
source=Transformation, label=_('Arguments'), attribute='arguments'
|
||||
)
|
||||
SourceColumn(attribute='arguments', source=Transformation)
|
||||
|
||||
menu_object.bind_links(
|
||||
links=(link_transformation_edit, link_transformation_delete),
|
||||
|
||||
@@ -16,10 +16,10 @@ class TransformationForm(forms.ModelForm):
|
||||
|
||||
def clean(self):
|
||||
try:
|
||||
yaml.safe_load(self.cleaned_data['arguments'])
|
||||
yaml.safe_load(stream=self.cleaned_data['arguments'])
|
||||
except yaml.YAMLError:
|
||||
raise ValidationError(
|
||||
_(
|
||||
message=_(
|
||||
'"%s" not a valid entry.'
|
||||
) % self.cleaned_data['arguments']
|
||||
)
|
||||
|
||||
@@ -17,15 +17,15 @@ class TransformationManager(models.Manager):
|
||||
content_type = ContentType.objects.get_for_model(obj)
|
||||
|
||||
self.create(
|
||||
content_type=content_type, object_id=obj.pk,
|
||||
name=transformation.name, arguments=yaml.safe_dump(arguments)
|
||||
arguments=yaml.safe_dump(arguments), content_type=content_type,
|
||||
name=transformation.name, object_id=obj.pk
|
||||
)
|
||||
|
||||
def copy(self, source, targets):
|
||||
"""
|
||||
Copy transformation from source to all targets
|
||||
"""
|
||||
content_type = ContentType.objects.get_for_model(source)
|
||||
content_type = ContentType.objects.get_for_model(obj=source)
|
||||
|
||||
# Get transformations
|
||||
transformations = self.filter(
|
||||
@@ -76,7 +76,7 @@ class TransformationManager(models.Manager):
|
||||
for transformation in transformations:
|
||||
try:
|
||||
transformation_class = BaseTransformation.get(
|
||||
transformation.name
|
||||
name=transformation.name
|
||||
)
|
||||
except KeyError:
|
||||
# Non existant transformation, but we don't raise an error
|
||||
@@ -89,7 +89,9 @@ class TransformationManager(models.Manager):
|
||||
# Some transformations don't require arguments
|
||||
# return an empty dictionary as ** doesn't allow None
|
||||
if transformation.arguments:
|
||||
kwargs = yaml.safe_load(transformation.arguments)
|
||||
kwargs = yaml.safe_load(
|
||||
stream=transformation.arguments
|
||||
)
|
||||
else:
|
||||
kwargs = {}
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ class Transformation(models.Model):
|
||||
"""
|
||||
content_type = models.ForeignKey(on_delete=models.CASCADE, to=ContentType)
|
||||
object_id = models.PositiveIntegerField()
|
||||
content_object = GenericForeignKey('content_type', 'object_id')
|
||||
content_object = GenericForeignKey(
|
||||
ct_field='content_type', fk_field='object_id'
|
||||
)
|
||||
order = models.PositiveIntegerField(
|
||||
blank=True, db_index=True, default=0, help_text=_(
|
||||
'Order in which the transformations will be executed. If left '
|
||||
|
||||
@@ -7,14 +7,14 @@ from mayan.apps.permissions import PermissionNamespace
|
||||
namespace = PermissionNamespace(label=_('Converter'), name='converter')
|
||||
|
||||
permission_transformation_create = namespace.add_permission(
|
||||
name='transformation_create', label=_('Create new transformations')
|
||||
label=_('Create new transformations'), name='transformation_create'
|
||||
)
|
||||
permission_transformation_delete = namespace.add_permission(
|
||||
name='transformation_delete', label=_('Delete transformations')
|
||||
label=_('Delete transformations'), name='transformation_delete'
|
||||
)
|
||||
permission_transformation_edit = namespace.add_permission(
|
||||
name='transformation_edit', label=_('Edit transformations')
|
||||
label=_('Edit transformations'), name='transformation_edit'
|
||||
)
|
||||
permission_transformation_view = namespace.add_permission(
|
||||
name='transformation_view', label=_('View existing transformations')
|
||||
label=_('View existing transformations'), name='transformation_view'
|
||||
)
|
||||
|
||||
@@ -9,7 +9,7 @@ from .literals import (
|
||||
DEFAULT_PDFTOPPM_PATH, DEFAULT_PDFINFO_PATH, DEFAULT_PILLOW_FORMAT
|
||||
)
|
||||
|
||||
namespace = Namespace(name='converter', label=_('Converter'))
|
||||
namespace = Namespace(label=_('Converter'), name='converter')
|
||||
setting_graphics_backend = namespace.add_setting(
|
||||
default='mayan.apps.converter.backends.python.Python',
|
||||
help_text=_('Graphics conversion backend to use.'),
|
||||
|
||||
@@ -80,7 +80,7 @@ class TransformationViewsTestCase(GenericDocumentViewTestCase):
|
||||
def _transformation_delete_view(self):
|
||||
return self.post(
|
||||
viewname='converter:transformation_delete', kwargs={
|
||||
'pk': self.transformation.pk
|
||||
'transformation_pk': self.transformation.pk
|
||||
}
|
||||
)
|
||||
|
||||
@@ -104,7 +104,7 @@ class TransformationViewsTestCase(GenericDocumentViewTestCase):
|
||||
def _transformation_edit_view(self):
|
||||
return self.post(
|
||||
viewname='converter:transformation_edit', kwargs={
|
||||
'pk': self.transformation.pk
|
||||
'transformation_pk': self.transformation.pk
|
||||
}, data={
|
||||
'arguments': TEST_TRANSFORMATION_ARGUMENT_EDITED,
|
||||
'name': self.transformation.name
|
||||
|
||||
@@ -33,18 +33,6 @@ class BaseTransformation(object):
|
||||
|
||||
return result.hexdigest()
|
||||
|
||||
@classmethod
|
||||
def register(cls, transformation):
|
||||
cls._registry[transformation.name] = transformation
|
||||
|
||||
@classmethod
|
||||
def get_transformation_choices(cls):
|
||||
return sorted(
|
||||
[
|
||||
(name, klass.get_label()) for name, klass in cls._registry.items()
|
||||
]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get(cls, name):
|
||||
return cls._registry[name]
|
||||
@@ -58,6 +46,19 @@ class BaseTransformation(object):
|
||||
else:
|
||||
return cls.label
|
||||
|
||||
@classmethod
|
||||
def get_transformation_choices(cls):
|
||||
return sorted(
|
||||
[
|
||||
(name, klass.get_label()) for name, klass in cls._registry.items()
|
||||
]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def register(cls, transformation):
|
||||
cls._registry[transformation.name] = transformation
|
||||
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.kwargs = {}
|
||||
for argument_name in self.arguments:
|
||||
|
||||
@@ -9,19 +9,19 @@ from .views import (
|
||||
|
||||
urlpatterns = [
|
||||
url(
|
||||
r'^create_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$',
|
||||
TransformationCreateView.as_view(), name='transformation_create'
|
||||
regex=r'^create_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$',
|
||||
name='transformation_create', view=TransformationCreateView.as_view()
|
||||
),
|
||||
url(
|
||||
r'^list_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$',
|
||||
TransformationListView.as_view(), name='transformation_list'
|
||||
regex=r'^list_for/(?P<app_label>[-\w]+)/(?P<model>[-\w]+)/(?P<object_id>\d+)/$',
|
||||
name='transformation_list', view=TransformationListView.as_view()
|
||||
),
|
||||
url(
|
||||
r'^delete/(?P<pk>\d+)/$', TransformationDeleteView.as_view(),
|
||||
name='transformation_delete'
|
||||
regex=r'^delete/(?P<transformation_pk>\d+)/$',
|
||||
name='transformation_delete', view=TransformationDeleteView.as_view()
|
||||
),
|
||||
url(
|
||||
r'^edit/(?P<pk>\d+)/$', TransformationEditView.as_view(),
|
||||
name='transformation_edit'
|
||||
regex=r'^edit/(?P<transformation_pk>\d+)/$',
|
||||
name='transformation_edit', view=TransformationEditView.as_view()
|
||||
),
|
||||
]
|
||||
|
||||
@@ -29,15 +29,16 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class TransformationDeleteView(SingleObjectDeleteView):
|
||||
model = Transformation
|
||||
pk_url_kwarg = 'transformation_pk'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.transformation = get_object_or_404(
|
||||
klass=Transformation, pk=self.kwargs['pk']
|
||||
klass=Transformation, pk=self.kwargs['transformation_pk']
|
||||
)
|
||||
|
||||
AccessControlList.objects.check_access(
|
||||
permissions=permission_transformation_delete, user=request.user,
|
||||
obj=self.transformation.content_object
|
||||
permissions=permission_transformation_delete,
|
||||
obj=self.transformation.content_object, user=request.user
|
||||
)
|
||||
|
||||
return super(TransformationDeleteView, self).dispatch(
|
||||
@@ -46,11 +47,11 @@ class TransformationDeleteView(SingleObjectDeleteView):
|
||||
|
||||
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
|
||||
)
|
||||
viewname='converter:transformation_list', kwargs={
|
||||
'app_label': self.transformation.content_type.app_label,
|
||||
'model': self.transformation.content_type.model,
|
||||
'object_id': self.transformation.object_id
|
||||
}
|
||||
)
|
||||
|
||||
def get_extra_context(self):
|
||||
@@ -58,11 +59,11 @@ class TransformationDeleteView(SingleObjectDeleteView):
|
||||
'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
|
||||
)
|
||||
viewname='converter:transformation_list', kwargs={
|
||||
'app_label': self.transformation.content_type.app_label,
|
||||
'model': self.transformation.content_type.model,
|
||||
'object_id': self.transformation.object_id
|
||||
}
|
||||
),
|
||||
'title': _(
|
||||
'Delete transformation "%(transformation)s" for: '
|
||||
@@ -92,8 +93,8 @@ class TransformationCreateView(SingleObjectCreateView):
|
||||
raise Http404
|
||||
|
||||
AccessControlList.objects.check_access(
|
||||
permissions=permission_transformation_create, user=request.user,
|
||||
obj=self.content_object
|
||||
permissions=permission_transformation_create,
|
||||
obj=self.content_object, user=request.user
|
||||
)
|
||||
|
||||
return super(TransformationCreateView, self).dispatch(
|
||||
@@ -108,9 +109,11 @@ class TransformationCreateView(SingleObjectCreateView):
|
||||
instance.save()
|
||||
except Exception as exception:
|
||||
logger.debug('Invalid form, exception: %s', exception)
|
||||
return super(TransformationCreateView, self).form_invalid(form)
|
||||
return super(TransformationCreateView, self).form_invalid(
|
||||
form=form
|
||||
)
|
||||
else:
|
||||
return super(TransformationCreateView, self).form_valid(form)
|
||||
return super(TransformationCreateView, self).form_valid(form=form)
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
@@ -123,32 +126,34 @@ class TransformationCreateView(SingleObjectCreateView):
|
||||
|
||||
def get_post_action_redirect(self):
|
||||
return reverse(
|
||||
'converter:transformation_list', args=(
|
||||
self.kwargs['app_label'], self.kwargs['model'],
|
||||
self.kwargs['object_id']
|
||||
)
|
||||
viewname='converter:transformation_list', kwargs={
|
||||
'app_label': self.kwargs['app_label'],
|
||||
'model': self.kwargs['model'],
|
||||
'object_id': self.kwargs['object_id']
|
||||
}
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
return Transformation.objects.get_for_model(self.content_object)
|
||||
return Transformation.objects.get_for_model(obj=self.content_object)
|
||||
|
||||
|
||||
class TransformationEditView(SingleObjectEditView):
|
||||
form_class = TransformationForm
|
||||
model = Transformation
|
||||
pk_url_kwarg = 'transformation_pk'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.transformation = get_object_or_404(
|
||||
klass=Transformation, pk=self.kwargs['pk']
|
||||
klass=Transformation, pk=self.kwargs['transformation_pk']
|
||||
)
|
||||
|
||||
AccessControlList.objects.check_access(
|
||||
permissions=permission_transformation_edit, user=request.user,
|
||||
obj=self.transformation.content_object
|
||||
obj=self.transformation.content_object,
|
||||
permissions=permission_transformation_edit, user=request.user
|
||||
)
|
||||
|
||||
return super(TransformationEditView, self).dispatch(
|
||||
request, *args, **kwargs
|
||||
request=request, *args, **kwargs
|
||||
)
|
||||
|
||||
def form_valid(self, form):
|
||||
@@ -158,9 +163,9 @@ class TransformationEditView(SingleObjectEditView):
|
||||
instance.save()
|
||||
except Exception as exception:
|
||||
logger.debug('Invalid form, exception: %s', exception)
|
||||
return super(TransformationEditView, self).form_invalid(form)
|
||||
return super(TransformationEditView, self).form_invalid(form=form)
|
||||
else:
|
||||
return super(TransformationEditView, self).form_valid(form)
|
||||
return super(TransformationEditView, self).form_valid(form=form)
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
@@ -177,11 +182,11 @@ class TransformationEditView(SingleObjectEditView):
|
||||
|
||||
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
|
||||
)
|
||||
viewname='converter:transformation_list', kwargs={
|
||||
'app_label': self.transformation.content_type.app_label,
|
||||
'model': self.transformation.content_type.model,
|
||||
'object_id': self.transformation.object_id
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -200,12 +205,13 @@ class TransformationListView(SingleObjectListView):
|
||||
raise Http404
|
||||
|
||||
AccessControlList.objects.check_access(
|
||||
permissions=permission_transformation_view, user=request.user,
|
||||
obj=self.content_object
|
||||
obj=self.content_object,
|
||||
permissions=permission_transformation_view,
|
||||
user=request.user
|
||||
)
|
||||
|
||||
return super(TransformationListView, self).dispatch(
|
||||
request, *args, **kwargs
|
||||
request=request, *args, **kwargs
|
||||
)
|
||||
|
||||
def get_extra_context(self):
|
||||
@@ -230,4 +236,4 @@ class TransformationListView(SingleObjectListView):
|
||||
}
|
||||
|
||||
def get_object_list(self):
|
||||
return Transformation.objects.get_for_model(self.content_object)
|
||||
return Transformation.objects.get_for_model(obj=self.content_object)
|
||||
|
||||
Reference in New Issue
Block a user