Пример #1
0
def detach_tags(request):
    def get_type(type_str):
        """ find the lookup class for the named channel.  this is used internally """
        lookup_label = type_str.rsplit(".", 1)
        lookup_module = __import__(lookup_label[0], {}, {}, [""])
        lookup_class = getattr(lookup_module, lookup_label[1])
        return lookup_class()

    if request.method == "POST":  # If the form has been submitted...
        form = SimpleTagAddForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            ret = ""
            object_id = form.cleaned_data["object_id"]
            type = get_type(form.cleaned_data["object_type"])
            object = get_object_or_404(type, pk=object_id)
            for tag in form.cleaned_data["tag_list"].split(","):
                tag = tag.strip()
                try:
                    if tag.startswith("id="):
                        tago = Tag.objects.get(id=int(tag[3:]))
                    else:
                        tago = Tag.objects.get(text=tag)

                    object.remove_tags(tago)
                    ret += "OK:" + str(tago.id) + ":" + tago.text + "|"
                except Exception as e:
                    # TODO message to user
                    if tag.startswith("id="):
                        tag = tag[3:]
                    ret += "EE:" + tag + "|"
                    continue

            #            return "OK"
            return HttpResponse(ret)
        else:
            # return HttpResponseRedirect('/')
            pass
    else:
        # return HttpResponseRedirect('/')
        form = SimpleTagAddForm()  # An unbound form

    return render_to_response("slogans/add_tag.html", {"form": form}, context_instance=RequestContext(request))
Пример #2
0
def attach_tags(request):
    def get_type(type_str):
        """ find the lookup class for the named channel.  this is used internally """
        lookup_label = type_str.rsplit(".", 1)
        lookup_module = __import__(lookup_label[0], {}, {}, [""])
        lookup_class = getattr(lookup_module, lookup_label[1])
        return lookup_class()

    if request.method == "POST":  # If the form has been submitted...
        form = SimpleTagAddForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            # Process the data in form.cleaned_data
            ret = ""
            object_id = form.cleaned_data["object_id"]
            type = get_type(form.cleaned_data["object_type"])
            object = get_object_or_404(type, pk=object_id)
            for tag in form.cleaned_data["tag_list"].split(","):
                tag = tag.strip()
                try:
                    if tag.startswith("id="):
                        tag = tag[3:]
                        tago = Tag.objects.get(id=int(tag))
                    elif tag.startswith("new="):
                        tag = tag[4:]
                        count = Tag.objects.filter(text__iexact=tag).count()
                        if count == 1:
                            tago = Tag.objects.filter(text__iexact=tag)[0]
                        elif count > 1:
                            count = Tag.objects.filter(text=tag).count()
                            if count == 1:
                                tago = Tag.objects.filter(text=tag)[0]
                            elif count > 1:
                                ret += "WW:" + tag + "|"
                                continue
                        else:
                            tago = Tag()
                            tago.text = tag
                            tago.save()

                    else:
                        tago = Tag.objects.get(text=tag)
                    object.add_tags(tago)
                    ret += "OK:" + str(tago.id) + ":" + tago.text + "|"
                    # else:
                    #    ret += "EE:" + tag + "|"
                except:
                    # TODO message to user
                    ret += "EE:" + tag + "|"
                    continue

            #            return "OK"
            return HttpResponse(ret)
            # return HttpResponseRedirect(reverse('truemen.slogans.views.slogan',
            #                                    args=(slogan_id,))) # Redirect after POST
        else:
            # return HttpResponseRedirect('/')
            pass
    else:
        # return HttpResponseRedirect('/')
        form = SimpleTagAddForm()  # An unbound form

    return render_to_response("slogans/add_tag.html", {"form": form}, context_instance=RequestContext(request))