On Thu, Aug 7, 2014 at 6:06 PM, Tian Hongjie <[email protected]>
wrote:

>  Hi,
>
> I see some redirect have been written in function, such as ticket _do_save
> function:
>
> def _do_save(self, req, ticket, action):
>         # Save the action controllers we need to call side-effects for before
>         # we save the changes to the ticket.
>         controllers = list(self._get_action_controllers(req, ticket, action))
>
>         # -- Save changes
>
>         fragment = ''
>         now = datetime.now(utc)
>         cnum = ticket.save_changes(get_reporter_id(req, 'author'),
>                                    req.args.get('comment'), when=now,
>                                    replyto=req.args.get('replyto'))
>         if cnum:
>             fragment = '#comment:%d' % cnum
>             try:
>                 tn = TicketNotifyEmail(self.env)
>                 tn.notify(ticket, newticket=False, modtime=now)
>             except Exception, e:
>                 self.log.error("Failure sending notification on change to "
>                         "ticket #%s: %s", ticket.id, exception_to_unicode(e))
>                 # TRANSLATOR: The 'change' has been saved... (link)
>                 change = _('change')
>                 if fragment:
>                     change = tag.a(change, href=fragment)
>                 add_warning(req, tag_("The %(change)s has been saved, but an "
>                                       "error occurred while sending "
>                                       "notifications: %(message)s",
>                                       change=change, message=to_unicode(e)))
>                 fragment = ''
>
>         # After saving the changes, apply the side-effects.
>         for controller in controllers:
>             self.log.debug("Side effect for %s" %
>                            controller.__class__.__name__)
>             controller.apply_action_side_effects(req, ticket, action)
>
>         *req.redirect(req.href.ticket(ticket.id <http://ticket.id>) + 
> fragment)*
>
> My question is could i redirect to home page writing some handler not
> rewrite this function only modify redirect which with underline?
>
> Thanks,
> Charlie
>


The following single-file plugin will redirect to the default handler when
//Submit changes// is pressed on the ticket page. It will not redirect on
ticket creation, comment edit and comment deletion.

from trac.core import Component, implements
from trac.ticket.web_ui import TicketModule
from trac.web.api import IRequestFilter


class RedirectOnTicketSave(Component):

    implements(IRequestFilter)

    def pre_process_request(self, req, handler):
        if handler is TicketModule(self.env) \
                and req.method == 'POST' and 'submit' in req.args \
                and req.args['submit'] == 'Submit changes' \
                and not req.args.get('preview'):
            req.add_redirect_listener(ticket_save_listener)
        return handler

    def post_process_request(self, req, template, data, content_type):
        return template, data, content_type


def ticket_save_listener(req, url, permanent):
    if ticket_save_listener in req.redirect_listeners:
        req.redirect_listeners.remove(ticket_save_listener)
    req.redirect(req.href())


If I misunderstood your need, and instead you want to redirect on ticket
creation, you'll need to modify the conditions in pre_process_request. The
change req.args['submit'] == 'Submit changes'  -> req.args['submit'] ==
'Create ticket' should be sufficient.  Additionally, the code below adds a
notice to indicate that the ticket has been created.

from trac.core import Component, implements
from trac.ticket.web_ui import TicketModule
from trac.util.translation import _
from trac.web.api import IRequestFilter
from trac.web.chrome import add_notice


class RedirectOnTicketSave(Component):

    implements(IRequestFilter)

    def pre_process_request(self, req, handler):
        if handler is TicketModule(self.env) \
                and req.method == 'POST' and 'submit' in req.args \
                and req.args['submit'] == 'Create ticket' \
                and not req.args.get('preview'):
            req.add_redirect_listener(ticket_save_listener)
        return handler

    def post_process_request(self, req, template, data, content_type):
        return template, data, content_type


def ticket_save_listener(req, url, permanent):
    if ticket_save_listener in req.redirect_listeners:
        req.redirect_listeners.remove(ticket_save_listener)
    id = url.split('/')[-1]
    add_notice(req, _("Ticket #%(id)s has been created.", id=id))
    req.redirect(req.href())

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/trac-dev.
For more options, visit https://groups.google.com/d/optout.

Reply via email to