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:
@@ -104,10 +104,7 @@ from .queues import * # NOQA
|
||||
from .search import document_page_search, document_search # NOQA
|
||||
from .signals import post_version_upload
|
||||
from .statistics import * # NOQA
|
||||
from .widgets import (
|
||||
DocumentPageThumbnailWidget, widget_document_page_number,
|
||||
widget_document_version_page_number
|
||||
)
|
||||
from .widgets import DocumentPageThumbnailWidget
|
||||
|
||||
|
||||
class DocumentsApp(MayanAppConfig):
|
||||
@@ -129,7 +126,7 @@ class DocumentsApp(MayanAppConfig):
|
||||
DocumentType = self.get_model('DocumentType')
|
||||
DocumentTypeFilename = self.get_model('DocumentTypeFilename')
|
||||
DocumentVersion = self.get_model('DocumentVersion')
|
||||
DuplicatedDocument = self.get_model('DuplicatedDocument')
|
||||
DuplicatedDocumentProxy = self.get_model('DuplicatedDocumentProxy')
|
||||
|
||||
DynamicSerializerField.add_serializer(
|
||||
klass=Document,
|
||||
@@ -244,9 +241,7 @@ class DocumentsApp(MayanAppConfig):
|
||||
SourceColumn(
|
||||
attribute='document_type', label=_('Type'), source=Document
|
||||
)
|
||||
SourceColumn(
|
||||
func=widget_document_page_number, label=_('Pages'), source=Document
|
||||
)
|
||||
SourceColumn(attribute='get_page_count', source=Document)
|
||||
|
||||
# DocumentPage
|
||||
SourceColumn(
|
||||
@@ -274,9 +269,8 @@ class DocumentsApp(MayanAppConfig):
|
||||
)
|
||||
|
||||
SourceColumn(
|
||||
func=lambda context: TwoStateWidget(
|
||||
state=context['object'].enabled
|
||||
).render(), label=_('Enabled'), source=DocumentTypeFilename
|
||||
attribute='enabled', source=DocumentTypeFilename,
|
||||
widget=TwoStateWidget
|
||||
)
|
||||
|
||||
# DeletedDocument
|
||||
@@ -290,41 +284,34 @@ class DocumentsApp(MayanAppConfig):
|
||||
attribute='document_type', source=DeletedDocument
|
||||
)
|
||||
SourceColumn(
|
||||
attribute='deleted_date_time', source=DeletedDocument
|
||||
attribute='get_rendered_deleted_date_time', source=DeletedDocument
|
||||
)
|
||||
|
||||
# DocumentVersion
|
||||
SourceColumn(
|
||||
attribute='get_rendered_timestamp', is_identifier=True,
|
||||
source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
func=lambda context: document_page_thumbnail_widget.render(
|
||||
instance=context['object']
|
||||
), label=_('Thumbnail'), source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
attribute='timestamp', source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
func=widget_document_version_page_number, label=_('Pages'),
|
||||
source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
attribute='mimetype', source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
attribute='encoding', source=DocumentVersion
|
||||
)
|
||||
SourceColumn(
|
||||
attribute='comment', source=DocumentVersion
|
||||
)
|
||||
SourceColumn(attribute='get_page_count', source=DocumentVersion)
|
||||
SourceColumn(attribute='mimetype', source=DocumentVersion)
|
||||
SourceColumn(attribute='encoding', source=DocumentVersion)
|
||||
SourceColumn(attribute='comment', source=DocumentVersion)
|
||||
|
||||
# DuplicatedDocument
|
||||
SourceColumn(
|
||||
func=lambda context: document_page_thumbnail_widget.render(
|
||||
instance=context['object'].document
|
||||
), label=_('Thumbnail'), source=DuplicatedDocument
|
||||
instance=context['object']
|
||||
), label=_('Thumbnail'), source=DuplicatedDocumentProxy
|
||||
)
|
||||
SourceColumn(
|
||||
func=lambda context: context['object'].documents.count(),
|
||||
label=_('Duplicates'), source=DuplicatedDocument
|
||||
func=lambda context: context['object'].get_duplicate_count(
|
||||
user=context['request'].user,
|
||||
), label=_('Duplicates'), source=DuplicatedDocumentProxy
|
||||
)
|
||||
|
||||
app.conf.beat_schedule.update(
|
||||
|
||||
@@ -128,10 +128,10 @@ class DuplicatedDocumentManager(models.Manager):
|
||||
self.filter(documents=None).delete()
|
||||
|
||||
def get_duplicated_documents(self):
|
||||
Document = apps.get_model(
|
||||
app_label='documents', model_name='Document'
|
||||
DuplicatedDocumentProxy = apps.get_model(
|
||||
app_label='documents', model_name='DuplicatedDocumentProxy'
|
||||
)
|
||||
return Document.objects.filter(
|
||||
return DuplicatedDocumentProxy.objects.filter(
|
||||
pk__in=self.filter(documents__isnull=False).values_list(
|
||||
'document_id', flat=True
|
||||
)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.16 on 2018-12-22 09:30
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('documents', '0049_auto_20181211_0011'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='DuplicatedDocumentProxy',
|
||||
fields=[
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Duplicated document',
|
||||
'proxy': True,
|
||||
'verbose_name_plural': 'Duplicated documents',
|
||||
'indexes': [],
|
||||
},
|
||||
bases=('documents.document',),
|
||||
),
|
||||
]
|
||||
@@ -7,12 +7,15 @@ from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.core.files import File
|
||||
from django.db import models
|
||||
from django.template import Context, Template
|
||||
from django.urls import reverse
|
||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.acls.models import AccessControlList
|
||||
|
||||
from ..events import (
|
||||
event_document_create, event_document_properties_edit,
|
||||
event_document_type_change,
|
||||
@@ -21,6 +24,7 @@ from ..managers import (
|
||||
DocumentManager, DuplicatedDocumentManager, FavoriteDocumentManager,
|
||||
PassthroughManager, RecentDocumentManager, TrashCanManager
|
||||
)
|
||||
from ..permissions import permission_document_view
|
||||
from ..settings import setting_language
|
||||
from ..signals import post_document_type_change
|
||||
|
||||
@@ -77,7 +81,6 @@ class Document(models.Model):
|
||||
'Whether or not this document is in the trash.'
|
||||
), editable=False, verbose_name=_('In trash?')
|
||||
)
|
||||
# TODO: set editable to False
|
||||
deleted_date_time = models.DateTimeField(
|
||||
blank=True, editable=True, help_text=_(
|
||||
'The server date and time when the document was moved to the '
|
||||
@@ -139,6 +142,24 @@ class Document(models.Model):
|
||||
if latest_version:
|
||||
return latest_version.get_api_image_url(*args, **kwargs)
|
||||
|
||||
def get_duplicates(self):
|
||||
try:
|
||||
return DuplicatedDocument.objects.get(document=self).documents.all()
|
||||
except DuplicatedDocument.DoesNotExist:
|
||||
return Document.objects.none()
|
||||
|
||||
def get_page_count(self):
|
||||
return self.pages.count()
|
||||
get_page_count.short_description = _('Pages')
|
||||
|
||||
def get_rendered_deleted_date_time(self):
|
||||
return Template('{{ instance.deleted_date_time }}').render(
|
||||
context=Context({'instance': self})
|
||||
)
|
||||
get_rendered_deleted_date_time.short_description = _(
|
||||
'Date and time trashed'
|
||||
)
|
||||
|
||||
def invalidate_cache(self):
|
||||
for document_version in self.versions.all():
|
||||
document_version.invalidate_cache()
|
||||
@@ -285,6 +306,20 @@ class DuplicatedDocument(models.Model):
|
||||
return force_text(self.document)
|
||||
|
||||
|
||||
class DuplicatedDocumentProxy(Document):
|
||||
class Meta:
|
||||
proxy = True
|
||||
verbose_name = _('Duplicated document')
|
||||
verbose_name_plural = _('Duplicated documents')
|
||||
|
||||
def get_duplicate_count(self, user):
|
||||
queryset = AccessControlList.objects.filter_by_access(
|
||||
permission=permission_document_view, user=user,
|
||||
queryset=self.get_duplicates()
|
||||
)
|
||||
return queryset.count()
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class FavoriteDocument(models.Model):
|
||||
"""
|
||||
|
||||
@@ -87,10 +87,11 @@ class DocumentType(models.Model):
|
||||
|
||||
def get_document_count(self, user):
|
||||
queryset = AccessControlList.objects.filter_by_access(
|
||||
permission_document_view, user, queryset=self.documents
|
||||
permission=permission_document_view, user=user,
|
||||
queryset=self.documents
|
||||
)
|
||||
|
||||
return queryset.count()
|
||||
get_document_count.short_description = _('Documents')
|
||||
|
||||
def natural_key(self):
|
||||
return (self.label,)
|
||||
|
||||
@@ -181,6 +181,10 @@ class DocumentVersion(models.Model):
|
||||
)
|
||||
raise
|
||||
|
||||
def get_page_count(self):
|
||||
return self.pages.count()
|
||||
get_page_count.short_description = _('Pages')
|
||||
|
||||
def get_rendered_string(self, preserve_extension=False):
|
||||
if preserve_extension:
|
||||
filename, extension = os.path.splitext(self.document.label)
|
||||
@@ -196,6 +200,7 @@ class DocumentVersion(models.Model):
|
||||
return Template('{{ instance.timestamp }}').render(
|
||||
context=Context({'instance': self})
|
||||
)
|
||||
get_rendered_timestamp.short_description = _('Date and time')
|
||||
|
||||
def natural_key(self):
|
||||
return (self.checksum, self.document.natural_key())
|
||||
|
||||
@@ -20,7 +20,6 @@ from mayan.apps.common.generics import (
|
||||
SingleObjectDownloadView, SingleObjectEditView, SingleObjectListView
|
||||
)
|
||||
from mayan.apps.common.mixins import MultipleInstanceActionMixin
|
||||
from mayan.apps.common.utils import encapsulate
|
||||
from mayan.apps.converter.models import Transformation
|
||||
from mayan.apps.converter.permissions import (
|
||||
permission_transformation_delete, permission_transformation_edit
|
||||
@@ -259,12 +258,7 @@ class DocumentDuplicatesListView(DocumentListView):
|
||||
return context
|
||||
|
||||
def get_object_list(self):
|
||||
try:
|
||||
return DuplicatedDocument.objects.get(
|
||||
document=self.get_document()
|
||||
).documents.all()
|
||||
except DuplicatedDocument.DoesNotExist:
|
||||
return Document.objects.none()
|
||||
return self.get_document().get_duplicates()
|
||||
|
||||
|
||||
class DocumentEditView(SingleObjectEditView):
|
||||
@@ -842,16 +836,6 @@ class DuplicatedDocumentListView(DocumentListView):
|
||||
context = super(DuplicatedDocumentListView, self).get_extra_context()
|
||||
context.update(
|
||||
{
|
||||
'extra_columns': (
|
||||
{
|
||||
'name': _('Duplicates'),
|
||||
'attribute': encapsulate(
|
||||
lambda document: DuplicatedDocument.objects.get(
|
||||
document=document
|
||||
).documents.count()
|
||||
)
|
||||
},
|
||||
),
|
||||
'no_results_icon': icon_duplicated_document_list,
|
||||
'no_results_text': _(
|
||||
'Duplicates are documents that are composed of the exact '
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||
from django import forms
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .settings import (
|
||||
setting_display_height, setting_display_width, setting_preview_height,
|
||||
@@ -75,11 +74,3 @@ def document_link(document):
|
||||
return mark_safe('<a href="%s">%s</a>' % (
|
||||
document.get_absolute_url(), document)
|
||||
)
|
||||
|
||||
|
||||
def widget_document_page_number(context):
|
||||
return context['object'].pages.count()
|
||||
|
||||
|
||||
def widget_document_version_page_number(context):
|
||||
return context['object'].pages.count()
|
||||
|
||||
Reference in New Issue
Block a user