# 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
# 
# class DayStatistic(models.Model):
#     """DayStatistic is used to generate timeline graphs. Data for the
#     DayStatistic table is gathered with the 'gatherstatistics' management
#     command.
#     """
#     date = models.DateField()
#     distribution = models.ForeignKey(Distribution, blank=True, null=True)
#     article_type = models.ForeignKey(ArticleType, blank=True, null=True)
#     count = models.IntegerField()
#     checked = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         unique_together = (('date', 'distribution', 'article_type'),)
# 
#     def __unicode__(self):
#         return '{}: {}'.format(str(self.date), self.count)
# 
# class StopWord(models.Model):
#     """Model to store stopwords.
# 
#     Stopwords can be:
# 
#     - specific for a Query (applicable to a single User's Query, query/user != None)
#     - specific for a User (applicable for all of a User's Queries, query == None, user != None)
#     - non-specific/default (applicable to all Queries for all Users, user/query == None)
#     """
#     user = models.ForeignKey(User, null=True, blank=True)
#     query = models.ForeignKey(Query, null=True, blank=True)
#     word = models.CharField(max_length=100)
# 
#     def __unicode__(self):
#         return self.word
# 
#     def get_stopword_dict(self):
#         if self.user:
#             user_name = self.user.username
#         else:
#             user_name = ''
# 
#         if self.query:
#             query = self.query.title
#         else:
#             query = ''
# 
#         return {
#             'id': self.id,
#             'user': user_name,
#             'query': query,
#             'word': self.word
#         }
# 
# class Pillar(models.Model):
#     """Model that allows to store a categorization of Newspapers along Pillars."""
#     name = models.CharField(max_length=200, unique=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Newspaper(models.Model):
#     """Model that stores the available Newspapers"""
#     id = models.CharField(max_length=9, primary_key=True)
#     title = models.CharField(max_length=500)
#     start_date = models.DateField()
#     end_date = models.DateField()
#     editions = models.PositiveIntegerField()
#     pillar = models.ForeignKey(Pillar, null=True)
# 
#     def __unicode__(self):
#         return self.title
# 
# class Period(models.Model):
#     """Model to store the Periods for a Query"""
#     date_lower = models.DateField()
#     date_upper = models.DateField()
#     query = models.ForeignKey(Query)


from django.contrib import admin
from query.models import Query, DayStatistic, StopWord, Pillar, Newspaper, Period
    model = Period
    max_num = 2


@admin.register(DayStatistic)
class DayStatisticAdmin(admin.ModelAdmin):
    list_display = ('date', 'distribution', 'article_type', 'count', 'checked', )
    ordering = ['date', 'distribution', 'article_type']


@admin.register(Query)
class QueryAdmin(admin.ModelAdmin):
    inlines = [PeriodInline]
    list_display = ('title', 'query', 'user', 'date_modified')
    list_filter = ('user', )
    ordering = ['user', '-date_modified']


@admin.register(StopWord)
class StopWordAdmin(admin.ModelAdmin):
    list_display = ('word', 'user', 'query', )
    search_fields = ['word']
    list_filter = ('user', )
    ordering = ['user', 'query', 'word']


@admin.register(Newspaper)
class NewspaperAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'start_date', 'end_date', 'editions', 'pillar', )
    search_fields = ['title']

# 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
# 
# class DayStatistic(models.Model):
#     """DayStatistic is used to generate timeline graphs. Data for the
#     DayStatistic table is gathered with the 'gatherstatistics' management
#     command.
#     """
#     date = models.DateField()
#     distribution = models.ForeignKey(Distribution, blank=True, null=True)
#     article_type = models.ForeignKey(ArticleType, blank=True, null=True)
#     count = models.IntegerField()
#     checked = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         unique_together = (('date', 'distribution', 'article_type'),)
# 
#     def __unicode__(self):
#         return '{}: {}'.format(str(self.date), self.count)
# 
# class StopWord(models.Model):
#     """Model to store stopwords.
# 
#     Stopwords can be:
# 
#     - specific for a Query (applicable to a single User's Query, query/user != None)
#     - specific for a User (applicable for all of a User's Queries, query == None, user != None)
#     - non-specific/default (applicable to all Queries for all Users, user/query == None)
#     """
#     user = models.ForeignKey(User, null=True, blank=True)
#     query = models.ForeignKey(Query, null=True, blank=True)
#     word = models.CharField(max_length=100)
# 
#     def __unicode__(self):
#         return self.word
# 
#     def get_stopword_dict(self):
#         if self.user:
#             user_name = self.user.username
#         else:
#             user_name = ''
# 
#         if self.query:
#             query = self.query.title
#         else:
#             query = ''
# 
#         return {
#             'id': self.id,
#             'user': user_name,
#             'query': query,
#             'word': self.word
#         }
# 
# class Pillar(models.Model):
#     """Model that allows to store a categorization of Newspapers along Pillars."""
#     name = models.CharField(max_length=200, unique=True)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Newspaper(models.Model):
#     """Model that stores the available Newspapers"""
#     id = models.CharField(max_length=9, primary_key=True)
#     title = models.CharField(max_length=500)
#     start_date = models.DateField()
#     end_date = models.DateField()
#     editions = models.PositiveIntegerField()
#     pillar = models.ForeignKey(Pillar, null=True)
# 
#     def __unicode__(self):
#         return self.title
# 
# class Period(models.Model):
#     """Model to store the Periods for a Query"""
#     date_lower = models.DateField()
#     date_upper = models.DateField()
#     query = models.ForeignKey(Query)


# Path: query/admin.py
from django.contrib import admin
from query.models import Query, DayStatistic, StopWord, Pillar, Newspaper, Period

    model = Period
    max_num = 2


@admin.register(DayStatistic)
class DayStatisticAdmin(admin.ModelAdmin):
    list_display = ('date', 'distribution', 'article_type', 'count', 'checked', )
    ordering = ['date', 'distribution', 'article_type']


@admin.register(Query)
class QueryAdmin(admin.ModelAdmin):
    inlines = [PeriodInline]
    list_display = ('title', 'query', 'user', 'date_modified')
    list_filter = ('user', )
    ordering = ['user', '-date_modified']


@admin.register(StopWord)
class StopWordAdmin(admin.ModelAdmin):
    list_display = ('word', 'user', 'query', )
    search_fields = ['word']
    list_filter = ('user', )
    ordering = ['user', 'query', 'word']


@admin.register(Newspaper)
class NewspaperAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'start_date', 'end_date', 'editions', 'pillar', )
    search_fields = ['title']

    list_filter = ('pillar', )