paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Twisted classes. |
paulb@1 | 5 | """ |
paulb@1 | 6 | |
paulb@1 | 7 | import Generic |
paulb@1 | 8 | |
paulb@1 | 9 | class Transaction(Generic.Transaction): |
paulb@1 | 10 | |
paulb@1 | 11 | """ |
paulb@1 | 12 | Twisted transaction interface. |
paulb@1 | 13 | """ |
paulb@1 | 14 | |
paulb@1 | 15 | def __init__(self, trans): |
paulb@1 | 16 | |
paulb@1 | 17 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 18 | |
paulb@1 | 19 | self.trans = trans |
paulb@1 | 20 | |
paulb@1 | 21 | # Request-related methods. |
paulb@1 | 22 | |
paulb@1 | 23 | def get_request_stream(self): |
paulb@1 | 24 | |
paulb@1 | 25 | """ |
paulb@1 | 26 | A framework-specific method which returns the request stream for |
paulb@1 | 27 | the transaction. |
paulb@1 | 28 | """ |
paulb@1 | 29 | |
paulb@5 | 30 | return self.trans.content |
paulb@1 | 31 | |
paulb@1 | 32 | def get_request_method(self): |
paulb@1 | 33 | |
paulb@1 | 34 | """ |
paulb@1 | 35 | A framework-specific method which gets the request method. |
paulb@1 | 36 | """ |
paulb@1 | 37 | |
paulb@1 | 38 | return self.trans.method |
paulb@1 | 39 | |
paulb@1 | 40 | def get_headers(self): |
paulb@1 | 41 | |
paulb@1 | 42 | """ |
paulb@1 | 43 | A framework-specific method which returns the request headers. |
paulb@1 | 44 | NOTE: Experimental, since framework support varies somewhat. |
paulb@1 | 45 | """ |
paulb@1 | 46 | |
paulb@1 | 47 | # NOTE: Accessing attribute of transaction object. |
paulb@1 | 48 | |
paulb@1 | 49 | return self.trans.received_headers |
paulb@1 | 50 | |
paulb@1 | 51 | def get_content_type(self): |
paulb@1 | 52 | |
paulb@1 | 53 | """ |
paulb@1 | 54 | A framework-specific method which gets the content type specified on the |
paulb@1 | 55 | request, along with the charset employed. |
paulb@1 | 56 | """ |
paulb@1 | 57 | |
paulb@1 | 58 | return self.parse_content_type(self.trans.getHeader("Content-Type")) |
paulb@1 | 59 | |
paulb@1 | 60 | def get_content_charsets(self): |
paulb@1 | 61 | |
paulb@1 | 62 | """ |
paulb@1 | 63 | Returns the character set preferences. |
paulb@1 | 64 | """ |
paulb@1 | 65 | |
paulb@1 | 66 | return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) |
paulb@1 | 67 | |
paulb@1 | 68 | def get_content_languages(self): |
paulb@1 | 69 | |
paulb@1 | 70 | """ |
paulb@1 | 71 | A framework-specific method which extracts language information from |
paulb@1 | 72 | the transaction. |
paulb@1 | 73 | """ |
paulb@1 | 74 | |
paulb@1 | 75 | return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) |
paulb@1 | 76 | |
paulb@5 | 77 | def get_path(self): |
paulb@5 | 78 | |
paulb@5 | 79 | """ |
paulb@5 | 80 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 81 | """ |
paulb@5 | 82 | |
paulb@5 | 83 | # NOTE: The path and "path info" are mostly equivalent for Twisted. |
paulb@5 | 84 | |
paulb@10 | 85 | return self.get_path_info() |
paulb@5 | 86 | |
paulb@1 | 87 | def get_path_info(self): |
paulb@1 | 88 | |
paulb@1 | 89 | """ |
paulb@1 | 90 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 91 | URL after the resource name handling the current request) from the |
paulb@1 | 92 | request. |
paulb@1 | 93 | """ |
paulb@1 | 94 | |
paulb@10 | 95 | return "/%s" % "/".join(self.trans.postpath) |
paulb@1 | 96 | |
paulb@1 | 97 | # Higher level request-related methods. |
paulb@1 | 98 | |
paulb@1 | 99 | def get_fields(self): |
paulb@1 | 100 | |
paulb@1 | 101 | """ |
paulb@1 | 102 | A framework-specific method which extracts the form fields from the |
paulb@1 | 103 | transaction. |
paulb@1 | 104 | """ |
paulb@1 | 105 | |
paulb@1 | 106 | # NOTE: Discard multiple field values. |
paulb@1 | 107 | |
paulb@1 | 108 | return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) |
paulb@1 | 109 | |
paulb@1 | 110 | def get_agent_information(self): |
paulb@1 | 111 | |
paulb@1 | 112 | """ |
paulb@1 | 113 | A framework-specific method which extracts agent information from |
paulb@1 | 114 | the transaction. |
paulb@1 | 115 | """ |
paulb@1 | 116 | |
paulb@1 | 117 | return None |
paulb@1 | 118 | |
paulb@1 | 119 | # Response-related methods. |
paulb@1 | 120 | |
paulb@1 | 121 | def get_response_stream(self): |
paulb@1 | 122 | |
paulb@1 | 123 | """ |
paulb@1 | 124 | A framework-specific method which returns the response stream for |
paulb@1 | 125 | the transaction. |
paulb@1 | 126 | """ |
paulb@1 | 127 | |
paulb@5 | 128 | return self.trans |
paulb@1 | 129 | |
paulb@1 | 130 | def set_content_type(self, content_type): |
paulb@1 | 131 | |
paulb@1 | 132 | """ |
paulb@1 | 133 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 134 | response. |
paulb@1 | 135 | """ |
paulb@1 | 136 | |
paulb@5 | 137 | self.trans.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 138 | |
paulb@1 | 139 | # vim: tabstop=4 expandtab shiftwidth=4 |