def pictures(self): try: return self._cache['pictures'] except: from htags.models import Tag self._cache['pictures'] = Tag.complex_filter(str(self.query), Picture).distinct().order_by(self.sortby) return self._cache['pictures']
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))
def ajax_get_tag_info(request): def clean(st): """ fall 1: <> or foo | <> and foo ==> foo fall 2: foo or <> | foo and <> ==> foo fall 3: foo or <> or bar ==> foo or bar fall 4: foo and <> or bar ==> foo or bar fall 5: foo or <> and bar ==> foo or bar """ import re pos = st.find("<>") begin = old_begin = st.find("(", 0, pos) while begin > 0: old_begin = begin begin = st.find("(", begin + 1, pos) begin = old_begin + 1 end = st.find(")", pos) end = len(st) if end == -1 else end tmp_st = st[begin:end] def rp(mo): return mo.group(1).replace(mo.group(2), "") tmp_st = re.sub(r"((<>[\s]+and)[\s\w\d=_]+)", rp, tmp_st) tmp_st = re.sub(r"([\s\w\d=_]+(and[\s]+<>))", rp, tmp_st) tmp_st = re.sub(r"((<>[\s]+or)[\s\w\d=_]+)", rp, tmp_st) tmp_st = re.sub(r"([\s\w\d=_]+(or[\s]+<>))", rp, tmp_st) tmp_st = tmp_st.replace("<>", "") tmp_st = re.sub(r"[\s]{2,}", " ", tmp_st) return st[: old_begin + 1] + tmp_st + st[end:] if request.method == "POST": id = request.POST.get("id", "") query = request.POST.get("query", "mist") from htags.models import Tag # try: tag = Tag.objects.get(id=id) parent_list = [] child_list = [] tmp_list = [o.id for o in Tag.complex_filter(query.replace("<>", "id=" + str(id)), Picture)] tmp_list.sort() rm = [i.id for i in Tag.complex_filter(clean(query), Picture)] rm.sort() for parent in tag.get_all_parents(): tmpquery = query.replace("<>", "id=" + str(parent.id)) tmp = [i.id for i in Tag.complex_filter(tmpquery, Picture)] tmp.sort() tmpquery = query.replace("<>", "") if tmp == tmp_list or tmp == rm: continue pic_count = len(tmp) if pic_count > 0: parent_list.append((parent, pic_count)) for child in tag.get_all_children(): tmpquery = query.replace("<>", "id=" + str(child.id)) tmp = [i.id for i in Tag.complex_filter(tmpquery, Picture)] tmp.sort() if tmp == tmp_list or tmp == rm: continue pic_count = len(tmp) if pic_count > 0: child_list.append((child, pic_count)) return render_to_response( "wilson/replace_snippet.html", { "tag": tag, "query": query, "parents": parent_list, "children": child_list, "current_count": len(tmp_list), "del": {"count": len(rm), "query": clean(query)}, }, context_instance=RequestContext(request), ) # except TypeError as t: # raise t # except Exception as e: # info = str(e) if settings.DEBUG else '' # return HttpResponse('<p>interner Fehler</p><p>'+info+'</p>') return HttpResponseNotAllowed(["POST"])