def graphData_similarKonceptsVisJs(a_koncept, user=None, onlypublic=True): """ creates the json structures needed by the visjs library graph: network of koncept > similar koncepts > related sources """ KONCEPT_LABEL = which_labels(None)['KONCEPT_LABEL'] DOCUMENT_LABEL = which_labels(None)['DOCUMENT_LABEL'] def addBRspace(stringa): """ util to add linebreaks """ SIZE = 3 ss = stringa.split() return '\n'.join(' '.join(ss[pos:pos+SIZE]) for pos in xrange(0, len(ss), SIZE)) if True: # only koncepts similar nodes = [] edges = [] rootid = "k_"+ str(a_koncept.id) INDEX_KONS = [rootid] #hack to keep track of what's in there already - the js libs fails otherwise INDEX_DOCS = [] top = {'id' : rootid, 'label' : a_koncept.name, 'group' : 'group_root'} nodes += [top] for x in a_koncept.getSimilarKoncepts(n=6, user=user, onlypublic=onlypublic): x_id = "k_"+ str(x.id) if x_id not in INDEX_KONS: INDEX_KONS += [x_id] nodes += [{'id' : x_id, 'label' : addBRspace(x.name), 'group' : 'group_koncept'}] edges += [{'from' : rootid, 'to' : x_id, 'label' : "", 'color' : '#848484'}] for s in x.getSources(): sid = "s_"+ str(s.id) if sid not in INDEX_DOCS: s_label = addBRspace(s.title) + "\n" + s.get_url_domain().strip("http://") nodes += [{'id' : sid, 'label' : s_label, 'group' : 'group_source'}] INDEX_DOCS += [sid] edges += [{'from' : x_id, 'to' : sid, 'label' : "%s" % DOCUMENT_LABEL.lower(), 'color' : '#2B7CE9'}] if False: for x1 in x.getSimilarKoncepts(n=6, user=user, onlypublic=onlypublic): x1_id = "k_"+ str(x1.id) if x1_id not in INDEX_KONS: INDEX_KONS += [x1_id] nodes += [{'id' : x1_id, 'label' : addBRspace(x1.name), 'group' : 'group_koncept'}] edges += [{'from' : x_id, 'to' : x1_id, 'label' : "similar to" }] return [simplejson.dumps(nodes), simplejson.dumps(edges)]
def graphData_relatedSources(a_koncept, user=None, onlypublic=True): """ creates the json structures needed by the visjs library graph: network of koncept > sources > others koncepts """ KONCEPT_LABEL = which_labels(None)['KONCEPT_LABEL'] DOCUMENT_LABEL = which_labels(None)['DOCUMENT_LABEL'] def addBRspace(stringa): """ util to add linebreaks """ SIZE = 3 ss = stringa.split() return '\n'.join(' '.join(ss[pos:pos+SIZE]) for pos in xrange(0, len(ss), SIZE)) # koncept sources + their related koncepts nodes1 = [] edges1 = [] rootid = "k_"+ str(a_koncept.id) INDEX1 = [rootid] #hack to keep track of what's in there already - the js libs fails otherwise top = {'id' : rootid, 'label' : addBRspace(a_koncept.name), 'group' : 'group_root'} nodes1 += [top] for s in a_koncept.getSources(): sid = "s_"+ str(s.id) s_label = addBRspace(s.title) + "\n" + s.get_url_domain().strip("http://") nodes1 += [{'id' : sid, 'label' : s_label, 'group' : 'group_source'}] edges1 += [{'from' : rootid, 'to' : sid, 'label' : "%s" % DOCUMENT_LABEL.lower()}] for k in s.get_koncepts(): kid = "k_"+ str(k.id) if kid != rootid: if kid not in INDEX1: INDEX1 += [kid] nodes1 += [{'id' : kid, 'label' : addBRspace(k.name), 'group' : 'group_koncept'}] edges1 += [{'from' : sid, 'to' : kid, 'label' : "%s" % KONCEPT_LABEL.lower()}] return [simplejson.dumps(nodes1), simplejson.dumps(edges1)]
def edit_quote_subjects(request): """ Ajax action for modifying the title of a quote """ user = request.user ## in the future we'll have to handle username too quote_id = request.GET.get('quote_id', None) newsubjects = request.GET.get('subjects', "") data = { 'new_subjects' : "" } try: f1 = Fragment.objects.get(pk=int(quote_id), created_by=user) except: return HttpResponseForbidden() newsubjects = newsubjects.strip().split(",") old_subjects = list(f1.subjects.all()) if not newsubjects and not old_subjects: pass else: # clear tags and recreate them f1.subjects.clear() if newsubjects: for el in newsubjects: if el: try: subject = Subject.objects.get(name=el.strip().lower(), created_by=user) except: subject = Subject(name=el.strip().lower(), created_by=user) subject.save() f1.subjects.add(subject) # return_str = render_block_to_string('bstrap3.2.0/components/tags_snippet.html', return_str = render_block_to_string('bstrap3.2.0/components/sidebar_tagsinfo.html', 'snippet_tags_snippet', { 'snippet' : f1 , 'mykoncepts' : True, 'SUBJECT_LABEL' : which_labels(request)['SUBJECT_LABEL'] }) data = { 'new_subjects' : return_str } Subject.cleanUpUserSubjects(user, old_subjects) json = simplejson.dumps(data) return HttpResponse(json, mimetype='application/json')
# ) # # Tip: # [^/]+ ==> match anything except / # .* ==> matches everything.. from django.conf.urls.defaults import * from django.views.generic import RedirectView from context_processors import which_labels KONCEPT_LABEL = which_labels(None)['KONCEPT_LABEL'].lower() + "s" SUBJECT_LABEL = which_labels(None)['SUBJECT_LABEL'].lower() + "s" DOCUMENT_LABEL = which_labels(None)['DOCUMENT_LABEL'].lower() + "s" SNIPPET_LABEL = which_labels(None)['SNIPPET_LABEL'].lower() + "s" FOLDER_LABEL = which_labels(None)['FOLDER_LABEL'].lower() urlpatterns = patterns('koncepts.views', url(r'^$', 'homepage', name='homepage'), ) # "/test/" pattern