Add widget support to SourceColumn
Allow passing a widget class to SourceColumn. This makes using lambdas to render model column unnecesary and are mostly removed too. Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
@@ -35,15 +35,41 @@ def decode_metadata_from_querystring(querystring=None):
|
||||
return metadata_list
|
||||
|
||||
|
||||
def save_metadata_list(metadata_list, document, create=False, _user=None):
|
||||
def metadata_repr(metadata_list):
|
||||
"""
|
||||
Take a list of metadata dictionaries and associate them to a
|
||||
document
|
||||
Return a printable representation of a metadata list
|
||||
"""
|
||||
for item in metadata_list:
|
||||
save_metadata(
|
||||
metadata_dict=item, document=document, create=create, _user=_user
|
||||
)
|
||||
return ', '.join(metadata_repr_as_list(metadata_list))
|
||||
|
||||
|
||||
def metadata_repr_as_list(metadata_list):
|
||||
"""
|
||||
Turn a list of metadata into a list of printable representations
|
||||
"""
|
||||
output = []
|
||||
for metadata_dict in metadata_list:
|
||||
try:
|
||||
output.append('%s - %s' % (MetadataType.objects.get(
|
||||
pk=metadata_dict['id']), metadata_dict.get('value', '')))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def set_bulk_metadata(document, metadata_dictionary):
|
||||
document_type = document.document_type
|
||||
document_type_metadata_types = [
|
||||
document_type_metadata_type.metadata_type for document_type_metadata_type in document_type.metadata.all()
|
||||
]
|
||||
|
||||
for metadata_type_name, value in metadata_dictionary.items():
|
||||
metadata_type = MetadataType.objects.get(name=metadata_type_name)
|
||||
|
||||
if metadata_type in document_type_metadata_types:
|
||||
DocumentMetadata.objects.get_or_create(
|
||||
document=document, metadata_type=metadata_type, value=value
|
||||
)
|
||||
|
||||
|
||||
def save_metadata(metadata_dict, document, create=False, _user=None):
|
||||
@@ -84,38 +110,12 @@ def save_metadata(metadata_dict, document, create=False, _user=None):
|
||||
document_metadata.save(_user=_user)
|
||||
|
||||
|
||||
def metadata_repr(metadata_list):
|
||||
def save_metadata_list(metadata_list, document, create=False, _user=None):
|
||||
"""
|
||||
Return a printable representation of a metadata list
|
||||
Take a list of metadata dictionaries and associate them to a
|
||||
document
|
||||
"""
|
||||
return ', '.join(metadata_repr_as_list(metadata_list))
|
||||
|
||||
|
||||
def metadata_repr_as_list(metadata_list):
|
||||
"""
|
||||
Turn a list of metadata into a list of printable representations
|
||||
"""
|
||||
output = []
|
||||
for metadata_dict in metadata_list:
|
||||
try:
|
||||
output.append('%s - %s' % (MetadataType.objects.get(
|
||||
pk=metadata_dict['id']), metadata_dict.get('value', '')))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def set_bulk_metadata(document, metadata_dictionary):
|
||||
document_type = document.document_type
|
||||
document_type_metadata_types = [
|
||||
document_type_metadata_type.metadata_type for document_type_metadata_type in document_type.metadata.all()
|
||||
]
|
||||
|
||||
for metadata_type_name, value in metadata_dictionary.items():
|
||||
metadata_type = MetadataType.objects.get(name=metadata_type_name)
|
||||
|
||||
if metadata_type in document_type_metadata_types:
|
||||
DocumentMetadata.objects.get_or_create(
|
||||
document=document, metadata_type=metadata_type, value=value
|
||||
)
|
||||
for item in metadata_list:
|
||||
save_metadata(
|
||||
metadata_dict=item, document=document, create=create, _user=_user
|
||||
)
|
||||
|
||||
@@ -54,7 +54,7 @@ from .permissions import (
|
||||
)
|
||||
from .queues import * # NOQA
|
||||
from .search import metadata_type_search # NOQA
|
||||
from .widgets import get_metadata_string
|
||||
from .widgets import widget_get_metadata_string
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -144,27 +144,27 @@ class MetadataApp(MayanAppConfig):
|
||||
)
|
||||
|
||||
SourceColumn(
|
||||
source=Document, label=_('Metadata'),
|
||||
func=lambda context: get_metadata_string(context['object'])
|
||||
func=widget_get_metadata_string, source=Document
|
||||
)
|
||||
|
||||
SourceColumn(
|
||||
source=DocumentPageSearchResult, label=_('Metadata'),
|
||||
func=lambda context: get_metadata_string(
|
||||
context['object'].document
|
||||
)
|
||||
func=widget_get_metadata_string, kwargs={'attribute': 'document'},
|
||||
source=DocumentPageSearchResult,
|
||||
)
|
||||
|
||||
SourceColumn(
|
||||
source=DocumentMetadata, label=_('Value'),
|
||||
attribute='value'
|
||||
attribute='metadata_type', is_identifier=True,
|
||||
source=DocumentMetadata
|
||||
)
|
||||
SourceColumn(attribute='value', source=DocumentMetadata)
|
||||
SourceColumn(
|
||||
attribute='is_required', source=DocumentMetadata,
|
||||
widget=TwoStateWidget
|
||||
)
|
||||
SourceColumn(
|
||||
source=DocumentMetadata, label=_('Required'),
|
||||
func=lambda context: TwoStateWidget(
|
||||
state=context['object'].is_required
|
||||
).render()
|
||||
attribute='label', is_identifier=True, source=MetadataType
|
||||
)
|
||||
SourceColumn(attribute='name', source=MetadataType)
|
||||
|
||||
app.conf.task_queues.append(
|
||||
Queue('metadata', Exchange('metadata'), routing_key='metadata'),
|
||||
|
||||
@@ -265,6 +265,7 @@ class DocumentMetadata(models.Model):
|
||||
return self.metadata_type.get_required_for(
|
||||
document_type=self.document.document_type
|
||||
)
|
||||
is_required.fget.short_description = _('Required')
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.metadata_type.pk not in self.document.document_type.metadata.values_list('metadata_type', flat=True):
|
||||
|
||||
@@ -402,7 +402,7 @@ class DocumentMetadataListView(SingleObjectListView):
|
||||
def get_extra_context(self):
|
||||
document = self.get_document()
|
||||
return {
|
||||
'hide_link': True,
|
||||
'hide_object': True,
|
||||
'object': document,
|
||||
'no_results_icon': icon_metadata,
|
||||
'no_results_main_link': link_metadata_add.resolve(
|
||||
@@ -618,13 +618,7 @@ class MetadataTypeListView(SingleObjectListView):
|
||||
|
||||
def get_extra_context(self):
|
||||
return {
|
||||
'extra_columns': (
|
||||
{
|
||||
'name': _('Internal name'),
|
||||
'attribute': 'name',
|
||||
},
|
||||
),
|
||||
'hide_link': True,
|
||||
'hide_object': True,
|
||||
'no_results_icon': icon_metadata,
|
||||
'no_results_main_link': link_setup_metadata_type_create.resolve(
|
||||
context=RequestContext(request=self.request)
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.html import format_html_join
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
def get_metadata_string(document):
|
||||
def widget_get_metadata_string(context, attribute=None):
|
||||
"""
|
||||
Return a formated representation of a document's metadata values
|
||||
"""
|
||||
obj = context['object']
|
||||
|
||||
if attribute:
|
||||
obj = getattr(context['object'], attribute)
|
||||
|
||||
return format_html_join(
|
||||
'\n', '<div class="metadata-display"><b>{}: </b><span data-metadata-type="{}" data-pk="{}">{}</span></div>',
|
||||
(
|
||||
(
|
||||
document_metadata.metadata_type, document_metadata.metadata_type_id, document_metadata.id, document_metadata.value
|
||||
) for document_metadata in document.metadata.all()
|
||||
) for document_metadata in obj.metadata.all()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
widget_get_metadata_string.short_description = _('Metadata')
|
||||
|
||||
Reference in New Issue
Block a user