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