1. pombredanne
  2. DJFacet

Source

DJFacet / demoproject / apps / djfacet / templatetags / djf_tags.py

	#	2010-11-17:	  NOT USED ANYMORE !!!!!!!	  
	# 
	# to delete in the future.....
	
	# OR USED??? 2012-01-10
	 


from django import template
from djfacet.fb_utils.utils import djfacetlog, split_list_into_two

register = template.Library()






@register.filter
def extract_father(fvalue):
	""" splits a list into two parts"""
	if fvalue.father:
		father = fvalue.facet.get_facetvalue_from_id(fvalue.father)
		return "%s" % father.displayname
	else:
		return ""


@register.filter
def split_list(somelist):
	""" splits a list into two parts"""
	return split_list_into_two(somelist)



@register.filter
def fvlist_formatter2(facetvalues, case_sensitive=False):
	""" 
	2012-05-15
	
	Takes a list of facetvalues and returns a nested list usable in singlefacet.html. 
		{{ with somelist|fvlist_formatter1 as xx }}, or {{ with somelist|fvlist_formatter1:"True" as xx }}

	The nested list is organized by initials and it also splits the facetvalues list into two groups, as required by the template. Eg.

	First we construct a dict
		{ "A" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)], "B" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)], etc.. }
	We transform it into an ordered tuple containing nested lists:
		[ ("A" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)]), ("B" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)]), etc.. }
		
	This is a better version than the one below, as it maintains ordering.. 
	(it may take longer though!!! TODO:check)
	"""
	mydict = {}
	# creates the dict
	if case_sensitive:
		for val in facetvalues:
			if val.displayname:
				if val.displayname[0] not in mydict.keys():
					mydict[val.displayname[0]] = [val]
				else:
					mydict[val.displayname[0]] += [val]
	else:
		for val in facetvalues:
			# print "v=%s, father=%s" % (val.name, str(val.father))
			# print "subs=%s" % (str(val.subspreview))
			if val.displayname:
				if val.displayname[0].upper() not in mydict.keys():
					mydict[val.displayname[0].upper()]= [val]
				else:
					mydict[val.displayname[0].upper()] += [val]
	# now split the lists
	for el in mydict.keys():
		mydict[el] = split_list_into_two(mydict[el])
	# sort
	keys = mydict.keys()
	keys.sort()
	return [(key, mydict[key]) for key in keys]
	# return mydict
	
	


# 2012-05-16: not used anymore cause it messes up the ordering

@register.filter
def fvlist_formatter1(facetvalues, case_sensitive=False):
	""" Takes a list of facetvalues and returns a dict usable in singlefacet.html. 
		{{ with somelist|fvlist_formatter1 as xx }}, or {{ with somelist|fvlist_formatter1:"True" as xx }}
	
	The dict is organized by initials and it also splits the facetvalues list into two groups, as required by the template. Eg.
	
		{ "A" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)], "B" : [(fv1, fv2, fv3 etc), (fv4, fv5, fv6 etc)], etc.. }
	"""
	mydict = {}
	# creates the dict
	if case_sensitive:
		for val in facetvalues:
			if val.displayname:
				if val.displayname[0] not in mydict.keys():
					mydict[val.displayname[0]] = [val]
				else:
					mydict[val.displayname[0]] += [val]
	else:
		for val in facetvalues:
			if val.displayname:
				if val.displayname[0].upper() not in mydict.keys():
					mydict[val.displayname[0].upper()]= [val]
				else:
					mydict[val.displayname[0].upper()] += [val]
	# now split the lists
	for el in mydict.keys():
		mydict[el] = split_list_into_two(mydict[el])
	# mydict.sort(key=lambda x)
	return mydict



# 2012-05-10: UNUSED ?
# ===>	if we keep this approach, we might just remove the little formatting code in ..._inner.html 
@register.inclusion_tag('djfacet/snippet_mpttfacet_inner.html')
def render_tree(facetvalues, facet, facetgroup):
	
	if False:  # testing 
		stuff = []
		for x in facetvalues:
			stuff.append(facet.recursive_tree_forfacetvalue(x))

	if True:
		stringa = ""
		#  the dict cointains the tree structure with IDs only
		djfacetlog("\n STARTING LOADING THE TREE............... \n")
		mydict = facet.recursive_tree_forfacetvalue_list(facetvalues)
		djfacetlog("\n DONE LOADING THE TREE \n")
		# stringa ideally should be built inside the template, using a recursive model.. TODO
		djfacetlog("\n STARTING VISUALIZING THE TREE............... \n")
		stringa = tree_visualizer(0, mydict, "", facet, facetgroup, facetvalues)
		djfacetlog("\n DONE VISUALIZING THE TREE \n")			
	return { 
			'stringa' : stringa,
			'facet_id' : facet.id, 
			
			# 'stuff' : stuff,		
			# 'facet' : facet,			
			# 'facetvalues': facetvalues, 
			# 'facetgroup' : facetgroup
			}



# once we obtained the tree_dict with the IDs, here we check again that only the values with updated howmany are passed
# OPTIMIZE? ? ? change the method in facet.py ? 

def tree_visualizer(root, tree_dict, res, facet, facetgroup, facetvalues):
	for x in tree_dict[root]:
		facetvalue = [v for v in facetvalues if v.id == x]
		if facetvalue:
			res += """<li style="font-size:12px;"><a id="%s_%d" class="%s" >%s (%d)</a>""" % (facetgroup.uniquename, 
				facetvalue[0].id, facet.name, facetvalue[0].displayname,  facetvalue[0].howmany)
		else:
			facetvalue = facet.get_facetvalue_from_id(x)
			res += """<li>%s""" % facetvalue.displayname  #<span style="color: #2C528B;">%s</span>
		if tree_dict.get(x):
			# djfacetlog("noise")
			res += "<ul>" + tree_visualizer(x, tree_dict, '', facet, facetgroup, facetvalues) + "</ul>"
		res += "</li>"
	return res