# 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
register = template.Library()


@register.inclusion_tag('partial/saved_searches.html', takes_context=True)
def show_saved_searches(context):
    try:
        user = context.get('request').user
    	saved_searches = SavedSearch.objects.filter(user=user)
    except:
        saved_searches =  SavedSearch.objects.none()
	return {'saved': saved_searches, 'default': False}
    default = saved_searches.filter(default=True) or False
    return {'saved': saved_searches, 'default': default}


@register.inclusion_tag('partial/graphs.html', takes_context=True)
def load_graphs(context):
    request = context.get('request')
    glist = cache.get('user_%s_tree' % (request.user.pk))
    if glist:
        grlist = json.loads(bz2.decompress(glist))
    else:
        grlist = []
        nlist = []
        gcdict = {}
        ndict = {}
        nlist = []
        grdict = {}
        parsed_node = []
        parsed_group = []

# 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

register = template.Library()


@register.inclusion_tag('partial/saved_searches.html', takes_context=True)
def show_saved_searches(context):
    try:
        user = context.get('request').user
    	saved_searches = SavedSearch.objects.filter(user=user)
    except:
        saved_searches =  SavedSearch.objects.none()
	return {'saved': saved_searches, 'default': False}
    default = saved_searches.filter(default=True) or False
    return {'saved': saved_searches, 'default': default}


@register.inclusion_tag('partial/graphs.html', takes_context=True)
def load_graphs(context):
    request = context.get('request')
    glist = cache.get('user_%s_tree' % (request.user.pk))
    if glist:
        grlist = json.loads(bz2.decompress(glist))
    else:
        grlist = []
        nlist = []
        gcdict = {}
        ndict = {}
        nlist = []
        grdict = {}
        parsed_node = []
        parsed_group = []

        graphs = NodeGraphs.objects.all().prefetch_related(