Alternative to Class Based Views

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#################################################################
# Callable class metaclass magic - borrowed from stackoverflow.
#################################################################

from django.http import HttpResponse, HttpRequest, HttpResponseNotAllowed, HttpResponseBadRequest

class CallableClass(type):
    def __call__(cls, *args, **kwargs):
        if args and isinstance(args[0], HttpRequest):
            instance = super(CallableClass, cls).__call__()
            return instance.__call__(*args, **kwargs)
        else:
            instance = super(CallableClass, cls).__call__(*args, **kwargs)
            return instance

class View(object):
    __metaclass__ = CallableClass

    def __call__(self, request, *args, **kwargs):
        if hasattr(self, request.method):
            handler = getattr(self, request.method)
            if hasattr(handler, '__call__'):
                return handler(request, *args, **kwargs)
        return HttpResponseBadRequest('Method Not Allowed', status=405)

#################################################################
# Our URLs file
#################################################################

from models import LoginView
from models import AccountView
from models import AdminView

urlpatterns = patterns('',

    # /login
    url(r'^login$', LoginView, name = "login"),

    # /account
    url(r'^account$', AccountView, name = "account"),

    # /admin
    url(r'^admin$', AdminView, name = "admin"),
)

#################################################################
# Our base class for all views, although I'm not sure if method decorators would still work
#################################################################

class BaseView(View):

    # Sets our default view parameters
    auth_required = True
    staff_required = False

    def __call__(self, request, *args, **kwargs):
        """This allows us to automatically check the security context
        for objects"""

        # check if we are logged in
        if self.auth_required and not request.user:
            if _partial:
                raise Exception, "You are not authenticated"

            logout(request)
            return redirect("login")

        if self.staff_required ans not request.is_staff():
            raise Exception, "User does not have staff permissions"

        cx = RequestContext(request, {
            'custom_field' : "custom_value",
        })

        return super(PortalView, self).__call__(request, cx, org, domain, *args, **kwargs)

class LoginView(BaseView):
    """Shows login page"""

    auth_required = False
    staff_required = False

    def GET(self, request, cx, *args, **kwargs):
        return render_to_response("login.html", context_instance = cx)


class AccountView(BaseView):
    """Shows account overview page"""

    auth_required = True
    staff_required = False

    def GET(self, request, cx, org, domain, *args, **kwargs):
        return render_to_response("account.html", context_instance = cx)

class AdminView(BaseView):
    """Shows account overview page"""

    auth_required = True
    staff_required = True

    def GET(self, request, cx, org, domain, *args, **kwargs):
        return render_to_response("admin.html", context_instance = cx)

More like this

  1. Log all interaction with user to the DB by inuwashi 2 years, 4 months ago
  2. Make runfcgi fail when database connection is open before fork by mpasternacki 2 years, 2 months ago
  3. Form widget for text inputs with external link. version2 by ramusus 3 years, 11 months ago
  4. Class Feeds DRY TemplateTag by gmandx 3 years ago
  5. is_dirty and dict of changed values by jaredthane 3 years, 9 months ago

Comments

sv1jsb (on September 13, 2012):

At line 75 shouldn't this be:

return super(BaseView, self).__call__(request, cx, org, domain, *args, **kwargs)

#

siblek31 (on April 13, 2013):

Kesehatan masyarakat agar cepat hamil juga mengambil berbagai tindakan untuk membatasi kesenjangan kesehatan antara daerah yang berbeda negara dan, dalam beberapa kasus, benua atau dunia. Salah soal ulangan sd satu masalah adalah akses individu dan masyarakat untuk perawatan kesehatan dalam hal keuangan, kendala geografis atau sosial budaya untuk mengakses dan menggunakan layanan. [Rujukan?] Aplikasi dari sistem kesehatan publik meliputi bidang kesehatan ibu dan anak, administrasi pelayanan kesehatan, tanggap darurat, dan belajar bahasa inggris pencegahan dan pengendalian penyakit menular dan kronis.

Dampak positif yang besar dari kursus teknisi komputer program kesehatan masyarakat secara luas diakui. dimana lagi Sebagian karena kebijakan dan tindakan dikembangkan melalui kesehatan masyarakat, abad ke-20 mencatat penurunan kursus bahasa inggris online angka kematian bayi dan anak-anak dan peningkatan terus-menerus dalam cara mendapatkan uang dari internet harapan hidup di sebagian besar dunia. Sebagai contoh, diperkirakan bahwa harapan hidup telah meningkat untuk Amerika oleh tiga puluh tahun sejak tahun 1900, [24] dan di seluruh dunia oleh enam tahun sejak tahun 1990. [25]

#

(Forgotten your password?)