github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

zerok / django-oopviews

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 13
    • 2
  • Source
  • Commits
  • Network (2)
  • Issues (0)
  • Downloads (1)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (1)
    • 0.2.0
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

django-oopviews provides a simple way to have some object orientation in Django's view layer — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Also provide access to the wrapped class from within the view function 
zerok (author)
Mon Nov 10 16:46:35 -0800 2008
commit  8b80cae437b6089310ae12dd76532624c84db18b
tree    455706c02fb0a5dfcb29572779c2dde34ecb3c1c
parent  2ed44332279daefbae0804f9f060901c38ff34cd
django-oopviews /
name age
history
message
file .gitignore Loading commit data...
file LICENSE.txt
file README.rst
directory django_oopviews/
file ez_setup.py
file setup.py
README.rst

django-oopviews

Contents

  • django-oopviews
    • Base-implementation
    • Addons
      • Content-Type-Negotitation with OOPViews
    • History

Base-implementation

In some instances you end up producing tons of views that actually do mostly the same except for perhaps one or two lines. This module offers you a simple alternative:

from django_oopviews.base import create_view, BaseView

class View1(BaseView):
    def __init__(self, request, *args, **kwargs):
        # Here you have your common code
        self.my_variable = 1
    def __call__(self, request, *args, **kwargs):
        whatever = self.my_variable + 1
        return HttpResponse(whatever)

class View2(View1):
    def __call__(self, request, *args, **kwargs):
        return HttpResponse(self.my_variable)

view1 = create_view(View1)
view2 = create_view(View2)

In this example, the code in View1.__init__ is shared between View1 and View2, so you don't need to write it again.

If you want to share some HttpResponse post-processing, implement the BaseView.__after__(self, response_obj) method

For more details check out this blog post

Addons

Content-Type-Negotitation with OOPViews

In some situations it comes in handy, to do some content type negotiation to really provide an optimized view for the user depending on what a user's application supports (say WML or HTML or XML over HTML). HTTP/1.1 handles this using the "Accept"-request header to give the user the option, to say what kind of content type she'd prefer or give a list of content types prioritized with a value between 0 and 1.

This abstract view class should demonstrate, how you can easily handle such situations within Django purely in the view code. The idea is pretty simple: Simply use the __call__ method as dispatcher for content-type-specific methods.

To use this code, simply inherit the basic implementation and then specify your content-type-specific methods and register them in the ctn_accept_binding-dictionary:

from django.http import HttpResponse
from django_oopview import ctn

class TestView(ctn.AbstractCTNView):
    ctn_accept_binding = {
        'text/html': 'html',
        'text/*': 'html',
        '*/*': 'html',
    }

    def html(self, request, *args, **kwargs):
        return HttpResponse("Hello", mimetype='text/html')

The ctn_accept_binding-dictionary not only allows you to bind a method to a content-type, but if you set a value to a tuple instead of just a string, it will take the first element of that tuple as a priority value similar to the one used in the "Accept"-handling. This way, you can prioritize methods for the case, that the user requests any type of a given family like for instance 'text/*'.

History

0.2 (Oct 1 2008)
comes as its own library using setuptools and offering with the django_oopviews.ctn module a simple implementation of content negotiation in HTTP using OOPViews.
0.1 (inofficial)
This version only included the BaseView as well as the create_view function and was bundled with the django-zsutils library.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server