# 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"
# 
# Path: muparse/forms.py
# class SavedSearchForm(ModelForm):
#     class Meta:
#         model = SavedSearch
# 
# Path: accounts/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     nodes = models.ManyToManyField(Node, blank=True, null=True)
# 
#     def get_nodes(self):
#         ret = ''
#         ngs = self.nodes.all()
#         for ng in ngs:
#             ret = "%s, %s" % (ng, ret)
#         if ret:
#             nodes = ret.rstrip(', ')
#             return nodes
#         else:
#             return None
# 
#     def save(self, *args, **kwargs):
#         cache.delete('user_%s_tree' % (self.user.pk))
#         cache.delete('user_%s_tree_cat' % (self.user.pk))
#         super(UserProfile, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return "%s:%s" % (self.user.username, self.get_nodes())
# 
# Path: muparse/models.py
# 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"


import json
from django.shortcuts import render
from django.http import HttpResponse
from muparse.models import SavedSearch
from muparse.forms import SavedSearchForm
from accounts.models import UserProfile
from django.views.decorators.cache import never_cache
from django.contrib.auth.decorators import login_required
from django.http import Http404
from muparse.models import NodeGraphs



@login_required
def home(request):
    saved_search = SavedSearch.objects.filter(user=request.user)
    if saved_search:
        default = saved_search.filter(default=True)
        if default:
            saved_search = default
        graphs = saved_search[0].graphs.all()
    else:
        nodes = request.user.get_profile().nodes.all()
        graphs = NodeGraphs.objects.filter(node__in=nodes)[:50]
        default = False
    return render(request, 'main.html', {'graphs': graphs, 'default': default})


@login_required
def get_menu(request):
    return render(request, 'partial/graphs_menu.html')


@login_required
@never_cache
def save_search(request):
    request_data = request.POST.copy()
    graph_pks = request_data.get('graphs').split(',')
    is_edit = request_data.get('is_edit')
    request_data.setlist('graphs', graph_pks)

# 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"
# 
# Path: muparse/forms.py
# class SavedSearchForm(ModelForm):
#     class Meta:
#         model = SavedSearch
# 
# Path: accounts/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     nodes = models.ManyToManyField(Node, blank=True, null=True)
# 
#     def get_nodes(self):
#         ret = ''
#         ngs = self.nodes.all()
#         for ng in ngs:
#             ret = "%s, %s" % (ng, ret)
#         if ret:
#             nodes = ret.rstrip(', ')
#             return nodes
#         else:
#             return None
# 
#     def save(self, *args, **kwargs):
#         cache.delete('user_%s_tree' % (self.user.pk))
#         cache.delete('user_%s_tree_cat' % (self.user.pk))
#         super(UserProfile, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return "%s:%s" % (self.user.username, self.get_nodes())
# 
# Path: muparse/models.py
# 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"


# Path: muparse/views.py
import json
from django.shortcuts import render
from django.http import HttpResponse
from muparse.models import SavedSearch
from muparse.forms import SavedSearchForm
from accounts.models import UserProfile
from django.views.decorators.cache import never_cache
from django.contrib.auth.decorators import login_required
from django.http import Http404
from muparse.models import NodeGraphs




@login_required
def home(request):
    saved_search = SavedSearch.objects.filter(user=request.user)
    if saved_search:
        default = saved_search.filter(default=True)
        if default:
            saved_search = default
        graphs = saved_search[0].graphs.all()
    else:
        nodes = request.user.get_profile().nodes.all()
        graphs = NodeGraphs.objects.filter(node__in=nodes)[:50]
        default = False
    return render(request, 'main.html', {'graphs': graphs, 'default': default})


@login_required
def get_menu(request):
    return render(request, 'partial/graphs_menu.html')


@login_required
@never_cache
def save_search(request):
    request_data = request.POST.copy()
    graph_pks = request_data.get('graphs').split(',')
    is_edit = request_data.get('is_edit')
    request_data.setlist('graphs', graph_pks)

    form = SavedSearchForm(request_data)