fusionbox/django-fusionbox — Issue #17: what to do with generic views?
Author: chroto
Created: 2012-01-20 21:28:04+00:00
State: open

I'm starting to acquire some generic, reusable views. I want to be able to share and reuse, but not really sure where they belong ( if they belong ) in django-fusionbox.

For example, I have this list view that takes publishable objects and returns a sorted, paginated list, filtered by category. In my project I have this used by 2 different apps and I currently am importing it from the __init__.py for the "root app" i.e. `from zerosun import ZerosunPaginateListView`

Does something like this have a place in django_fusionbox? maybe `from fusionbox.generic_views import SortedListView` ?

~~~

### View Helpers
class ZerosunPaginateListView(ListView):
paginate_by = 9
sorts = {
"date": lambda x: x.order_by("-publish_at"),
"alpha" : lambda x: x.order_by("title"),
"popular": lambda x: x.annotate(
comment_count=models.Count("comments"))\
.order_by("-comment_count")
}

def get_context_data(self, **kwargs):
context = super(ZerosunPaginateListView, self)\
.get_context_data(**kwargs)
context["categories"] = Category.objects.all()
context["category"] = self.request.GET.get("category", "all")
return context

def get_queryset(self):
paginated_items = self.model.published.all()
category_slug = self.request.GET.get("category", "all")
sorting_type = self.request.GET.get("sort", self.sorts.keys()[0])
if category_slug != "all":
paginated_items = paginated_items.filter(
category__slug=category_slug)
return self.sorts[sorting_type](paginated_items)

~~~