From d9fb0080af722bd0fffc4680af0aba29e9f3c718 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 24 Aug 2015 01:53:07 -0400 Subject: [PATCH] Use proxy document tag to simplify accessing a document's tags. --- mayan/apps/tags/api_views.py | 4 ++-- mayan/apps/tags/apps.py | 11 +++++++++-- mayan/apps/tags/links.py | 2 +- mayan/apps/tags/models.py | 9 ++++++++- mayan/apps/tags/views.py | 6 +++--- mayan/apps/tags/widgets.py | 4 ++-- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/mayan/apps/tags/api_views.py b/mayan/apps/tags/api_views.py index 79726395de..19dad396b0 100644 --- a/mayan/apps/tags/api_views.py +++ b/mayan/apps/tags/api_views.py @@ -133,7 +133,7 @@ class APIDocumentTagListView(generics.ListCreateAPIView): permission_document_view, self.request.user, document ) - return document.tags.all() + return document.tags().all() def get_serializer_context(self): """ @@ -199,7 +199,7 @@ class APIDocumentTagView(generics.RetrieveDestroyAPIView): return document def get_queryset(self): - return self.get_document().tags.all() + return self.get_document().tags().all() def get_serializer_context(self): """ diff --git a/mayan/apps/tags/apps.py b/mayan/apps/tags/apps.py index 73dd50f04e..3f46edca2c 100644 --- a/mayan/apps/tags/apps.py +++ b/mayan/apps/tags/apps.py @@ -20,7 +20,7 @@ from .links import ( link_tag_delete, link_tag_document_list, link_tag_edit, link_tag_list, link_tag_multiple_delete, link_tag_tagged_item_list ) -from .models import Tag +from .models import DocumentTag, Tag from .permissions import ( permission_tag_attach, permission_tag_delete, permission_tag_edit, permission_tag_remove, permission_tag_view @@ -37,6 +37,8 @@ class TagsApp(MayanAppConfig): APIEndPoint(app=self, version_string='1') + Document.add_to_class('tags', lambda document: DocumentTag.objects.filter(documents=document)) + ModelPermission.register( model=Document, permissions=( permission_tag_attach, permission_tag_remove, @@ -52,6 +54,11 @@ class TagsApp(MayanAppConfig): ) ) + SourceColumn( + source=DocumentTag, label=_('Preview'), + func=lambda context: widget_single_tag(context['object']) + ) + SourceColumn( source=Document, label=_('Tags'), func=lambda context: widget_inline_tags(context['object']) @@ -86,7 +93,7 @@ class TagsApp(MayanAppConfig): ) menu_multi_item.bind_links( links=(link_single_document_multiple_tag_remove,), - sources=[CombinedSource(obj=Tag, view='tags:document_tags')] + sources=(DocumentTag,) ) menu_object.bind_links( links=( diff --git a/mayan/apps/tags/links.py b/mayan/apps/tags/links.py index bc248453b4..cd59a9df13 100644 --- a/mayan/apps/tags/links.py +++ b/mayan/apps/tags/links.py @@ -18,7 +18,7 @@ link_multiple_documents_attach_tag = Link( ) link_single_document_multiple_tag_remove = Link( permissions=(permission_tag_remove,), text=_('Remove tags'), - view='tags:single_document_multiple_tag_remove', args='document.id' + view='tags:single_document_multiple_tag_remove', args='object.id' ) link_tag_attach = Link( permissions=(permission_tag_attach,), text=_('Attach tag'), diff --git a/mayan/apps/tags/models.py b/mayan/apps/tags/models.py index 79f2f2b128..8920030230 100644 --- a/mayan/apps/tags/models.py +++ b/mayan/apps/tags/models.py @@ -20,7 +20,7 @@ class Tag(models.Model): ) color = RGBColorField(verbose_name=_('Color')) documents = models.ManyToManyField( - Document, related_name='tags', verbose_name=_('Documents') + Document, verbose_name=_('Documents') ) class Meta: @@ -41,3 +41,10 @@ class Tag(models.Model): ) return queryset.count() + + +class DocumentTag(Tag): + class Meta: + proxy = True + verbose_name = _('Document tag') + verbose_name_plural = _('Document tags') diff --git a/mayan/apps/tags/views.py b/mayan/apps/tags/views.py index 16bef84245..c5c5dddae7 100644 --- a/mayan/apps/tags/views.py +++ b/mayan/apps/tags/views.py @@ -74,7 +74,7 @@ def tag_attach(request, document_id=None, document_id_list=None): if form.is_valid(): tag = form.cleaned_data['tag'] for document in documents: - if tag in document.tags.all(): + if tag in document.tags().all(): messages.warning( request, _( 'Document "%(document)s" is already tagged as "%(tag)s"' @@ -263,7 +263,7 @@ class DocumentTagListView(TagListView): } def get_tag_queryset(self): - return self.document.tags.all() + return self.document.tags().all() def tag_remove(request, document_id=None, document_id_list=None, tag_id=None, tag_id_list=None): @@ -369,7 +369,7 @@ def tag_remove(request, document_id=None, document_id_list=None, tag_id=None, ta if request.method == 'POST': for document in documents: for tag in tags: - if tag not in document.tags.all(): + if tag not in document.tags().all(): messages.warning( request, _( 'Document "%(document)s" wasn\'t tagged as "%(tag)s"' diff --git a/mayan/apps/tags/widgets.py b/mayan/apps/tags/widgets.py index 3afb666ad1..8e51e4ec07 100644 --- a/mayan/apps/tags/widgets.py +++ b/mayan/apps/tags/widgets.py @@ -10,9 +10,9 @@ def widget_inline_tags(document): """ tags_template = [] - tag_count = document.tags.count() + tag_count = document.tags().count() if tag_count: - for tag in document.tags.all(): + for tag in document.tags().all(): tags_template.append(widget_single_tag(tag)) return mark_safe(''.join(tags_template))