Changeset 7820
- Timestamp:
- 07/01/08 23:34:05 (3 months ago)
- Files:
-
- django/trunk/django/test/client.py (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/test/client.py
r7814 r7820 6 6 except ImportError: 7 7 from StringIO import StringIO 8 8 9 from django.conf import settings 9 10 from django.contrib.auth import authenticate, login … … 23 24 MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY 24 25 26 25 27 class FakePayload(object): 26 28 """ … … 42 44 return content 43 45 46 44 47 class ClientHandler(BaseHandler): 45 48 """ … … 62 65 response = self.get_response(request) 63 66 64 # Apply response middleware 67 # Apply response middleware. 65 68 for middleware_method in self._response_middleware: 66 69 response = middleware_method(request, response) … … 72 75 73 76 def store_rendered_templates(store, signal, sender, template, context): 74 "A utility function for storing templates and contexts that are rendered" 77 """ 78 Stores templates and contexts that are rendered. 79 """ 75 80 store.setdefault('template',[]).append(template) 76 81 store.setdefault('context',[]).append(context) … … 78 83 def encode_multipart(boundary, data): 79 84 """ 80 A simple method for encoding multipart POST data from a dictionary of 81 form values. 85 Encodes multipart POST data from a dictionary of form values. 82 86 83 87 The key will be used as the form data name; the value will be transmitted … … 91 95 lines.extend([ 92 96 '--' + boundary, 93 'Content-Disposition: form-data; name="%s"; filename="%s"' % (to_str(key), to_str(os.path.basename(value.name))), 97 'Content-Disposition: form-data; name="%s"; filename="%s"' \ 98 % (to_str(key), to_str(os.path.basename(value.name))), 94 99 'Content-Type: application/octet-stream', 95 100 '', … … 145 150 def store_exc_info(self, *args, **kwargs): 146 151 """ 147 Utility method that can be used to store exceptions when they are 148 generated by a view. 152 Stores exceptions when they are generated by a view. 149 153 """ 150 154 self.exc_info = sys.exc_info() 151 155 152 156 def _session(self): 153 "Obtain the current session variables" 157 """ 158 Obtains the current session variables. 159 """ 154 160 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 155 161 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) … … 167 173 using the arguments to the request. 168 174 """ 169 170 175 environ = { 171 176 'HTTP_COOKIE': self.cookies, … … 181 186 environ.update(request) 182 187 183 # Curry a data dictionary into an instance of 184 # the template renderer callback function188 # Curry a data dictionary into an instance of the template renderer 189 # callback function. 185 190 data = {} 186 191 on_template_render = curry(store_rendered_templates, data) 187 192 dispatcher.connect(on_template_render, signal=signals.template_rendered) 188 193 189 # Capture exceptions created by the handler 194 # Capture exceptions created by the handler. 190 195 dispatcher.connect(self.store_exc_info, signal=got_request_exception) 191 196 … … 210 215 self.exc_info = None 211 216 raise exc_info[1], None, exc_info[2] 212 213 # Save the client and request that stimulated the response 217 218 # Save the client and request that stimulated the response. 214 219 response.client = self 215 220 response.request = request 216 221 217 # Add any rendered template detail to the response 222 # Add any rendered template detail to the response. 218 223 # If there was only one template rendered (the most likely case), 219 # flatten the list to a single element 224 # flatten the list to a single element. 220 225 for detail in ('template', 'context'): 221 226 if data.get(detail): … … 227 232 setattr(response, detail, None) 228 233 229 # Update persistent cookie data 234 # Update persistent cookie data. 230 235 if response.cookies: 231 236 self.cookies.update(response.cookies) … … 234 239 235 240 def get(self, path, data={}, **extra): 236 "Request a response from the server using GET." 241 """ 242 Requests a response from the server using GET. 243 """ 237 244 r = { 238 245 'CONTENT_LENGTH': None, … … 247 254 248 255 def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): 249 "Request a response from the server using POST." 250 256 """ 257 Requests a response from the server using POST. 258 """ 251 259 if content_type is MULTIPART_CONTENT: 252 260 post_data = encode_multipart(BOUNDARY, data) … … 266 274 267 275 def login(self, **credentials): 268 """Set the Client to appear as if it has sucessfully logged into a site. 276 """ 277 Sets the Client to appear as if it has successfully logged into a site. 269 278 270 279 Returns True if login is possible; False if the provided credentials … … 273 282 """ 274 283 user = authenticate(**credentials) 275 if user and user.is_active and 'django.contrib.sessions' in settings.INSTALLED_APPS: 284 if user and user.is_active \ 285 and 'django.contrib.sessions' in settings.INSTALLED_APPS: 276 286 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) 277 287 278 # Create a fake request to store login details 288 # Create a fake request to store login details. 279 289 request = HttpRequest() 280 290 request.session = engine.SessionStore() 281 291 login(request, user) 282 292 283 # Set the cookie to represent the session 293 # Set the cookie to represent the session. 284 294 self.cookies[settings.SESSION_COOKIE_NAME] = request.session.session_key 285 295 self.cookies[settings.SESSION_COOKIE_NAME]['max-age'] = None … … 289 299 self.cookies[settings.SESSION_COOKIE_NAME]['expires'] = None 290 300 291 # Save the session values 301 # Save the session values. 292 302 request.session.save() 293 303 … … 297 307 298 308 def logout(self): 299 """Removes the authenticated user's cookies. 309 """ 310 Removes the authenticated user's cookies. 300 311 301 312 Causes the authenticated user to be logged out.
