# Path: query/models.py
# class Query(models.Model):
#     """Model to store a User's queries.
#     """
#     title = models.CharField(max_length=100)
#     comment = models.TextField(blank=True)
#     query = models.TextField()
#     nr_results = models.PositiveIntegerField(null=True)
# 
#     exclude_article_types = models.ManyToManyField(ArticleType, blank=True)
#     exclude_distributions = models.ManyToManyField(Distribution, blank=True)
#     selected_pillars = models.ManyToManyField(Pillar, blank=True)
#     user = models.ForeignKey(User)
# 
#     date_created = models.DateTimeField(auto_now_add=True)
#     date_modified = models.DateTimeField(auto_now=True, null=True)
# 
#     class Meta:
#         """Make sure that Query titles are unique for a User"""
#         unique_together = ('user', 'title')
#         # Custom permissions
#         permissions = (
#             (
#                 'download_documents',
#                 'Can download results of a query',
#             ),
#             (
#                 'download_many_documents',
#                 'Can download larger numbers of results',
#             ),
#         )
#         verbose_name_plural = 'Queries'
# 
#     def get_query_dict(self):
#         """Returns a JSON serializable representation of the query object, that
#         contains all relevant data and metadata.
#         """
#         periods = Period.objects.filter(query=self)
#         selected_dateranges = [{'lower': p.date_lower.isoformat(), 'upper': p.date_upper.isoformat()} for p in periods]
#         excl_art_types = [a.id for a in self.exclude_article_types.all()]
#         excl_distr = [d.id for d in self.exclude_distributions.all()]
#         selected_pillars = [p.id for p in self.selected_pillars.all()]
#         selected_pillar_names = [p.name for p in self.selected_pillars.all()]
# 
#         return {
#             'pk': self.pk,
#             'title': self.title,
#             'query': self.query,
#             'comment': self.comment,
#             'nr_results': self.nr_results,
#             'date_created': self.date_created.isoformat(),
#             'dates': selected_dateranges,
#             'exclude_article_types': excl_art_types,
#             'exclude_distributions': excl_distr,
#             'selected_pillars': selected_pillars,
#             'selected_pillar_names': selected_pillar_names
#         }
# 
#     def __unicode__(self):
#         return self.query
# 
# Path: services/es.py
# def get_document_ids(idx, typ, query, date_ranges, resolution,
#                      exclude_distributions=[], exclude_article_types=[], selected_pillars=[]):
#     """Return a list of document ids and dates for a query
#     """
#     result = defaultdict(list)
# 
#     q = create_query(query, date_ranges, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     date_field = 'paper_dc_date'
#     fields = [date_field]
#     get_more_docs = True
#     start = 0
#     num = 2500
# 
#     while get_more_docs:
#         results = _es().search(index=idx, doc_type=typ, body=q, fields=fields,
#                                from_=start, size=num)
#         for r in results['hits']['hits']:
#             ymd = r['fields'][date_field][0].split('-')
#             y = int(ymd[0])
#             m = int(ymd[1]) if resolution != 'year' else 1
#             d = int(ymd[2]) if resolution == 'day' else 1
#             result[datetime(y, m, d)].append(r['_id'])
# 
#         start += num
# 
#         if len(results['hits']['hits']) < num:
#             get_more_docs = False
# 
#     return result
# 
# def count_search_results(idx, typ, query, date_range, exclude_distributions,
#                          exclude_article_types, selected_pillars):
#     """Count the number of results for a query
#     """
#     q = create_query(query, date_range, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     return _es().count(index=idx, doc_type=typ, body=q)


from sys import stderr
from django.conf import settings
from django.db import DatabaseError
from .models import Query
from services.es import get_document_ids, count_search_results
from texcavator.utils import json_response_message
"""Utility functions for saved queries."""





def get_query_object(query_id):
    """
    Returns the query object stored for a query id and an appropriate error
    message if the query cannot be retrieved.
    """
    query = None
    response = None

    try:
        query = Query.objects.get(pk=query_id)

