From 9a6a6495eed504423858d79aa7ffdb4f322d5fc5 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 10 Oct 2012 02:21:45 -0400 Subject: [PATCH] Improve bulk tagging and consolidate code --- apps/tags/__init__.py | 14 +++--- apps/tags/links.py | 9 ++-- apps/tags/urls.py | 13 +++--- apps/tags/views.py | 102 ++++++++++++++++++++++++------------------ 4 files changed, 78 insertions(+), 60 deletions(-) diff --git a/apps/tags/__init__.py b/apps/tags/__init__.py index c0e2eeb7e1..c931c6d46f 100644 --- a/apps/tags/__init__.py +++ b/apps/tags/__init__.py @@ -11,15 +11,14 @@ from acls.api import class_permissions from taggit.models import Tag from taggit.managers import TaggableManager -from .links import (tag_list, tag_create, tag_attach, tag_document_remove, - tag_document_remove_multiple, tag_document_list, tag_delete, - tag_edit, tag_tagged_item_list, tag_multiple_delete, tag_acl_list, - tag_multiple_attach, multiple_document_tag_remove) +from .links import (tag_list, tag_create, tag_attach, + tag_document_list, tag_delete, tag_edit, tag_tagged_item_list, + tag_multiple_delete, tag_acl_list, tag_multiple_attach, + single_document_multiple_tag_remove, multiple_documents_selection_tag_remove) from .widgets import (get_tags_inline_widget_simple, single_tag_widget) from .permissions import (PERMISSION_TAG_ATTACH, PERMISSION_TAG_REMOVE, PERMISSION_TAG_DELETE, PERMISSION_TAG_EDIT, PERMISSION_TAG_VIEW) - register_model_list_columns(Tag, [ { 'name': _(u'preview'), @@ -44,10 +43,9 @@ register_top_menu('tags', link={'text': _(u'tags'), 'view': 'tag_list', 'famfam' register_links(Document, [tag_document_list], menu_name='form_header') register_links(['document_tags', 'tag_remove', 'tag_multiple_remove', 'tag_attach'], [tag_attach], menu_name='sidebar') -register_multi_item_links(['document_tags'], [tag_document_remove_multiple]) +register_multi_item_links(['document_tags'], [single_document_multiple_tag_remove]) -register_multi_item_links(['document_find_duplicates', 'folder_view', 'index_instance_node_view', 'document_type_document_list', 'search', 'results', 'document_group_view', 'document_list', 'document_list_recent', 'tag_tagged_item_list'], [tag_multiple_attach]) -register_multi_item_links(['tag_tagged_item_list'], [multiple_document_tag_remove]) +register_multi_item_links(['document_find_duplicates', 'folder_view', 'index_instance_node_view', 'document_type_document_list', 'search', 'results', 'document_group_view', 'document_list', 'document_list_recent', 'tag_tagged_item_list'], [tag_multiple_attach, multiple_documents_selection_tag_remove]) class_permissions(Document, [ PERMISSION_TAG_ATTACH, diff --git a/apps/tags/links.py b/apps/tags/links.py index e5bbc6a8d2..7c28cfe6fc 100644 --- a/apps/tags/links.py +++ b/apps/tags/links.py @@ -10,11 +10,14 @@ from .permissions import (PERMISSION_TAG_CREATE, PERMISSION_TAG_ATTACH, tag_list = {'text': _(u'tag list'), 'view': 'tag_list', 'famfam': 'tag_blue'} tag_create = {'text': _(u'create new tag'), 'view': 'tag_create', 'famfam': 'tag_blue_add', 'permissions': [PERMISSION_TAG_CREATE]} + tag_attach = {'text': _(u'attach tag'), 'view': 'tag_attach', 'args': 'object.pk', 'famfam': 'tag_blue_add', 'permissions': [PERMISSION_TAG_ATTACH]} tag_multiple_attach = {'text': _(u'attach tag'), 'view': 'tag_multiple_attach', 'famfam': 'tag_blue_add'} -tag_document_remove = {'text': _(u'remove'), 'view': 'tag_remove', 'args': ['object.id', 'document.id'], 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_REMOVE]} -multiple_document_tag_remove = {'text': _(u'remove tag'), 'view': 'multiple_document_tag_remove', 'args': 'object.id', 'famfam': 'tag_blue_delete'} -tag_document_remove_multiple = {'text': _(u'remove'), 'view': 'tag_multiple_remove', 'args': 'document.id', 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_REMOVE]} + +#tag_remove = {'text': _(u'remove tag'), 'view': 'tag_remove', 'args': 'object.pk', 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_REMOVE]} +multiple_documents_selection_tag_remove = {'text': _(u'remove tag'), 'view': 'multiple_documents_selection_tag_remove', 'famfam': 'tag_blue_delete'} +single_document_multiple_tag_remove = {'text': _(u'remove tags'), 'view': 'single_document_multiple_tag_remove', 'args': 'document.id', 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_REMOVE]} + tag_document_list = {'text': _(u'tags'), 'view': 'document_tags', 'args': 'object.pk', 'famfam': 'tag_blue', 'permissions': [PERMISSION_TAG_REMOVE, PERMISSION_TAG_ATTACH], 'children_view_regex': ['tag']} tag_delete = {'text': _(u'delete'), 'view': 'tag_delete', 'args': 'object.id', 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_DELETE]} tag_edit = {'text': _(u'edit'), 'view': 'tag_edit', 'args': 'object.id', 'famfam': 'tag_blue_edit', 'permissions': [PERMISSION_TAG_EDIT]} diff --git a/apps/tags/urls.py b/apps/tags/urls.py index d29e86e6ec..4ec02272cc 100644 --- a/apps/tags/urls.py +++ b/apps/tags/urls.py @@ -8,12 +8,13 @@ urlpatterns = patterns('tags.views', url(r'^(?P\d+)/tagged_item/list/$', 'tag_tagged_item_list', (), 'tag_tagged_item_list'), url(r'^multiple/delete/$', 'tag_multiple_delete', (), 'tag_multiple_delete'), - url(r'^(?P\d+)/remove_from_document/(?P\d+)/$', 'tag_remove', (), 'tag_remove'), - url(r'^(?P\d+)/remove_from_document/multiple/$', 'multiple_document_tag_remove', (), 'multiple_document_tag_remove'), - url(r'^multiple/remove_from_document/(?P\d+)/$', 'tag_multiple_remove', (), 'tag_multiple_remove'), - url(r'^document/(?P\d+)/add/$', 'tag_attach', (), 'tag_attach'), - url(r'^document/multiple/add/$', 'tag_multiple_attach', (), 'tag_multiple_attach'), - url(r'^document/(?P\d+)/list/$', 'document_tags', (), 'document_tags'), + url(r'^multiple/remove/document/(?P\d+)/$', 'single_document_multiple_tag_remove', (), 'single_document_multiple_tag_remove'), + url(r'^multiple/remove/document/multiple/$', 'multiple_documents_selection_tag_remove', (), 'multiple_documents_selection_tag_remove'), + + url(r'^selection/attach/document/(?P\d+)/$', 'tag_attach', (), 'tag_attach'), + url(r'^selection/attach/document/multiple/$', 'tag_multiple_attach', (), 'tag_multiple_attach'), + + url(r'^for/document/(?P\d+)/$', 'document_tags', (), 'document_tags'), url(r'^(?P\d+)/acl/list/$', 'tag_acl_list', (), 'tag_acl_list'), ) diff --git a/apps/tags/views.py b/apps/tags/views.py index 26b0df578e..a9904a7a20 100644 --- a/apps/tags/views.py +++ b/apps/tags/views.py @@ -279,68 +279,84 @@ def tag_remove(request, document_id=None, document_id_list=None, tag_id=None, ta except PermissionDenied: documents = AccessEntry.objects.filter_objects_by_access(PERMISSION_TAG_REMOVE, request.user, documents, exception_on_empty=True) - if tag_id: - tags = [get_object_or_404(Tag, pk=tag_id)] - elif tag_id_list: - tags = [get_object_or_404(Tag, pk=tag_id) for tag_id in tag_id_list.split(',')] - else: - messages.error(request, _(u'Must provide at least one tag.')) - return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) - post_action_redirect = None previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', '/'))) - if request.method == 'POST': - for document in documents: - for tag in tags: - try: - document.tags.remove(tag) - messages.success(request, _(u'Tag "%(tag)s" removed successfully from document: %(document)s.') % { - 'tag': tag, 'document': document} - ) - except Exception, e: - messages.error(request, _(u'Error deleting tag "%(tag)s" from document "%(document)s"; %(error)s') % { - 'tag': tag, 'error': e, 'document': document - }) - - return HttpResponseRedirect(next) - context = { 'previous': previous, 'next': next, 'form_icon': u'tag_blue_delete.png', } - if len(tags) == 1: + template = 'generic_confirm.html' + if tag_id: + tags = [get_object_or_404(Tag, pk=tag_id)] + elif tag_id_list: + tags = [get_object_or_404(Tag, pk=tag_id) for tag_id in tag_id_list.split(',')] + else: + template = 'generic_form.html' + + if request.method == 'POST': + form = TagListForm(request.POST, user=request.user) + if form.is_valid(): + tags = [form.cleaned_data['tag']] + else: + if not tag_id and not tag_id_list: + form = TagListForm(user=request.user) + tags = None + + context['form'] = form if len(documents) == 1: context['object'] = documents[0] - context['title'] = _(u'Are you sure you wish to remove the tag "%(tag)s" from the document: %(document)s?') % { - 'tag': ', '.join([unicode(d) for d in tags]), 'document': ', '.join([unicode(d) for d in documents])} - else: - context['title'] = _(u'Are you sure you wish to remove the tag "%(tag)s" from the documents: %(documents)s?') % { - 'tag': ', '.join([unicode(d) for d in tags]), 'documents': ', '.join([unicode(d) for d in documents])} - elif len(tags) > 1: - if len(documents) == 1: - context['object'] = documents[0] - context['title'] = _(u'Are you sure you wish to remove the taqs: %(tags)s from the document: %(document)s?') % { - 'tags': ', '.join([unicode(d) for d in tags]), 'document': ', '.join([unicode(d) for d in documents])} - else: - context['title'] = _(u'Are you sure you wish to remove the tags %(tag)s from the documents: %(documents)s?') % { - 'tags': ', '.join([unicode(d) for d in tags]), 'documents': ', '.join([unicode(d) for d in documents])} - #context['title'] = _(u'Are you sure you wish to remove the tags: %s?') % ', '.join([unicode(d) for d in tags]) + context['title'] = _(u'Remove tag from document: %s.') % ', '.join([unicode(d) for d in documents]) + elif len(documents) > 1: + context['title'] = _(u'Remove tag from documents: %s.') % ', '.join([unicode(d) for d in documents]) - return render_to_response('generic_confirm.html', context, - context_instance=RequestContext(request)) + if tags: + if len(tags) == 1: + if len(documents) == 1: + context['object'] = documents[0] + context['title'] = _(u'Are you sure you wish to remove the tag "%(tag)s" from the document: %(document)s?') % { + 'tag': ', '.join([unicode(d) for d in tags]), 'document': ', '.join([unicode(d) for d in documents])} + else: + context['title'] = _(u'Are you sure you wish to remove the tag "%(tag)s" from the documents: %(documents)s?') % { + 'tag': ', '.join([unicode(d) for d in tags]), 'documents': ', '.join([unicode(d) for d in documents])} + elif len(tags) > 1: + if len(documents) == 1: + context['object'] = documents[0] + context['title'] = _(u'Are you sure you wish to remove the tags: %(tags)s from the document: %(document)s?') % { + 'tags': ', '.join([unicode(d) for d in tags]), 'document': ', '.join([unicode(d) for d in documents])} + else: + context['title'] = _(u'Are you sure you wish to remove the tags %(tag)s from the documents: %(documents)s?') % { + 'tags': ', '.join([unicode(d) for d in tags]), 'documents': ', '.join([unicode(d) for d in documents])} + + if request.method == 'POST': + for document in documents: + for tag in tags: + if tag not in document.tags.all(): + messages.warning(request, _(u'Document "%(document)s" wasn\'t tagged as "%(tag)s"') % { + 'document': document, 'tag': tag} + ) + else: + document.tags.remove(tag) + messages.success(request, _(u'Tag "%(tag)s" removed successfully from document "%(document)s".') % { + 'document': document, 'tag': tag} + ) + + return HttpResponseRedirect(next) + else: + return render_to_response(template, context, + context_instance=RequestContext(request)) -def tag_multiple_remove(request, document_id): +def single_document_multiple_tag_remove(request, document_id): return tag_remove(request, document_id=document_id, tag_id_list=request.GET.get('id_list', [])) -def multiple_document_tag_remove(request, tag_id): - return tag_remove(request, tag_id=tag_id, document_id_list=request.GET.get('id_list', [])) +def multiple_documents_selection_tag_remove(request): + return tag_remove(request, document_id_list=request.GET.get('id_list', [])) def tag_acl_list(request, tag_pk):