Login

Simple Class Based View Wrapper

Author:
mattjmorrison
Posted:
June 2, 2010
Language:
Python
Version:
1.2
Score:
0 (after 2 ratings)

After reading this article: http://blog.roseman.org.uk/2010/06/2/class-based-views-and-thread-safety/

and checking out this snippet: http://djangosnippets.org/snippets/2046/

I realized that I was using a class based view without even thinking about the consequences... so, without having to completely re-factor my existing class based views to make them singletons, I wrote this wrapper that should allow my class based views to work as I intended, and maintain state for a single request only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
"Django Extension View utilities"

class ClassView():
    """
    this acts as a 'buffer' to make class based views thread safe
    usage:
    urlpatterns = patterns('accounts',
        url(r'^new/$', ClassView(views.NewBuild), name="new"),
    )
    """

    def __init__(self, class_name):
        "store the class name in an instance variable"
        self.class_name = class_name

    def __call__(self, request, *args, **kwargs):
        """each time the class_view is invoked - for each request
        new-up a class_name and call it"""
        view = self.class_name()
        return view(request, *args, **kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.