Add contol sheet preview
Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -226,6 +226,6 @@ class APIControlSheetCodeImageView(generics.RetrieveAPIView):
|
||||
if '_hash' in request.GET:
|
||||
patch_cache_control(
|
||||
response,
|
||||
max_age=settings_control_sheet_image_cache_time.value
|
||||
max_age=settings_control_sheet_code_image_cache_time.value
|
||||
)
|
||||
return response
|
||||
|
||||
@@ -8,19 +8,21 @@ import qrcode
|
||||
|
||||
from django.apps import apps
|
||||
from django.db import transaction
|
||||
from django.utils.translation import string_concat, ugettext_lazy as _
|
||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||
from django.utils.translation import (
|
||||
force_text, string_concat, ugettext_lazy as _
|
||||
)
|
||||
|
||||
from mayan.apps.common.serialization import yaml_dump, yaml_load
|
||||
from mayan.apps.documents.literals import DOCUMENT_IMAGE_TASK_TIMEOUT
|
||||
from mayan.apps.documents.tasks import task_generate_document_page_image
|
||||
|
||||
from .literals import (
|
||||
CONTROL_CODE_MAGIC_NUMBER, CONTROL_CODE_SEPARATOR, CONTROL_CODE_VERSION
|
||||
)
|
||||
from .literals import (CONTROL_CODE_MAGIC_NUMBER, CONTROL_CODE_SEPARATOR)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class ControlCode(object):
|
||||
_registry = {}
|
||||
arguments = ()
|
||||
@@ -38,16 +40,9 @@ class ControlCode(object):
|
||||
|
||||
@classmethod
|
||||
def get_choices(cls):
|
||||
#if layer:
|
||||
# transformation_list = [
|
||||
# (transformation.name, transformation) for transformation in cls._layer_transformations[layer]
|
||||
# ]
|
||||
#else:
|
||||
#control_code_list = cls._registry.items()
|
||||
|
||||
return sorted(
|
||||
[
|
||||
(name, klass.get_label()) for name, klass in cls._registry.items()#transformation_list
|
||||
(name, klass.get_label()) for name, klass in cls._registry.items()
|
||||
]
|
||||
)
|
||||
|
||||
@@ -75,21 +70,20 @@ class ControlCode(object):
|
||||
parts = code.data.split(CONTROL_CODE_SEPARATOR)
|
||||
|
||||
if parts[0] == CONTROL_CODE_MAGIC_NUMBER:
|
||||
# Version
|
||||
if parts[1] == '1':
|
||||
try:
|
||||
control_code_class = ControlCode.get(name=parts[2])
|
||||
except KeyError:
|
||||
# Unknown control code name
|
||||
pass
|
||||
else:
|
||||
document_page.enabled = False
|
||||
document_page.save()
|
||||
arguments = CONTROL_CODE_SEPARATOR.join(parts[3:])
|
||||
control_code = control_code_class(
|
||||
**yaml_load(arguments)
|
||||
)
|
||||
control_code.execute()
|
||||
try:
|
||||
control_code_class = ControlCode.get(name=parts[2])
|
||||
except KeyError:
|
||||
# Unknown control code name
|
||||
pass
|
||||
else:
|
||||
document_page.enabled = False
|
||||
document_page.save()
|
||||
|
||||
arguments = CONTROL_CODE_SEPARATOR.join(parts[3:])
|
||||
control_code = control_code_class(
|
||||
**yaml_load(arguments)
|
||||
)
|
||||
control_code.execute()
|
||||
|
||||
@classmethod
|
||||
def register(cls, control_code):
|
||||
@@ -101,14 +95,16 @@ class ControlCode(object):
|
||||
setattr(self, argument_name, kwargs.get(argument_name))
|
||||
self.kwargs[argument_name] = kwargs.get(argument_name)
|
||||
|
||||
@property
|
||||
def image(self):
|
||||
return qrcode.make(self.get_qrcode_string())
|
||||
def __str__(self):
|
||||
return '{} {}'.format(self.label, self.kwargs)
|
||||
|
||||
def get_qrcode_string(self):
|
||||
def get_image(self, order=0):
|
||||
return qrcode.make(self.get_qrcode_string(order=order))
|
||||
|
||||
def get_qrcode_string(self, order=0):
|
||||
result = []
|
||||
result.append(CONTROL_CODE_MAGIC_NUMBER)
|
||||
result.append(CONTROL_CODE_VERSION)
|
||||
result.append(force_text(order))
|
||||
result.append(self.name)
|
||||
result.append(
|
||||
yaml_dump(
|
||||
|
||||
@@ -6,10 +6,11 @@ from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.common.forms import DetailForm
|
||||
from mayan.apps.common.serialization import yaml_load
|
||||
|
||||
from .classes import ControlCode
|
||||
from .models import ControlSheetCode
|
||||
from .models import ControlSheet, ControlSheetCode
|
||||
|
||||
|
||||
class ControlCodeClassSelectionForm(forms.Form):
|
||||
@@ -25,6 +26,60 @@ class ControlCodeClassSelectionForm(forms.Form):
|
||||
'control_code'
|
||||
].choices = ControlCode.get_choices()
|
||||
|
||||
"""
|
||||
class ControlSheetDetailForm(DetailForm):
|
||||
'''
|
||||
def __init__(self, *args, **kwargs):
|
||||
document = kwargs['instance']
|
||||
|
||||
extra_fields = [
|
||||
{
|
||||
'label': _('Date added'),
|
||||
'field': 'date_added',
|
||||
'widget': forms.widgets.DateTimeInput
|
||||
},
|
||||
{'label': _('UUID'), 'field': 'uuid'},
|
||||
{
|
||||
'label': _('Language'),
|
||||
'field': lambda x: get_language(language_code=document.language)
|
||||
},
|
||||
]
|
||||
|
||||
if document.latest_version:
|
||||
extra_fields += (
|
||||
{
|
||||
'label': _('File mimetype'),
|
||||
'field': lambda x: document.file_mimetype or _('None')
|
||||
},
|
||||
{
|
||||
'label': _('File encoding'),
|
||||
'field': lambda x: document.file_mime_encoding or _(
|
||||
'None'
|
||||
)
|
||||
},
|
||||
{
|
||||
'label': _('File size'),
|
||||
'field': lambda document: filesizeformat(
|
||||
document.size
|
||||
) if document.size else '-'
|
||||
},
|
||||
{'label': _('Exists in storage'), 'field': 'exists'},
|
||||
{
|
||||
'label': _('File path in storage'),
|
||||
'field': 'latest_version.file'
|
||||
},
|
||||
{'label': _('Checksum'), 'field': 'checksum'},
|
||||
{'label': _('Pages'), 'field': 'page_count'},
|
||||
)
|
||||
|
||||
kwargs['extra_fields'] = extra_fields
|
||||
super(DocumentPropertiesForm, self).__init__(*args, **kwargs)
|
||||
'''
|
||||
class Meta:
|
||||
fields = ('label', 'codes')
|
||||
model = ControlSheet
|
||||
|
||||
"""
|
||||
|
||||
"""
|
||||
class ControlSheetCodeForm(forms.ModelForm):
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
CONTROL_CODE_MAGIC_NUMBER = 'MCTRL'
|
||||
CONTROL_CODE_MAGIC_NUMBER = 'MCTRL1'
|
||||
CONTROL_CODE_SEPARATOR = ':'
|
||||
CONTROL_CODE_VERSION = '1'
|
||||
|
||||
CONTROL_SHEET_CODE_IMAGE_CACHE_NAME = 'control_sheet_codes'
|
||||
CONTROL_SHEET_CODE_IMAGE_CACHE_STORAGE_INSTANCE_PATH = 'mayan.apps.control_codes.storages.storage_controlsheetcodeimagecache'
|
||||
|
||||
@@ -75,6 +75,7 @@ class ControlSheetCode(models.Model):
|
||||
enabled = models.BooleanField(default=True, verbose_name=_('Enabled'))
|
||||
|
||||
class Meta:
|
||||
ordering = ('order',)
|
||||
verbose_name = _('Control sheet code')
|
||||
verbose_name_plural = _('Control sheet codes')
|
||||
|
||||
@@ -108,7 +109,6 @@ class ControlSheetCode(models.Model):
|
||||
|
||||
image = self.render()
|
||||
with self.cache_partition.create_file(filename=cache_filename) as file_object:
|
||||
#file_object.write(image)
|
||||
image.save(file_object)
|
||||
|
||||
return cache_filename
|
||||
@@ -117,8 +117,11 @@ class ControlSheetCode(models.Model):
|
||||
final_url = furl()
|
||||
final_url.args = kwargs
|
||||
final_url.path = reverse(
|
||||
viewname='rest_api:workflow-image',
|
||||
kwargs={'pk': self.pk}
|
||||
viewname='rest_api:controlsheet-code-image',
|
||||
kwargs={
|
||||
'control_sheet_id': self.control_sheet.pk,
|
||||
'control_sheet_code_id': self.pk
|
||||
}
|
||||
)
|
||||
final_url.args['_hash'] = self.get_hash()
|
||||
|
||||
@@ -130,6 +133,14 @@ class ControlSheetCode(models.Model):
|
||||
def get_control_code_class(self):
|
||||
return ControlCode.get(name=self.name)
|
||||
|
||||
def get_control_code_instance(self):
|
||||
return self.get_control_code_class()(
|
||||
**self.get_arguments()
|
||||
)
|
||||
|
||||
def get_display(self):
|
||||
return force_text(self.get_control_code_instance())
|
||||
|
||||
def get_hash(self):
|
||||
objects_lists = list(
|
||||
ControlSheetCode.objects.filter(pk=self.pk)
|
||||
@@ -142,7 +153,7 @@ class ControlSheetCode(models.Model):
|
||||
).hexdigest()
|
||||
|
||||
def render(self):
|
||||
return self.get_control_code_class()(**self.get_arguments()).image
|
||||
return self.get_control_code_instance().get_image(order=self.order)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.order:
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{% extends 'appearance/base_plain.html' %}
|
||||
|
||||
{% load documents_tags %}
|
||||
|
||||
{% block title %}{{ title }}{% endblock title %}
|
||||
|
||||
{% block content_plain %}
|
||||
<p class="text-center">
|
||||
Mayan EDMS control sheet: {{ object }}
|
||||
</p>
|
||||
|
||||
<ul class="list-inline text-center">
|
||||
{% for code in object.codes.all %}
|
||||
<li class="list-inline-item">
|
||||
<img
|
||||
class="img-responsive"
|
||||
src="{{ code.get_api_image_url }}"
|
||||
style=""
|
||||
/>
|
||||
<p>{{ code.get_display }}</p>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,2 @@
|
||||
<img class="img-responsive" name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %} src="{{ widget.value.get_api_image_url }}" style="margin:auto;" />
|
||||
|
||||
@@ -25,12 +25,13 @@ class ControlSheetDetailView(SingleObjectDetailView):
|
||||
fields = ('label',)
|
||||
pk_url_kwarg = 'control_sheet_id'
|
||||
model = ControlSheet
|
||||
template_name = 'control_codes/control_sheet_print.html'
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'object': self.object,
|
||||
'title': _(
|
||||
'Details for control sheet: %s'
|
||||
'Print preview for control sheet: %s'
|
||||
) % self.object
|
||||
}
|
||||
|
||||
|
||||
12
mayan/apps/control_codes/widgets.py
Normal file
12
mayan/apps/control_codes/widgets.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django import forms
|
||||
|
||||
|
||||
class ControlSheetCodeImageWidget(forms.widgets.Widget):
|
||||
template_name = 'control_codes/forms/widgets/control_sheet_code_image.html'
|
||||
|
||||
def format_value(self, value):
|
||||
if value == '' or value is None:
|
||||
return None
|
||||
return value
|
||||
Reference in New Issue
Block a user