Use proxy document tag to simplify accessing a document's tags.
This commit is contained in:
@@ -133,7 +133,7 @@ class APIDocumentTagListView(generics.ListCreateAPIView):
|
|||||||
permission_document_view, self.request.user, document
|
permission_document_view, self.request.user, document
|
||||||
)
|
)
|
||||||
|
|
||||||
return document.tags.all()
|
return document.tags().all()
|
||||||
|
|
||||||
def get_serializer_context(self):
|
def get_serializer_context(self):
|
||||||
"""
|
"""
|
||||||
@@ -199,7 +199,7 @@ class APIDocumentTagView(generics.RetrieveDestroyAPIView):
|
|||||||
return document
|
return document
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.get_document().tags.all()
|
return self.get_document().tags().all()
|
||||||
|
|
||||||
def get_serializer_context(self):
|
def get_serializer_context(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from .links import (
|
|||||||
link_tag_delete, link_tag_document_list, link_tag_edit, link_tag_list,
|
link_tag_delete, link_tag_document_list, link_tag_edit, link_tag_list,
|
||||||
link_tag_multiple_delete, link_tag_tagged_item_list
|
link_tag_multiple_delete, link_tag_tagged_item_list
|
||||||
)
|
)
|
||||||
from .models import Tag
|
from .models import DocumentTag, Tag
|
||||||
from .permissions import (
|
from .permissions import (
|
||||||
permission_tag_attach, permission_tag_delete, permission_tag_edit,
|
permission_tag_attach, permission_tag_delete, permission_tag_edit,
|
||||||
permission_tag_remove, permission_tag_view
|
permission_tag_remove, permission_tag_view
|
||||||
@@ -37,6 +37,8 @@ class TagsApp(MayanAppConfig):
|
|||||||
|
|
||||||
APIEndPoint(app=self, version_string='1')
|
APIEndPoint(app=self, version_string='1')
|
||||||
|
|
||||||
|
Document.add_to_class('tags', lambda document: DocumentTag.objects.filter(documents=document))
|
||||||
|
|
||||||
ModelPermission.register(
|
ModelPermission.register(
|
||||||
model=Document, permissions=(
|
model=Document, permissions=(
|
||||||
permission_tag_attach, permission_tag_remove,
|
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(
|
SourceColumn(
|
||||||
source=Document, label=_('Tags'),
|
source=Document, label=_('Tags'),
|
||||||
func=lambda context: widget_inline_tags(context['object'])
|
func=lambda context: widget_inline_tags(context['object'])
|
||||||
@@ -86,7 +93,7 @@ class TagsApp(MayanAppConfig):
|
|||||||
)
|
)
|
||||||
menu_multi_item.bind_links(
|
menu_multi_item.bind_links(
|
||||||
links=(link_single_document_multiple_tag_remove,),
|
links=(link_single_document_multiple_tag_remove,),
|
||||||
sources=[CombinedSource(obj=Tag, view='tags:document_tags')]
|
sources=(DocumentTag,)
|
||||||
)
|
)
|
||||||
menu_object.bind_links(
|
menu_object.bind_links(
|
||||||
links=(
|
links=(
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ link_multiple_documents_attach_tag = Link(
|
|||||||
)
|
)
|
||||||
link_single_document_multiple_tag_remove = Link(
|
link_single_document_multiple_tag_remove = Link(
|
||||||
permissions=(permission_tag_remove,), text=_('Remove tags'),
|
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(
|
link_tag_attach = Link(
|
||||||
permissions=(permission_tag_attach,), text=_('Attach tag'),
|
permissions=(permission_tag_attach,), text=_('Attach tag'),
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class Tag(models.Model):
|
|||||||
)
|
)
|
||||||
color = RGBColorField(verbose_name=_('Color'))
|
color = RGBColorField(verbose_name=_('Color'))
|
||||||
documents = models.ManyToManyField(
|
documents = models.ManyToManyField(
|
||||||
Document, related_name='tags', verbose_name=_('Documents')
|
Document, verbose_name=_('Documents')
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -41,3 +41,10 @@ class Tag(models.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return queryset.count()
|
return queryset.count()
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentTag(Tag):
|
||||||
|
class Meta:
|
||||||
|
proxy = True
|
||||||
|
verbose_name = _('Document tag')
|
||||||
|
verbose_name_plural = _('Document tags')
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ def tag_attach(request, document_id=None, document_id_list=None):
|
|||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
tag = form.cleaned_data['tag']
|
tag = form.cleaned_data['tag']
|
||||||
for document in documents:
|
for document in documents:
|
||||||
if tag in document.tags.all():
|
if tag in document.tags().all():
|
||||||
messages.warning(
|
messages.warning(
|
||||||
request, _(
|
request, _(
|
||||||
'Document "%(document)s" is already tagged as "%(tag)s"'
|
'Document "%(document)s" is already tagged as "%(tag)s"'
|
||||||
@@ -263,7 +263,7 @@ class DocumentTagListView(TagListView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def get_tag_queryset(self):
|
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):
|
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':
|
if request.method == 'POST':
|
||||||
for document in documents:
|
for document in documents:
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
if tag not in document.tags.all():
|
if tag not in document.tags().all():
|
||||||
messages.warning(
|
messages.warning(
|
||||||
request, _(
|
request, _(
|
||||||
'Document "%(document)s" wasn\'t tagged as "%(tag)s"'
|
'Document "%(document)s" wasn\'t tagged as "%(tag)s"'
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ def widget_inline_tags(document):
|
|||||||
"""
|
"""
|
||||||
tags_template = []
|
tags_template = []
|
||||||
|
|
||||||
tag_count = document.tags.count()
|
tag_count = document.tags().count()
|
||||||
if tag_count:
|
if tag_count:
|
||||||
for tag in document.tags.all():
|
for tag in document.tags().all():
|
||||||
tags_template.append(widget_single_tag(tag))
|
tags_template.append(widget_single_tag(tag))
|
||||||
|
|
||||||
return mark_safe(''.join(tags_template))
|
return mark_safe(''.join(tags_template))
|
||||||
|
|||||||
Reference in New Issue
Block a user