1 #!/usr/bin/env python 2 3 """ 4 mod_python classes. 5 """ 6 7 import Generic 8 from mod_python.util import FieldStorage 9 from mod_python import apache 10 11 class Transaction(Generic.Transaction): 12 13 """ 14 mod_python transaction interface. 15 """ 16 17 def __init__(self, trans): 18 19 "Initialise the transaction using the mod_python transaction 'trans'." 20 21 self.trans = trans 22 self.response_code = apache.OK 23 24 # Request-related methods. 25 26 def get_request_stream(self): 27 28 """ 29 A framework-specific method which returns the request stream for 30 the transaction. 31 """ 32 33 return self.trans 34 35 def get_request_method(self): 36 37 """ 38 A framework-specific method which gets the request method. 39 """ 40 41 return self.trans.method 42 43 def get_headers(self): 44 45 """ 46 A framework-specific method which returns the request headers. 47 NOTE: Experimental, since framework support varies somewhat. 48 """ 49 50 return self.trans.headers_in 51 52 def get_content_type(self): 53 54 """ 55 A framework-specific method which gets the content type specified on the 56 request, along with the charset employed. 57 """ 58 59 return self.parse_content_type(self.trans.content_type) 60 61 def get_content_charsets(self): 62 63 """ 64 Returns the character set preferences. 65 """ 66 67 return self.parse_content_preferences(self.trans.headers_in["Accept-Charset"]) 68 69 def get_content_languages(self): 70 71 """ 72 A framework-specific method which extracts language information from 73 the transaction. 74 """ 75 76 return self.parse_content_preferences(self.trans.headers_in["Accept-Language"]) 77 78 def get_path(self): 79 80 """ 81 A framework-specific method which gets the entire path from the request. 82 """ 83 84 return self.trans.uri 85 86 def get_path_info(self): 87 88 """ 89 A framework-specific method which gets the "path info" (the part of the 90 URL after the resource name handling the current request) from the 91 request. 92 """ 93 94 return self.trans.path_info 95 96 # Higher level request-related methods. 97 98 def get_fields(self): 99 100 """ 101 A framework-specific method which extracts the form fields from the 102 transaction. 103 """ 104 105 return FieldStorage(self.trans, keep_blank_values=1) 106 107 def get_user(self): 108 109 """ 110 A framework-specific method which extracts user information from the 111 transaction. 112 """ 113 114 return self.trans.user 115 116 # Response-related methods. 117 118 def get_response_stream(self): 119 120 """ 121 A framework-specific method which returns the response stream for 122 the transaction. 123 """ 124 125 return self.trans 126 127 def get_response_code(self): 128 129 """ 130 Get the response code associated with the transaction. If no response 131 code is defined, None is returned. 132 """ 133 134 return self.response_code 135 136 def set_response_code(self, response_code): 137 138 """ 139 Set the 'response_code' using a numeric constant defined in the HTTP 140 specification. 141 """ 142 143 self.response_code = response_code 144 145 def set_content_type(self, content_type): 146 147 """ 148 A framework-specific method which sets the 'content_type' for the 149 response. 150 """ 151 152 self.trans.content_type = self.format_content_type(content_type) 153 154 # vim: tabstop=4 expandtab shiftwidth=4