Example #1
0
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))        
Example #2
0
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', reverse('tag_list'))))

    if request.method == 'POST':
        previous = request.META.get('HTTP_REFERER', '/')
        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(previous)

            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(previous)

            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 successfully.') % tag_name)

    return HttpResponseRedirect(previous)
Example #3
0
def tag(request):
    """ Tags a thing using an AddTagForm
    
    Returns a JSON response with an "obj" and "key" porperty. "obj" is the object that was tagged, while "type" is it's
    type. If there is an error, a JSON object with an "error" key is returned instead.
    """
    f = AddTagForm(request.POST)
    
    if not f.is_valid():
        return HttpResponse('{"error":"Form invalid"}', content_type="application/json", status=422)
    
    taggable = tags.lookup_taggable(f.cleaned_data["type"])
    
    if taggable is None:
        return HttpResponse('{"error":"Taggable type invalid"}', content_type="application/json", status=422)
    
    try:
        obj = taggable.objects.get(owner=request.user, pk=f.cleaned_data["pk"])
    except taggable.DoesNotExist:
        return HttpResponse('{"error":"Taggable not found"}', content_type="application/json", status=422)
    
    tag_names = map(lambda x: x.strip(), f.cleaned_data["name"].split(","))
    
    for n in tag_names:
        if not n:
            continue
        
        try:
            tag = Tag.objects.get(owner=request.user, slug=makeslug(n))
        except Tag.DoesNotExist:
            tag = Tag(owner=request.user, name=n, colour=f.instance.colour)
            tag.save()
        
        obj.tag(tag)
    
    return HttpResponse(
        '{{"obj":{}, "type":"{}"}}'.format(obj.to_json(), f.cleaned_data["type"]),
        content_type="application/json"
    )
Example #4
0
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)))

    if request.method == 'POST':
        previous = request.META.get('HTTP_REFERER', '/')
        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(previous)

            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(previous)

            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 successfully.') % tag_name)

    return HttpResponseRedirect(previous)
Example #5
0
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))