# Path: muparse/models.py
# class SavedSearch(models.Model):
#     description = models.CharField(max_length=255)
#     display_type = models.CharField(max_length=64)
#     graphs = models.ManyToManyField(NodeGraphs, blank=True, null=True)
#     user = models.ForeignKey(User)
#     default = models.BooleanField(default=False)
# 
#     def get_absolute_url(self):
#         return reverse('load_search', kwargs={'search_id': self.id})
# 
#     def get_delete_url(self):
#         return reverse('delete_search', kwargs={'search_id': self.id})
# 
#     def get_default_url(self):
#         return reverse('default_search', kwargs={'search_id': self.id})
# 
#     def __unicode__(self):
#         return self.description
# 
#     class Meta:
#         verbose_name_plural = "Saved Searches"
# 
# class NodeGraphs(models.Model):
#     node = models.ForeignKey(Node)
#     graph = models.ForeignKey(Graph)
#     baseurl = models.CharField(max_length=512)
#     pageurl = models.CharField(max_length=512)
#     updated = models.DateTimeField(auto_now=True)
# 
#     def img_url(self):
#         return '%s-day.png' % self.baseurl
# 
#     def __unicode__(self):
#         return u'%s:%s:%s' % (self.node, self.graph, self.baseurl)
# 
#     class Meta:
#         verbose_name_plural = "Node Graphs"
# 
# def common_start(sa, sb):
#     '''
#     returns the longest common substring from the beginning of sa and sb
#     '''
#     def _iter():
#         for a, b in zip(sa, sb):
#             if a == b:
#                 yield a
#             else:
#                 return
# 
#     return ''.join(_iter())


import bz2
import json
from django import template
from muparse.models import SavedSearch, NodeGraphs, common_start
from django.core.cache import cache
        parsed_group = []
        graphs = NodeGraphs.objects.all().prefetch_related(
            'node',
            'graph',
            'node__group',
            'graph__category'
        ).order_by('node__group', 'node', 'graph__category__name')
        nodes = request.user.get_profile().nodes.all()
        graphs = graphs.filter(node__in=nodes)
        len_graphs = len(graphs)
        for index, graph in enumerate(graphs):
            if graph.node not in parsed_node:
                parsed_graph_category = []
                if parsed_node:
                    ndict['children'].append(gcdict)

                    nlist.append(ndict)
                ndict = {}
                ndict['title'] = graph.node.name
                ndict['children'] = []
                parsed_node.append(graph.node)
            if graph.node.group not in parsed_group:
                if parsed_group:
                    nlist.sort(key=lambda item: item['title'], reverse=False)
                    grdict['children'] = nlist
                    grlist.append(grdict)
                    nlist = []
                grdict = {}
                grdict['title'] = graph.node.group.name
                grdict['children'] = []

# Path: muparse/models.py
# class SavedSearch(models.Model):
#     description = models.CharField(max_length=255)
#     display_type = models.CharField(max_length=64)
#     graphs = models.ManyToManyField(NodeGraphs, blank=True, null=True)
#     user = models.ForeignKey(User)
#     default = models.BooleanField(default=False)
# 
#     def get_absolute_url(self):
#         return reverse('load_search', kwargs={'search_id': self.id})
# 
#     def get_delete_url(self):
#         return reverse('delete_search', kwargs={'search_id': self.id})
# 
#     def get_default_url(self):
#         return reverse('default_search', kwargs={'search_id': self.id})
# 
#     def __unicode__(self):
#         return self.description
# 
#     class Meta:
#         verbose_name_plural = "Saved Searches"
# 
# class NodeGraphs(models.Model):
#     node = models.ForeignKey(Node)
#     graph = models.ForeignKey(Graph)
#     baseurl = models.CharField(max_length=512)
#     pageurl = models.CharField(max_length=512)
#     updated = models.DateTimeField(auto_now=True)
# 
#     def img_url(self):
#         return '%s-day.png' % self.baseurl
# 
#     def __unicode__(self):
#         return u'%s:%s:%s' % (self.node, self.graph, self.baseurl)
# 
#     class Meta:
#         verbose_name_plural = "Node Graphs"
# 
# def common_start(sa, sb):
#     '''
#     returns the longest common substring from the beginning of sa and sb
#     '''
#     def _iter():
#         for a, b in zip(sa, sb):
#             if a == b:
#                 yield a
#             else:
#                 return
# 
#     return ''.join(_iter())


# Path: muparse/templatetags/mupy_tags.py
import bz2
import json
from django import template
from muparse.models import SavedSearch, NodeGraphs, common_start
from django.core.cache import cache

        parsed_group = []
        graphs = NodeGraphs.objects.all().prefetch_related(
            'node',
            'graph',
            'node__group',
            'graph__category'
        ).order_by('node__group', 'node', 'graph__category__name')
        nodes = request.user.get_profile().nodes.all()
        graphs = graphs.filter(node__in=nodes)
        len_graphs = len(graphs)
        for index, graph in enumerate(graphs):
            if graph.node not in parsed_node:
                parsed_graph_category = []
                if parsed_node:
                    ndict['children'].append(gcdict)

                    nlist.append(ndict)
                ndict = {}
                ndict['title'] = graph.node.name
                ndict['children'] = []
                parsed_node.append(graph.node)
            if graph.node.group not in parsed_group:
                if parsed_group:
                    nlist.sort(key=lambda item: item['title'], reverse=False)
                    grdict['children'] = nlist
                    grlist.append(grdict)
                    nlist = []
                grdict = {}
                grdict['title'] = graph.node.group.name
                grdict['children'] = []

                grdict['baseurl'] = common_start(graph.baseurl, graph.node.url)