# Path: query/models.py
# class Query(models.Model):
#     """Model to store a User's queries.
#     """
#     title = models.CharField(max_length=100)
#     comment = models.TextField(blank=True)
#     query = models.TextField()
#     nr_results = models.PositiveIntegerField(null=True)
# 
#     exclude_article_types = models.ManyToManyField(ArticleType, blank=True)
#     exclude_distributions = models.ManyToManyField(Distribution, blank=True)
#     selected_pillars = models.ManyToManyField(Pillar, blank=True)
#     user = models.ForeignKey(User)
# 
#     date_created = models.DateTimeField(auto_now_add=True)
#     date_modified = models.DateTimeField(auto_now=True, null=True)
# 
#     class Meta:
#         """Make sure that Query titles are unique for a User"""
#         unique_together = ('user', 'title')
#         # Custom permissions
#         permissions = (
#             (
#                 'download_documents',
#                 'Can download results of a query',
#             ),
#             (
#                 'download_many_documents',
#                 'Can download larger numbers of results',
#             ),
#         )
#         verbose_name_plural = 'Queries'
# 
#     def get_query_dict(self):
#         """Returns a JSON serializable representation of the query object, that
#         contains all relevant data and metadata.
#         """
#         periods = Period.objects.filter(query=self)
#         selected_dateranges = [{'lower': p.date_lower.isoformat(), 'upper': p.date_upper.isoformat()} for p in periods]
#         excl_art_types = [a.id for a in self.exclude_article_types.all()]
#         excl_distr = [d.id for d in self.exclude_distributions.all()]
#         selected_pillars = [p.id for p in self.selected_pillars.all()]
#         selected_pillar_names = [p.name for p in self.selected_pillars.all()]
# 
#         return {
#             'pk': self.pk,
#             'title': self.title,
#             'query': self.query,
#             'comment': self.comment,
#             'nr_results': self.nr_results,
#             'date_created': self.date_created.isoformat(),
#             'dates': selected_dateranges,
#             'exclude_article_types': excl_art_types,
#             'exclude_distributions': excl_distr,
#             'selected_pillars': selected_pillars,
#             'selected_pillar_names': selected_pillar_names
#         }
# 
#     def __unicode__(self):
#         return self.query
# 
# Path: services/es.py
# def get_document_ids(idx, typ, query, date_ranges, resolution,
#                      exclude_distributions=[], exclude_article_types=[], selected_pillars=[]):
#     """Return a list of document ids and dates for a query
#     """
#     result = defaultdict(list)
# 
#     q = create_query(query, date_ranges, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     date_field = 'paper_dc_date'
#     fields = [date_field]
#     get_more_docs = True
#     start = 0
#     num = 2500
# 
#     while get_more_docs:
#         results = _es().search(index=idx, doc_type=typ, body=q, fields=fields,
#                                from_=start, size=num)
#         for r in results['hits']['hits']:
#             ymd = r['fields'][date_field][0].split('-')
#             y = int(ymd[0])
#             m = int(ymd[1]) if resolution != 'year' else 1
#             d = int(ymd[2]) if resolution == 'day' else 1
#             result[datetime(y, m, d)].append(r['_id'])
# 
#         start += num
# 
#         if len(results['hits']['hits']) < num:
#             get_more_docs = False
# 
#     return result
# 
# def count_search_results(idx, typ, query, date_range, exclude_distributions,
#                          exclude_article_types, selected_pillars):
#     """Count the number of results for a query
#     """
#     q = create_query(query, date_range, exclude_distributions,
#                      exclude_article_types, selected_pillars)
# 
#     return _es().count(index=idx, doc_type=typ, body=q)


# Path: query/utils.py
from sys import stderr
from django.conf import settings
from django.db import DatabaseError
from .models import Query
from services.es import get_document_ids, count_search_results
from texcavator.utils import json_response_message

"""Utility functions for saved queries."""





def get_query_object(query_id):
    """
    Returns the query object stored for a query id and an appropriate error
    message if the query cannot be retrieved.
    """
    query = None
    response = None

    try:
        query = Query.objects.get(pk=query_id)

    except Query.DoesNotExist: