from django.db.models import Model
from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404

def model_base(qs, key='qs'):
    def _model_base(request, c):
        if isinstance(qs, Model):
            c[key] = qs.objects.all()
        elif isinstance(qs, QuerySet):
            c[key] = qs
        else:
            raise Exception
        return c
    return _model_base

def filter(fields, qs='qs', query_key='q'):
    def _filter(request, c):
        pass
    return _filter

def get_object(qs='qs', key='id', pk='pk', template_object_name='obj'):
    def _get_object(request, c):
        # qs can be one of the following: Model, query, key for qs in vars
        if isinstance(qs, (Model, QuerySet)):
            d = {pk: c[key]}
            c[template_object_name] = get_object_or_404(qs, **d)
        elif isinstance(qs, str):
            c[template_object_name] = c[qs]
        else:
            raise Exception
        d = {c[field]: key}
        c[template_object_name] = qs.get(**d)
        return c
    return _get_object

def edit(Form, key='obj', method='POST'):
    def _edit(request, c):
        edit_form = Form(request.__getattribute__(method) or None, instance=c[key])
        if edit_form.is_valid():
            c[key] = edit_form.save()
            # redirect?
        return c 

def template(template_path):
    def _template(request, c):
        c['template_path'] = template_path
        return c
    return _template

def render(request, c):
    return render_to_response(c['template_path'], c)

def chain_view(*partials):
    def _chain(request, **kwargs):
        r, c = request, kwargs
        for partial in partials:
            r, c = partial(r, c)
            if isinstance(c, HttpResponse):
                return c
        return c 
        

@@ -1,83 +1,4 @@
-from django.db.models import Model%0Afrom django.db.models.query import QuerySet%0A
 from
@@ -100,35 +100,63 @@
 _404
-%0A%0Adef model_base(qs, key='q
+, _get_queryset%0A%0Adef detail(fields, template_var='field
 s'):
@@ -165,26 +165,22 @@
    def _
-model_base
+detail
 (request
@@ -197,258 +197,116 @@
     
-if isinstance(qs, Model):%0A            c%5Bkey%5D = qs.objects.all()%0A        elif isinstance(qs, QuerySet):%0A            c%5Bkey%5D = qs%0A        else:%0A            raise Exception%0A        return c%0A    return _model_base%0A%0Adef filter(fields, qs='qs', query_key='q
+c%5Btemplate_var%5D = fields%0A        return request, c%0A    return _detail%0A%0Adef list(fields, template_var='fields
 '):%0A
@@ -314,22 +314,20 @@
    def _
-filter
+list
 (request
@@ -344,11 +344,31 @@
     
-pas
+c%5Btemplate_var%5D = field
 s%0A  
@@ -381,14 +381,12 @@
 rn _
-filter
+list
 %0A%0Ade
@@ -402,17 +402,23 @@
 ect(
-qs='qs', 
+Model, request_
 key=
@@ -423,16 +423,22 @@
 y='id', 
+model_
 pk='pk',
@@ -511,155 +511,74 @@
     
- #
  qs 
-can be one of the following: Model, query, key for qs in vars%0A        if isinstance(qs, (Model, QuerySet)):%0A            d = %7Bpk: c%5B
+= _get_queryset(Model)%0A        d = %7Bmodel_pk: c%5Brequest_
 key%5D%7D%0A
-    
     
@@ -611,182 +611,178 @@
 %5D = 
-get_object_or_404(qs, **d)%0A        elif isinstance(qs, str):%0A            c%5Btemplate_object_name%5D = c%5Bqs%5D%0A        else:%0A            raise Exception%0A        d = %7Bc%5Bfield%5D: key%7D
+qs.get(**d)%0A        return request, c%0A    return _get_object%0A%0Adef get_list(Model, template_qs_name='qs'):%0A    def _get_list(request, c):%0A        qs = _get_queryset(Model)
 %0A   
@@ -789,38 +789,34 @@
      c%5Btemplate_
-object
+qs
 _name%5D = qs.get(
@@ -806,33 +806,24 @@
 s_name%5D = qs
-.get(**d)
 %0A        ret
@@ -817,32 +817,41 @@
 s%0A        return
+ request,
  c%0A    return _g
@@ -849,29 +849,27 @@
 return _get_
-objec
+lis
 t%0A%0Adef edit(
@@ -874,19 +874,65 @@
 t(Form, 
-key
+template_form_name='edit_form', template_obj_name
 ='obj', 
@@ -1052,19 +1052,33 @@
 tance=c%5B
-key
+template_obj_name
 %5D)%0A     
@@ -1119,19 +1119,24 @@
       c%5B
-key
+template
 %5D = edit
@@ -1182,24 +1182,33 @@
       return
+ request,
  c %0A%0Adef tem
@@ -1313,24 +1313,33 @@
       return
+ request,
  c%0A    retur
@@ -1385,16 +1385,25 @@
   return
+ request,
  render_
@@ -1479,16 +1479,21 @@
 f _chain
+_view
 (request
@@ -1693,13 +1693,28 @@
  c %0A    
-    
+return _chain_view%0A
 %0A

remove model_base in favor of get_object and get_list. function parameters now make some sort of sense.