Ticket #807: pollmacro.patch

File pollmacro.patch, 5.3 kB (added by JamesMills, 2 years ago)
  • tracpoll/tracpoll.py

    old new  
    77from trac.util import sorted, escape 
    88from trac.wiki.formatter import wiki_to_oneliner 
    99from trac.web.chrome import ITemplateProvider, add_stylesheet 
     10from tractags.parseargs import parseargs 
    1011 
    1112class Poll(object): 
    12     def __init__(self, base_dir, title, vote_defs): 
     13    def __init__(self, base_dir, title, format, vote_defs): 
    1314        self.vote_defs = vote_defs 
    1415        self.title = title 
     16        self.format = format 
    1517        # Perhaps the Wiki page name should be included? 
    1618        self.key = ''.join(re.findall(r'(\w+)', title)).lower() 
    1719        self.store = os.path.join(base_dir, self.key + '.poll') 
     
    5658            out.write('<form id="%(id)s" method="get" action="%(href)s#%(id)s">\n' 
    5759                      '<input type="hidden" name="poll" value="%(id)s"/>\n' 
    5860                      % {'id': self.key, 'href': env.href(req.path_info)}) 
    59         out.write('<fieldset class="poll">\n' 
    60                   ' <legend>%s</legend>\n' 
    61                   ' <ul>\n' 
    62                   % escape(self.title)) 
     61        if self.format == 'singleline': 
     62            out.write('<fieldset class="poll">\n' 
     63                      ' <legend>%s</legend>\n' 
     64                      % escape(self.title)) 
     65        else: 
     66            out.write('<fieldset class="poll">\n' 
     67                      ' <legend>%s</legend>\n' 
     68                      ' <ul>\n' 
     69                      % escape(self.title)) 
    6370        username = req.authname or 'anonymous' 
    6471        for id, style, vote in self.vote_defs: 
    6572            hid = escape(str(id)) 
    66             out.write('<li%s>\n' % (style and ' class="%s"' % style or '')) 
     73            if self.format == "singleline": 
     74                out.write('%s&nbsp;' % (style and ' class="%s"' % style or '')) 
     75            else: 
     76                out.write('<li%s>\n' % (style and ' class="%s"' % style or '')) 
    6777            if can_vote: 
    6878                checked = username in self.votes[id] 
    6979                out.write('<input type="radio" name="vote" id="%(pvid)s" value="%(vid)s"%(checked)s/>\n' 
     
    7888                out.write(' <span class="voters">(<span class="voter">' + 
    7989                          '</span>, <span class="voter">'.join(self.votes[id]) + 
    8090                          '</span>)</span>') 
    81             out.write('</li>\n') 
     91            if self.format == "singleline": 
     92                pass 
     93            else: 
     94                out.write('</li>\n') 
    8295        can_vote and out.write('<input type="submit" value="Vote"/>') 
    83         out.write(' </ul>\n</fieldset>\n') 
     96        if self.format == "singleline": 
     97            out.write('\n</fieldset>\n') 
     98        else: 
     99            out.write(' </ul>\n</fieldset>\n') 
    84100        can_vote and out.write('</form>\n') 
    85101        return out.getvalue() 
    86102 
     
    115131    new-line (if used as a processor). The first argument is the title of the 
    116132    poll, which is also the identifier for each poll. 
    117133     
    118     Usage: `[[TicketPoll(<title>; <arg> [; <arg>] ...)]]` 
     134    Usage: `[[TicketPoll(format, <title>; <arg> [; <arg>] ...)]]` 
    119135 
     136    "format" can be one of: 
     137     * default 
     138     * singleline 
     139 
    120140    Where <arg> conforms to the following: 
    121141 
    122142        || '''<arg>'''         || '''Description''' || 
     
    134154    base_dir = property(lambda self: self.env.config.get('poll', 'base_dir', '/tmp')) 
    135155 
    136156    def render_macro(self, req, name, content): 
     157        args, kwargs = self._split_macro_args(content) 
     158        self.env.log.debug("args: %s" % args) 
     159        self.env.log.debug("kwargs: %s" % args) 
     160        try: 
     161            format = kwargs['format'] 
     162            content = re.sub("format ?= ?%s, ?(?imus)" % format, "", content) 
     163        except KeyError: 
     164            format = 'default' 
     165 
    137166        content = filter(None, [i.strip() for i in 
    138167                                content.replace(';', '\n').split('\n')]) 
     168        self.env.log.debug("content: %s" % content) 
    139169        title = content.pop(0) 
    140         return self.render_poll(req, title, content) 
     170        return self.render_poll(req, title, format, content) 
    141171 
    142     def render_poll(self, req, title, votes): 
     172    def _split_macro_args(self, argv): 
     173        """Return a list of arguments and a dictionary of keyword arguments 
     174        """ 
     175        args = [] 
     176        kwargs = {} 
     177        if argv: 
     178            args, kwargs = parseargs(argv) 
     179        return args, kwargs 
     180 
     181    def render_poll(self, req, title, format, votes): 
    143182        from trac.ticket.model import Ticket, Priority 
    144183        from trac.ticket.query import Query 
    145184        add_stylesheet(req, 'poll/css/poll.css') 
     
    183222        if not all_votes: 
    184223            raise TracError('No votes provided') 
    185224 
    186         poll = Poll(self.base_dir, title, all_votes) 
     225        poll = Poll(self.base_dir, title, format, all_votes) 
    187226        if req.perm.has_permission('POLL_VOTE'): 
    188227            poll.populate(req) 
    189228        return poll.render(self.env, req) 
  • setup.py

    old new  
    11from setuptools import setup 
    22 
    33setup(name='TracPoll', 
    4       version='0.1', 
     4      version='0.2', 
    55      packages=['tracpoll'], 
    66      entry_points = {'trac.plugins': ['tracpoll = tracpoll']}, 
    77      author='Alec Thomas',