Skip to content

Commit

Permalink
Advanced filtering for RSSFeed functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
DEKHTIARJonathan committed Aug 20, 2017
1 parent 0787c4c commit eb00065
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions feedcrunch/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""Admin module for Django."""
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

import re, uuid, datetime

Expand Down Expand Up @@ -82,16 +83,57 @@ def _get_tags_count(self, obj):
admin.site.register(Post, PostAdmin)

# ==================== RSS Feed ============================

class RSSFeedListFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('# of bad attempts')

# Parameter for the filter that will be used in the URL query.
parameter_name = '# of bad attempts'

def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
return (
('Bad', _('With bad attempts')),
('Correct', _('Without bad attempts')),
)

def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
# Compare the requested value (either '80s' or '90s')
# to decide how to filter the queryset.
if self.value() == 'Bad':
return queryset.filter(bad_attempts__gte=1).order_by('-bad_attempts')
if self.value() == 'Correct':
return queryset.filter(bad_attempts=0)

class RSSFeedAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'get_domain', 'link', '_get_articles_count')
ordering = ('-id',)
list_display = ('id', 'title', 'get_domain', 'link', '_get_articles_count', 'active', 'bad_attempts')
#ordering = ('-id',)

def _get_articles_count(self, obj):
return obj.count_articles()

def _is_articles_with_bad_attempts(self, obj):
return obj.bad_attempts >= 0

_get_articles_count.short_description="Articles Count"

search_fields = ('title', 'get_domain')

list_filter = ('active', RSSFeedListFilter)

admin.site.register(RSSFeed, RSSFeedAdmin)

# ==================== RSSArticles =========================
Expand Down

0 comments on commit eb00065

Please sign in to comment.