Added new tag add/attach view and disabled the previous side bar tag template

This commit is contained in:
Roberto Rosario
2011-07-03 01:06:37 -04:00
parent a6a469ad9c
commit 3c4f651ebd
4 changed files with 61 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ register_permission(PERMISSION_TAG_EDIT)
register_permission(PERMISSION_TAG_VIEW)
tag_list = {'text': _(u'tag list'), 'view': 'tag_list', 'famfam': 'tag_blue'}
tag_add_attach = {'text': _(u'attach tag'), 'view': 'tag_add_attach', 'args': 'object.pk', 'famfam': 'tag_blue_add', 'permission': [PERMISSION_TAG_CREATE, PERMISSION_TAG_ATTACH]}
tag_document_remove = {'text': _(u'remove'), 'view': 'tag_remove', 'args': ['object.id', 'document.id'], 'famfam': 'tag_blue_delete', 'permissions': [PERMISSION_TAG_REMOVE]}
tag_document_remove_multiple = {'text': _(u'remove'), 'view': 'tag_multiple_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]}
@@ -52,10 +53,11 @@ register_multi_item_links(['tag_list'], [tag_multiple_delete])
register_links(['tag_list', 'tag_delete', 'tag_edit', 'tag_tagged_item_list', 'tag_multiple_delete'], [tag_list], menu_name='secondary_menu')
register_sidebar_template(['document_tags'], 'tags_sidebar_template.html')
#register_sidebar_template(['document_tags'], 'tags_sidebar_template.html')
register_top_menu('tags', link={'text': _(u'tags'), 'view': 'tag_list', 'famfam': 'tag_blue'}, children_path_regex=[r'^tags/[^d]/'])
register_top_menu('tags', link={'text': _(u'tags'), 'view': 'tag_list', 'famfam': 'tag_blue'}, children_path_regex=[r'^tags/[^d]'])
register_links(Document, [tag_document_list], menu_name='form_header')
register_links(['document_tags', 'tag_add_attach', 'tag_remove', 'tag_multiple_remove'], [tag_add_attach], menu_name='sidebar')
register_multi_item_links(['document_tags'], [tag_document_remove_multiple])

View File

@@ -13,7 +13,7 @@ def get_add_tag_to_document_form(context):
context.update({
'form': AddTagForm(),
'request': context['request'],
'form_action': reverse('tag_add', args=[context['document'].pk]),
'form_action': reverse('tag_add_sidebar', args=[context['document'].pk]),
'title': _('Add tag to document')
})
return context

View File

@@ -9,6 +9,7 @@ urlpatterns = patterns('tags.views',
url(r'^(?P<tag_id>\d+)/remove_from_document/(?P<document_id>\d+)/$', 'tag_remove', (), 'tag_remove'),
url(r'^multiple/remove_from_document/(?P<document_id>\d+)/$', 'tag_multiple_remove', (), 'tag_multiple_remove'),
url(r'^document/(?P<document_id>\d+)/add/$', 'tag_add', (), 'tag_add'),
url(r'^document/(?P<document_id>\d+)/add/$', 'tag_add_attach', (), 'tag_add_attach'),
url(r'^document/(?P<document_id>\d+)/add/from_sidebar/$', 'tag_add_sidebar', (), 'tag_add_sidebar'),
url(r'^document/(?P<document_id>\d+)/list/$', 'document_tags', (), 'document_tags'),
)

View File

@@ -18,7 +18,7 @@ from tags import PERMISSION_TAG_CREATE, PERMISSION_TAG_ATTACH, \
from tags import tag_tagged_item_list as tag_tagged_item_list_link
def tag_add(request, document_id):
def tag_add_sidebar(request, document_id):
document = get_object_or_404(Document, pk=document_id)
previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', None)))
@@ -57,6 +57,56 @@ def tag_add(request, document_id):
return HttpResponseRedirect(previous)
def tag_add_attach(request, document_id):
# TODO: merge with tag_add_sidebar
document = get_object_or_404(Document, pk=document_id)
next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', reverse('document_tags', args=[document.pk]))))
if request.method == 'POST':
form = AddTagForm(request.POST)
if form.is_valid():
if form.cleaned_data['new_tag']:
check_permissions(request.user, [PERMISSION_TAG_CREATE])
tag_name = form.cleaned_data['new_tag']
if Tag.objects.filter(name=tag_name):
is_new = False
else:
is_new = True
elif form.cleaned_data['existing_tags']:
check_permissions(request.user, [PERMISSION_TAG_ATTACH])
tag_name = form.cleaned_data['existing_tags']
is_new = False
else:
messages.error(request, _(u'Must choose either a new tag or an existing one.'))
return HttpResponseRedirect(next)
if tag_name in document.tags.values_list('name', flat=True):
messages.warning(request, _(u'Document is already tagged as "%s"') % tag_name)
return HttpResponseRedirect(next)
document.tags.add(tag_name)
if is_new:
tag = Tag.objects.get(name=tag_name)
TagProperties(tag=tag, color=form.cleaned_data['color']).save()
messages.success(request, _(u'Tag "%s" added and attached successfully.') % tag_name)
else:
messages.success(request, _(u'Tag "%s" attached successfully.') % tag_name)
return HttpResponseRedirect(next)
else:
form = AddTagForm()
return render_to_response('generic_form.html', {
'title': _(u'attach tag to: %s') % document,
'form': form,
'object': document,
'next': next,
},
context_instance=RequestContext(request))
def tag_list(request):
return render_to_response('generic_list.html', {
'object_list': Tag.objects.all(),
@@ -149,6 +199,7 @@ def tag_edit(request, tag_id):
'title': _(u'edit tag: %s') % tag,
'form': form,
'object': tag,
'object_name': _(u'tag'),
},
context_instance=RequestContext(request))
@@ -162,7 +213,8 @@ def tag_tagged_item_list(request, tag_id):
object_list=object_list,
title=_('documents with the tag "%s"') % tag,
extra_context={
'object': tag
'object': tag,
'object_name': _(u'tag'),
}
)