Source

djangorators / examples / djangorators_testproject / views.py

Full commit
from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.views.generic.simple import direct_to_template

from djangorators import from_object, from_queryset, owner_required, as_html
from djangorators_testproject.models import Animal

def classic_animal_item(request, animal_id):
    # Retrieve item instance
    animal = get_object_or_404(Animal, id=animal_id)
    
    # Check permissions
    if animal.owner != request.user:
        return HttpResponseNotFound()
    
    # Do something useful (isn't it?)
    animal.name = '%s (updated)' % animal.name
    animal.save()
    
    # Render response
    return direct_to_template(request, 'animals/item.html', locals())

@from_object(Animal)
@owner_required
@as_html('animals/item.html')
def decorated_animal_item(request, *args, **kwargs):
    animal = kwargs['related_object']
    
    # Only do something useful
    animal.name = '%s (updated)' % animal.name
    animal.save()
    
    return locals()

def classic_animal_list(request):
    # Retrieve queryset
    animals = Animal.objects.all()
    
    # Check permissions
    animals = animals.filter(owner=request.user)
    
    # Do something useful
    animals = animals.exclude(name=u'lion')
    
    # Render response
    return direct_to_template(request, 'animals/list.html', locals())    

@from_queryset(Animal.objects.all())
@owner_required
@as_html('animals/list.html')
def decorated_animal_list(request, *args, **kwargs):
    animals = kwargs['related_queryset']
    
    # Only do something useful
    animals = animals.exclude(name=u'lion')
    
    return locals()