root/trac_captcha/ticket.py

Revision 16:bb9f1fb34a32, 2.3 KB (checked in by fs, 19 months ago)

moved code for captcha injection in the controller so it can be shared

Line 
1# -*- coding: UTF-8 -*-
2#
3# The MIT License
4#
5# Copyright (c) 2010 Felix Schwarz <felix.schwarz@oss.schwarz.eu>
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24
25from genshi.filters.transform import Transformer
26from trac.core import Component, implements
27from trac.ticket.api import ITicketManipulator
28from trac.web.api import ITemplateStreamFilter
29
30from trac_captcha.controller import TracCaptchaController
31
32__all__ = ['TicketCaptcha']
33
34
35class TicketCaptcha(Component):
36    implements(ITemplateStreamFilter, ITicketManipulator)
37   
38    # --- ITemplateStreamFilter ------------------------------------------------
39    def filter_stream(self, req, method, filename, stream, data):
40        if filename != 'ticket.html':
41            return stream
42        transformer = Transformer('//div[@class="buttons"]')
43        return TracCaptchaController(self.env).inject_captcha_into_stream(req, stream, transformer)
44   
45    # --- ITicketManipulator ---------------------------------------------------
46    def prepare_ticket(self, req, ticket, fields, actions):
47        pass
48   
49    def validate_ticket(self, req, ticket):
50        error_message = TracCaptchaController(self.env).check_captcha_solution(req)
51        if error_message is None:
52            return ()
53        return ((None, error_message),)
54   
55
56
Note: See TracBrowser for help on using the browser.