annotate MoinMoin/web/contexts.py @ 4607:d8e5e9cfadf1

always do 'from werkzeug import ...', except for contrib, exceptions, routing, script
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 23 Feb 2009 22:39:32 +0100
parents d706f5d4f4ec
children 3a79d3ca5a83
rev   line source
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 """
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 MoinMoin - Context objects which are passed thru instead of the classic
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4 request objects. Currently contains legacy wrapper code for
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
5 a single request object.
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
6
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
7 @copyright: 2008-2008 MoinMoin:FlorianKrupicka
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
9 """
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
10
4334
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
11 import time, inspect, StringIO, sys, warnings
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
12
4607
d8e5e9cfadf1 always do 'from werkzeug import ...', except for contrib, exceptions, routing, script
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4594
diff changeset
13 from werkzeug import Headers, http_date, create_environ, redirect, abort
d8e5e9cfadf1 always do 'from werkzeug import ...', except for contrib, exceptions, routing, script
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4594
diff changeset
14 from werkzeug.exceptions import Unauthorized, NotFound
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
15
4429
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
16 from MoinMoin import i18n, error, user, config, wikiutil
4164
0be4fb46e492 Moved even more attributes into (lazy) properties
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4156
diff changeset
17 from MoinMoin.config import multiconfig
4156
40501b3e2176 Moved some attributes on Request into RenderContext
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4155
diff changeset
18 from MoinMoin.formatter import text_html
4170
02fcab41459f Factored out some of the loadTheme code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4169
diff changeset
19 from MoinMoin.theme import load_theme_fallback
4182
ca0cf44dab89 Moved request.clock into property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4180
diff changeset
20 from MoinMoin.util.clock import Clock
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4246
diff changeset
21 from MoinMoin.web.request import Request, MoinMoinFinish
4308
3a4426122606 Code review: factor in the simple spider check
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4300
diff changeset
22 from MoinMoin.web.utils import UniqueIDGenerator
4153
75b0b7f32423 Replace makeForbidden with HTTPException variant for WSGI
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4152
diff changeset
23 from MoinMoin.web.exceptions import Forbidden, SurgeProtection
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
24
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
25 from MoinMoin import log
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
26 logging = log.getLogger(__name__)
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
27 NoDefault = object()
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
28
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
29 class EnvironProxy(property):
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
30 """ Proxy attribute lookups to keys in the environ. """
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
31 def __init__(self, name, default=NoDefault):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
32 """
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
33 An entry will be proxied to the supplied name in the .environ
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
34 object of the property holder. A factory can be supplied, for
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
35 values that need to be preinstantiated. If given as first
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
36 parameter name is taken from the callable too.
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
37
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
38 @param name: key (or factory for convenience)
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
39 @param default: literal object or callable
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
40 """
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
41 if not isinstance(name, basestring):
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
42 default = name
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
43 name = default.__name__
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
44 self.name = 'moin.' + name
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
45 self.default = default
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
46 property.__init__(self, self.get, self.set, self.delete)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
47
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
48 def get(self, obj):
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
49 if self.name in obj.environ:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
50 res = obj.environ[self.name]
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
51 else:
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
52 factory = self.default
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
53 if factory is NoDefault:
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
54 raise AttributeError(self.name)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
55 elif hasattr(factory, '__call__'):
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
56 res = obj.environ.setdefault(self.name, factory(obj))
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
57 else:
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
58 res = obj.environ.setdefault(self.name, factory)
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
59 return res
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
60
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
61 def set(self, obj, value):
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
62 obj.environ[self.name] = value
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
63
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
64 def delete(self, obj):
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
65 del obj.environ[self.name]
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
66
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
67 def __repr__(self):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
68 return "<%s for '%s'>" % (self.__class__.__name__,
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
69 self.name)
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
70
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
71 class Context(object):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
72 """ Standard implementation for the context interface.
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
73
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
74 This one wraps up a Moin-Request object and the associated
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
75 environ and also keeps track of it's changes.
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
76 """
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
77 def __init__(self, request):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
78 assert isinstance(request, Request)
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
79
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
80 self.request = request
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
81 self.environ = environ = request.environ
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
82 self.personalities = self.environ.setdefault(
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
83 'context.personalities', []
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
84 )
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
85 self.personalities.append(self.__class__.__name__)
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
86
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
87 def become(self, cls):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
88 """ Become another context, based on given class.
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
89
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
90 @param cls: class to change to, must be a sister class
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
91 @rtype: boolean
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
92 @return: wether a class change took place
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
93 """
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
94 if self.__class__ is cls:
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
95 return False
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
96 else:
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
97 self.personalities.append(cls)
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
98 self.__class__ = cls
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
99 return True
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
100
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
101 def __repr__(self):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
102 return "<%s %r>" % (self.__class__.__name__, self.personalities)
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
103
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
104 class BaseContext(Context):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
105 """ Implements a basic context, that provides some common attributes.
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
106 Most attributes are lazily initialized via descriptors. """
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
107
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
108 # first the trivial attributes
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
109 action = EnvironProxy('action', lambda o: o.request.values.get('action', 'show'))
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
110 clock = EnvironProxy('clock', lambda o: Clock())
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
111 user = EnvironProxy('user', lambda o: user.User(o, auth_method='request:invalid'))
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
112
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4233
diff changeset
113 lang = EnvironProxy('lang')
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
114 content_lang = EnvironProxy('content_lang', lambda o: o.cfg.language_default)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
115 current_lang = EnvironProxy('current_lang')
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
116
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
117 html_formatter = EnvironProxy('html_formatter', lambda o: text_html.Formatter(o))
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
118 formatter = EnvironProxy('formatter', lambda o: o.html_formatter)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
119
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
120 page = EnvironProxy('page', None)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
121
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
122 # now the more complex factories
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
123 def cfg(self):
4327
62177a952833 merged moin/1.8 repo (tests need more fixing)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4315
diff changeset
124 if self.request.given_config is not None:
62177a952833 merged moin/1.8 repo (tests need more fixing)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4315
diff changeset
125 return self.request.given_config('MoinMoin._tests.wikiconfig')
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
126 try:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
127 self.clock.start('load_multi_cfg')
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
128 cfg = multiconfig.getConfig(self.request.url)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
129 self.clock.stop('load_multi_cfg')
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
130 return cfg
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
131 except error.NoConfigMatchedError:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
132 raise NotFound('<p>No wiki configuration matching the URL found!</p>')
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
133 cfg = EnvironProxy(cfg)
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
134
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
135 def getText(self):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
136 lang = self.lang
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
137 def _(text, i18n=i18n, request=self, lang=lang, **kw):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
138 return i18n.getText(text, request, lang, **kw)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
139 return _
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4233
diff changeset
140
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4233
diff changeset
141 getText = property(getText)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4233
diff changeset
142 _ = getText
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
143
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
144 def isSpiderAgent(self):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
145 """ Simple check if useragent is a spider bot. """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
146 cfg = self.cfg
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
147 user_agent = self.request.user_agent
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
148 if user_agent and cfg.cache.ua_spiders:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
149 return cfg.cache.ua_spiders.search(user_agent.browser) is not None
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
150 return False
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
151 isSpiderAgent = EnvironProxy(isSpiderAgent)
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
152
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
153 def rootpage(self):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
154 from MoinMoin.Page import RootPage
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
155 return RootPage(self)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
156 rootpage = EnvironProxy(rootpage)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
157
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
158 def rev(self):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
159 try:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
160 return int(self.values['rev'])
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
161 except:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
162 return None
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
163 rev = EnvironProxy(rev)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
164
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
165 def _theme(self):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
166 self.initTheme()
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
167 return self.theme
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
168 theme = EnvironProxy('theme', _theme)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
169
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
170 # finally some methods to act on those attributes
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
171 def setContentLanguage(self, lang):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
172 """ Set the content language, used for the content div
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
173
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
174 Actions that generate content in the user language, like search,
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
175 should set the content direction to the user language before they
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
176 call send_title!
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
177 """
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
178 self.content_lang = lang
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
179 self.current_lang = lang
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
180
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
181 def initTheme(self):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
182 """ Set theme - forced theme, user theme or wiki default """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
183 if self.cfg.theme_force:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
184 theme_name = self.cfg.theme_default
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
185 else:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
186 theme_name = self.user.theme_name
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
187 load_theme_fallback(self, theme_name)
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
188
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
189
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
190 class HTTPContext(BaseContext):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
191 """ Context that holds attributes and methods for manipulation of
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
192 incoming and outgoing HTTP data. """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
193
4191
5fdb6f984a54 Property for session and dummy user object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4189
diff changeset
194 session = EnvironProxy('session')
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
195 _auth_redirected = EnvironProxy('old._auth_redirected', 0)
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
196 cacheable = EnvironProxy('old.cacheable', 0)
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
197 writestack = EnvironProxy('old.writestack', lambda o: list())
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
198
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
199 # proxy some descriptors of the underlying WSGI request, since
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
200 # setting on those does not work over __(g|s)etattr__-proxies
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
201 class _proxy(property):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
202 def __init__(self, name):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
203 self.name = name
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
204 property.__init__(self, self.get, self.set, self.delete)
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
205 def get(self, obj):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
206 return getattr(obj.request, self.name)
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
207 def set(self, obj, value):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
208 setattr(obj.request, self.name, value)
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
209 def delete(self, obj):
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
210 delattr(obj.request, self.name)
4230
2074c2204d03 Proxied mimetype/content_type response header to underlying request object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4224
diff changeset
211
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
212 mimetype = _proxy('mimetype')
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
213 content_type = _proxy('content_type')
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
214 status = _proxy('status')
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
215 status_code = _proxy('status_code')
4230
2074c2204d03 Proxied mimetype/content_type response header to underlying request object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4224
diff changeset
216
4233
1c412625fdda Removed slots from Context-class, proxied some more attributes to underlying Request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4230
diff changeset
217 del _proxy
4230
2074c2204d03 Proxied mimetype/content_type response header to underlying request object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4224
diff changeset
218
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
219 # proxy further attribute lookups to the underlying request first
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
220 def __getattr__(self, name):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
221 try:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
222 return getattr(self.request, name)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
223 except AttributeError, e:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
224 return super(HTTPContext, self).__getattribute__(name)
4298
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
225
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
226 # methods regarding manipulation of HTTP related data
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
227 def read(self, n=None):
4298
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
228 """ Read n bytes (or everything) from input stream. """
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
229 if n is None:
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
230 return self.request.in_data
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
231 else:
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
232 return self.request.input_stream.read(n)
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
233
4153
75b0b7f32423 Replace makeForbidden with HTTPException variant for WSGI
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4152
diff changeset
234 def makeForbidden(self, resultcode, msg):
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
235 status = {401: Unauthorized,
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
236 403: Forbidden,
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
237 404: NotFound,
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
238 503: SurgeProtection}
4153
75b0b7f32423 Replace makeForbidden with HTTPException variant for WSGI
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4152
diff changeset
239 raise status[resultcode](msg)
75b0b7f32423 Replace makeForbidden with HTTPException variant for WSGI
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4152
diff changeset
240
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
241 def setHttpHeader(self, header):
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
242 header, value = header.split(':', 1)
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
243 self.headers.add(header, value)
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
244
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
245 def disableHttpCaching(self, level=1):
4298
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
246 """ Prevent caching of pages that should not be cached.
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
247
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
248 level == 1 means disabling caching when we have a cookie set
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
249 level == 2 means completely disabling caching (used by Page*Editor)
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
250
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
251 This is important to prevent caches break acl by providing one
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
252 user pages meant to be seen only by another user, when both users
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
253 share the same caching proxy.
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
254
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
255 AVOID using no-cache and no-store for attachments as it is completely broken on IE!
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
256
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
257 Details: http://support.microsoft.com/support/kb/articles/Q234/0/67.ASP
4744c6a5bdf0 Inserted docstrings from old MoinMoin.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4297
diff changeset
258 """
4246
44b5548fe213 Eliminated legacy _cache_disabled property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4241
diff changeset
259 if level == 1 and self.headers.get('Pragma') == 'no-cache':
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
260 return
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
261
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
262 if level == 1:
4246
44b5548fe213 Eliminated legacy _cache_disabled property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4241
diff changeset
263 self.headers.set('Cache-Control', 'private, must-revalidate, mag-age=10')
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
264 elif level == 2:
4246
44b5548fe213 Eliminated legacy _cache_disabled property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4241
diff changeset
265 self.headers.set('Cache-Control', 'no-cache')
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
266 self.headers.set('Pragma', 'no-cache')
4246
44b5548fe213 Eliminated legacy _cache_disabled property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4241
diff changeset
267 self.request.expires = time.time() - 3600 * 24 * 365
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
268
4260
f70003d57e47 Added http_redirect for compatibility
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4255
diff changeset
269 def http_redirect(self, url):
f70003d57e47 Added http_redirect for compatibility
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4255
diff changeset
270 """ Raise a simple redirect exception. """
f70003d57e47 Added http_redirect for compatibility
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4255
diff changeset
271 abort(redirect(url))
f70003d57e47 Added http_redirect for compatibility
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4255
diff changeset
272
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
273 # the output related methods
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
274 def write(self, *data):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
275 """ Write to output stream. """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
276 self.request.stream.writelines(data)
4164
0be4fb46e492 Moved even more attributes into (lazy) properties
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4156
diff changeset
277
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
278 def redirectedOutput(self, function, *args, **kw):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
279 """ Redirect output during function, return redirected output """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
280 buf = StringIO.StringIO()
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
281 self.redirect(buf)
4164
0be4fb46e492 Moved even more attributes into (lazy) properties
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4156
diff changeset
282 try:
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
283 function(*args, **kw)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
284 finally:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
285 self.redirect()
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
286 text = buf.getvalue()
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
287 buf.close()
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
288 return text
4155
7e59a7989b23 Make getText into a context property
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4154
diff changeset
289
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
290 def redirect(self, file=None):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
291 """ Redirect output to file, or restore saved output """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
292 if file:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
293 self.writestack.append(self.write)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
294 self.write = file.write
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
295 else:
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
296 self.write = self.writestack.pop()
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
297
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
298 def send_file(self, fileobj, bufsize=8192, do_flush=None):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
299 """ Send a file to the output stream.
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
300
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
301 @param fileobj: a file-like object (supporting read, close)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
302 @param bufsize: size of chunks to read/write
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
303 @param do_flush: call flush after writing?
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
304 """
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
305 def simple_wrapper(fileobj, bufsize):
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
306 return iter(lambda: fileobj.read(bufsize), '')
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
307 file_wrapper = self.environ.get('wsgi.file_wrapper', simple_wrapper)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
308 self.request.response = file_wrapper(fileobj, bufsize)
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
309 raise MoinMoinFinish('sent file')
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4148
diff changeset
310
4334
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
311 # fully deprecated functions, with warnings
4579
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
312 def getScriptname(self):
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
313 warnings.warn(
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
314 "request.getScriptname() is deprecated, please use the request's script_root property.",
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
315 DeprecationWarning)
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
316 return self.request.script_root
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
317
4334
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
318 def getBaseURL(self):
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
319 warnings.warn(
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
320 "request.getBaseURL() is deprecated, please use the request's "
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
321 "url_root property or the abs_href object if urls should be generated.",
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
322 DeprecationWarning)
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
323 return self.request.url_root
7add275cf4de Fixed: deprecated (and missing) getBaseURL() method
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4327
diff changeset
324
4429
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
325 def getQualifiedURL(self, uri=''):
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
326 """ Return an absolute URL starting with schema and host.
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
327
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
328 Already qualified urls are returned unchanged.
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
329
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
330 @param uri: server rooted uri e.g /scriptname/pagename.
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
331 It must start with a slash. Must be ascii and url encoded.
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
332 """
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
333 import urlparse
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
334 scheme = urlparse.urlparse(uri)[0]
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
335 if scheme:
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
336 return uri
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
337
4579
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
338 host_url = self.request.host_url.rstrip('/')
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
339 result = "%s%s" % (host_url, uri)
4429
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
340
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
341 # This might break qualified urls in redirects!
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
342 # e.g. mapping 'http://netloc' -> '/'
4579
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
343 result = wikiutil.mapURL(self, result)
87d97510de79 getScriptname() -> script_root, getBaseURL() -> url_root (for werkzeug API)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4429
diff changeset
344 return result
4429
574644cfc6d2 add missing request.getQualifiedURL()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4334
diff changeset
345
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
346 class AuxilaryMixin(object):
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
347 """
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
348 Mixin for diverse attributes and methods that aren't clearly assignable
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
349 to a particular phase of the request.
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
350 """
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
351
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
352 # several attributes used by other code to hold state across calls
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
353 _fmt_hd_counters = EnvironProxy('_fmt_hd_counters')
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
354 parsePageLinks_running = EnvironProxy('parsePageLinks_running', lambda o: {})
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
355 mode_getpagelinks = EnvironProxy('mode_getpagelinks', 0)
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
356
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4191
diff changeset
357 pragma = EnvironProxy('pragma', lambda o: {})
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4191
diff changeset
358 _login_messages = EnvironProxy('_login_messages', lambda o: [])
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4191
diff changeset
359 _login_multistage = EnvironProxy('_login_multistage', None)
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
360 _login_multistage_name = EnvironProxy('_login_multistage_name', None)
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4191
diff changeset
361 _setuid_real_user = EnvironProxy('_setuid_real_user', None)
4200
c21991eed9c9 Fixed: attribute lookup handling, in general as in uid_generator
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
362 pages = EnvironProxy('pages', lambda o: {})
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
363
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
364 def uid_generator(self):
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
365 pagename = None
4200
c21991eed9c9 Fixed: attribute lookup handling, in general as in uid_generator
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
366 if hasattr(self, 'page') and hasattr(self.page, 'page_name'):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
367 pagename = self.page.page_name
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
368 return UniqueIDGenerator(pagename=pagename)
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
369 uid_generator = EnvironProxy(uid_generator)
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
370
4165
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
371 def dicts(self):
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
372 """ Lazy initialize the dicts on the first access """
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
373 from MoinMoin import wikidicts
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
374 dicts = wikidicts.GroupDict(self)
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
375 dicts.load_dicts()
435ae662416d Moved from wrapped context-objects to class-changing context-objects
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
376 return dicts
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
377 dicts = EnvironProxy(dicts)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
378
4169
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
379 def reset(self):
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
380 self.current_lang = self.cfg.language_default
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
381 if hasattr(self, '_fmt_hd_counters'):
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
382 del self._fmt_hd_counters
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
383 if hasattr(self, 'uid_generator'):
1d5995a6a0e2 Moved unique-id-generation from request into own object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4168
diff changeset
384 del self.uid_generator
4170
02fcab41459f Factored out some of the loadTheme code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4169
diff changeset
385
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
386 def getPragma(self, key, defval=None):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
387 """ Query a pragma value (#pragma processing instruction)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
388
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
389 Keys are not case-sensitive.
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
390 """
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
391 return self.pragma.get(key.lower(), defval)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
392
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
393 def setPragma(self, key, value):
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
394 """ Set a pragma value (#pragma processing instruction)
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
395
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
396 Keys are not case-sensitive.
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
397 """
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
398 self.pragma[key.lower()] = value
4180
8f3914a83410 Further refinement to the Context-object model
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4174
diff changeset
399
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
400 class XMLRPCContext(HTTPContext, AuxilaryMixin):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
401 """ Context to act during a XMLRPC request. """
4184
b4acdce23cfa Group some functionality
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4182
diff changeset
402
4315
b75bbed31b67 Code review: regroup context classes and mixins in a different manner
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4311
diff changeset
403 class AllContext(HTTPContext, AuxilaryMixin):
4189
6246e8f813b7 General cleanups: doc strings, granularity of mixins
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4184
diff changeset
404 """ Catchall context to be able to quickly test old Moin code. """
4200
c21991eed9c9 Fixed: attribute lookup handling, in general as in uid_generator
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
405
4255
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
406 class ScriptContext(AllContext):
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
407 """ Context to act in scripting environments (e.g. former request_cli).
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
408
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
409 For input, sys.stdin is used as 'wsgi.input', output is written directly
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
410 to sys.stdout though.
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
411 """
4594
d706f5d4f4ec fix ScriptContext URL default value
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4587
diff changeset
412 def __init__(self, url=None, pagename=''):
d706f5d4f4ec fix ScriptContext URL default value
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4587
diff changeset
413 if url is None:
d706f5d4f4ec fix ScriptContext URL default value
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4587
diff changeset
414 url = 'http://localhost:0/' # just some somehow valid dummy URL
4587
ad5851792007 make 'make underlay' work
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4579
diff changeset
415 environ = create_environ(base_url=url) # XXX not sure about base_url, but makes "make underlay" work
4255
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
416 environ['HTTP_USER_AGENT'] = 'CLI/Script'
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
417 environ['wsgi.input'] = sys.stdin
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
418 request = Request(environ)
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
419 super(ScriptContext, self).__init__(request)
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
420 from MoinMoin import wsgiapp
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
421 wsgiapp.init(self)
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
422
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
423 def write(self, *data):
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
424 for d in data:
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
425 if isinstance(d, unicode):
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
426 d = d.encode(config.charset)
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
427 else:
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
428 d = str(d)
9d1817e8140b ScriptContext: first naive implementation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
429 sys.stdout.write(d)