Django - Know Your Namespace: Middleware

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Django - Know Your Namespace: Middleware - Presentation Transcript

    1. Know your namespace A peek inside a django namespace. (and an open source project implementation: django-db-log)
    2. django.middleware
    3. Pluggable
    4. why do i care? simple dummy...
    5. available middleware provides: • session handling • internationalization • caching • db transactions • authentication • gzip compression • flat pages • etc.
    6. ...umm, yea i could use those.
    7. activating middleware MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', )
    8. need a custom solution?
    9. easy as pie
    10. Simple Guidelines • don’t have to subclass anything • live anywhere on the python path (reusable!) • must define one or more of these 4 methods...
    11. feeling the flow, being the ball process process process call view request view response exception? process exception
    12. Real World Example
    13. from models import Error, ErrorBatch import traceback import socket import warnings Look mom, no hands! import md5 class DBLogMiddleware(object): def process_exception(self, request, exception): server_name = socket.gethostname() tb_text = traceback.format_exc() class_name = exception.__class__.__name__ checksum = md5.new(tb_text).hexdigest() defaults = dict( class_name = class_name, message = exception.message, url = request.build_absolute_uri(), server_name = server_name, traceback = tb_text, ) try: Error.objects.create(**defaults) batch, created = ErrorBatch.objects.get_or_create( class_name = class_name, server_name = server_name, checksum = checksum, defaults = defaults ) if not created: batch.times_seen += 1 batch.save() except Exception, exc: warnings.warn(unicode(exc))
    14. Discussion • http://code.google.com/p/django-db-log/ • http://www.davidcramer.net/ • http://www.djangoproject.com/documentation/middleware/

    + howiworkdailyhowiworkdaily, 2 years ago

    custom

    1035 views, 2 favs, 1 embeds more stats

    Presentation for the inaugural django-nyc meetup. W more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1035
      • 1034 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 51
    Most viewed embeds
    • 1 views on http://www.slideshare.net

    more

    All embeds
    • 1 views on http://www.slideshare.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories