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 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"])