Forked from mateuszpohl/SimpleFilteringListView.py
Created
September 18, 2012 12:07
-
-
Save teserak/3742781 to your computer and use it in GitHub Desktop.
Django generic list view with simple filtering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Incoming</h2> | |
<ul> | |
{% for obj in incoming %} | |
<li>{{ obj.schedule }}</li> | |
{% endfor %} | |
</ul> | |
<h2>Archive</h2> | |
<ul> | |
{% for obj in archive %} | |
<li>{{ obj.schedule }}</li> | |
{% endfor %} | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from datetime import datetime | |
class Task(models.Model): | |
schedule = models.DateTimeField() | |
def is_incoming(self): | |
return self.schedule > datetime.now() | |
def is_archive(self): | |
return self.schedule <= datetime.now() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic import ListView | |
class SimpleFilteringListView(ListView): | |
""" | |
List view with extra filtering and grouping in context data. | |
``filters`` dict is created as follows: {'context_variable_name', 'filtering_object_method_name'} | |
It calls filtering method on each object in object_list and if it returns True - this object is added | |
to proper context variable. | |
""" | |
filters = {} | |
def get_filtered_data(self): | |
filtered_data = {} | |
if self.filters: | |
for obj in self.object_list: | |
for filter_name in self.filters: | |
filter_func = getattr(obj, self.filters[filter_name]) | |
filter_list = filtered_data.get(filter_name, []) | |
if filter_func(): | |
filter_list.append(obj) | |
filtered_data[filter_name] = filter_list | |
return filtered_data | |
def get_context_data(self, **kwargs): | |
context = super(SimpleFilteringListView, self).get_context_data(**kwargs) | |
context.update(self.get_filtered_data()) | |
return context |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from SimpleFilteringListView import * | |
from models import Task | |
class TaskList(SimpleFilteringListView): | |
model = Task | |
filters = {'incoming':'is_incoming', 'archive':'is_archive'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment