# 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
    graph_pks = request_data.get('graphs').split(',')
    is_edit = request_data.get('is_edit')
    request_data.setlist('graphs', graph_pks)
    form = SavedSearchForm(request_data)
    if is_edit == 'edit':
        description = request_data.get('description')
        try:
            existinggraphsearch = SavedSearch.objects.get(description=description)
            form = SavedSearchForm(request_data, instance=existinggraphsearch)
        except SavedSearch.DoesNotExist:
            pass
    if form.is_valid():
        search = form.save(commit=False)
        search.save()
        form.save_m2m()
        response = json.dumps({"result": "Successfully saved %s graphs as %s" % (len(graph_pks), search.description), 'errors': None})
        return HttpResponse(response, mimetype="application/json")
    else:
        response = json.dumps({"result": "Errors: %s" % (form.errors.as_text()), 'errors': True})
        return HttpResponse(response, mimetype="application/json")


@login_required
@never_cache
def load_search(request, search_id=None):
    savedsearches = SavedSearch.objects.get(pk=search_id)
    graphs = []
    if not request.user.is_superuser:
        try:
            nodes = request.user.get_profile().nodes.all()

# 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

    graph_pks = request_data.get('graphs').split(',')
    is_edit = request_data.get('is_edit')
    request_data.setlist('graphs', graph_pks)
    form = SavedSearchForm(request_data)
    if is_edit == 'edit':
        description = request_data.get('description')
        try:
            existinggraphsearch = SavedSearch.objects.get(description=description)
            form = SavedSearchForm(request_data, instance=existinggraphsearch)
        except SavedSearch.DoesNotExist:
            pass
    if form.is_valid():
        search = form.save(commit=False)
        search.save()
        form.save_m2m()
        response = json.dumps({"result": "Successfully saved %s graphs as %s" % (len(graph_pks), search.description), 'errors': None})
        return HttpResponse(response, mimetype="application/json")
    else:
        response = json.dumps({"result": "Errors: %s" % (form.errors.as_text()), 'errors': True})
        return HttpResponse(response, mimetype="application/json")


@login_required
@never_cache
def load_search(request, search_id=None):
    savedsearches = SavedSearch.objects.get(pk=search_id)
    graphs = []
    if not request.user.is_superuser:
        try:
            nodes = request.user.get_profile().nodes.all()

        except UserProfile.DoesNotExist: