| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | PyLucid common middleware |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | Big work-a-round for the install section. Some django middlewares needs |
|---|
| 8 | his database tables. The user can create it with syncdb in the _install |
|---|
| 9 | section. So we need a way to activate the middlewares only if syncdb was |
|---|
| 10 | done in the past. |
|---|
| 11 | Here we try to load all middlewares. If a "no such table" error occurs |
|---|
| 12 | we send a info page to the user. |
|---|
| 13 | |
|---|
| 14 | Last commit info: |
|---|
| 15 | ~~~~~~~~~~~~~~~~~ |
|---|
| 16 | $LastChangedDate: $ |
|---|
| 17 | $Rev: $ |
|---|
| 18 | $Author: $ |
|---|
| 19 | |
|---|
| 20 | :copyleft: 2008 by the PyLucid team, see AUTHORS for more details. |
|---|
| 21 | :license: GNU GPL v3 or above, see LICENSE for more details. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | from django.conf import settings |
|---|
| 25 | |
|---|
| 26 | #try: |
|---|
| 27 | # from threading import local |
|---|
| 28 | #except ImportError: |
|---|
| 29 | # from django.utils._threading_local import local |
|---|
| 30 | |
|---|
| 31 | from django.contrib.sessions.middleware import SessionMiddleware |
|---|
| 32 | from django.contrib.auth.middleware import AuthenticationMiddleware |
|---|
| 33 | from django.contrib.auth.models import AnonymousUser |
|---|
| 34 | from django.middleware.locale import LocaleMiddleware |
|---|
| 35 | |
|---|
| 36 | from PyLucid.system.exceptions import LowLevelError |
|---|
| 37 | from PyLucid.system.template import render_help_page |
|---|
| 38 | from PyLucid.system.utils import setup_request |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | session_middleware = SessionMiddleware() |
|---|
| 42 | auth_middleware = AuthenticationMiddleware() |
|---|
| 43 | locale_middleware = LocaleMiddleware() |
|---|
| 44 | |
|---|
| 45 | #_thread_locals = local() |
|---|
| 46 | # |
|---|
| 47 | #def get_local_request(): |
|---|
| 48 | # """ |
|---|
| 49 | # Get current threading local request object |
|---|
| 50 | # """ |
|---|
| 51 | # return _thread_locals.request |
|---|
| 52 | ## return getattr(_thread_locals, 'request', None) |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | def raise_non_table_error(e): |
|---|
| 56 | """ |
|---|
| 57 | Raise the original error, if it is not the table access problem. |
|---|
| 58 | """ |
|---|
| 59 | err_msg = str(e) |
|---|
| 60 | if not "no such table" in err_msg: |
|---|
| 61 | # raise the previouse error |
|---|
| 62 | raise |
|---|
| 63 | |
|---|
| 64 | if "PyLucidPlugins_" in err_msg: |
|---|
| 65 | # raise the previouse error for missing plugin models tables |
|---|
| 66 | raise |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | class PyLucidCommonMiddleware(object): |
|---|
| 70 | """ |
|---|
| 71 | Load the django middlewares: |
|---|
| 72 | - 'django.contrib.sessions.middleware.SessionMiddleware' |
|---|
| 73 | - 'django.contrib.auth.middleware.AuthenticationMiddleware' |
|---|
| 74 | - 'django.middleware.locale.LocaleMiddleware' |
|---|
| 75 | |
|---|
| 76 | If the database tables not created (syncdb not executed, yet) send the user |
|---|
| 77 | a help page. |
|---|
| 78 | """ |
|---|
| 79 | def process_request(self, request): |
|---|
| 80 | # add "debug" and "page_msg" to the request object. Redirect warnings. |
|---|
| 81 | setup_request(request) |
|---|
| 82 | |
|---|
| 83 | # Attach the current request |
|---|
| 84 | # _thread_locals.request = request |
|---|
| 85 | |
|---|
| 86 | try: |
|---|
| 87 | session_middleware.process_request(request) |
|---|
| 88 | auth_middleware.process_request(request) |
|---|
| 89 | locale_middleware.process_request(request) |
|---|
| 90 | except Exception, e: |
|---|
| 91 | raise_non_table_error(e) |
|---|
| 92 | # These exist no database tables, the django session/auth middelware |
|---|
| 93 | # can't work. But e.g. in the PyLucid cache middleware we check if |
|---|
| 94 | # the user is anonymous, so we add the anonymous user object here: |
|---|
| 95 | # django.contrib.auth.models.AnonymousUser |
|---|
| 96 | request.user = AnonymousUser() |
|---|
| 97 | |
|---|
| 98 | def process_view(self, request, view_func, view_args, view_kwargs): |
|---|
| 99 | try: |
|---|
| 100 | # start the view |
|---|
| 101 | return view_func(request, *view_args, **view_kwargs) |
|---|
| 102 | except LowLevelError, e: |
|---|
| 103 | error_msg, e = e |
|---|
| 104 | return render_help_page(request, error_msg, e) |
|---|
| 105 | except Exception, e: |
|---|
| 106 | raise_non_table_error(e) |
|---|
| 107 | |
|---|
| 108 | url = request.META["PATH_INFO"] |
|---|
| 109 | if settings.INSTALL_URL_PREFIX in url or \ |
|---|
| 110 | settings.MEDIA_URL in url: |
|---|
| 111 | # Skip the middleware, if we are in the _install section or |
|---|
| 112 | # it's a static file request with the dev. server |
|---|
| 113 | return |
|---|
| 114 | |
|---|
| 115 | error_msg = "Can't get a database table." |
|---|
| 116 | return render_help_page(request, error_msg, e) |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | def process_response(self, request, response): |
|---|
| 120 | try: |
|---|
| 121 | return session_middleware.process_response(request, response) |
|---|
| 122 | except Exception, e: |
|---|
| 123 | raise_non_table_error(e) |
|---|
| 124 | |
|---|
| 125 | return response |
|---|