Exemplo n.º 1
0
 def get(self, pid):
     try:
         pid = int(pid)
         poll = Poll.get_by_id(pid)
         if not poll:
             raise ValueError
     except ValueError:
         self.response.set_status(404)
         self.response.write('Invalid poll ID')
         return
     self.response.write(poll.render_html())
Exemplo n.º 2
0
    def handle_message(self):
        message = self.update.message

        User.populate_by_id(message.from_user.id,
                            first_name=message.from_user.first_name,
                            last_name=message.from_user.last_name,
                            username=message.from_user.username)

        if not message.text:
            return

        text = message.text
        uid = str(message.chat.id)
        responding_to = memcache.get(uid)

        def deliver_poll(poll):
            backend.send_message(0.5,
                                 chat_id=uid,
                                 text=poll.render_text(),
                                 parse_mode='HTML',
                                 reply_markup=poll.build_admin_buttons())

        if text.startswith('/start'):
            backend.send_message(chat_id=uid, text=self.NEW_POLL)
            memcache.set(uid, value='START', time=3600)
            return

        elif text == '/done' and responding_to and responding_to.startswith(
                'OPT '):
            poll = Poll.get_by_id(int(responding_to[4:]))
            if not poll.options:
                backend.send_message(chat_id=uid,
                                     text=self.ERROR_PREMATURE_DONE)
                return
            backend.send_message(chat_id=uid, text=self.DONE)
            deliver_poll(poll)

        elif text == '/polls':
            header = [util.make_html_bold('Your polls')]

            recent_polls = Poll.query(
                Poll.admin_uid == uid).order(-Poll.created).fetch(50)
            body = [
                u'{}. {}'.format(i + 1, poll.generate_poll_summary_with_link())
                for i, poll in enumerate(recent_polls)
            ]

            footer = ['Use /start to create a new poll.']

            output = u'\n\n'.join(header + body + footer)

            backend.send_message(chat_id=uid, text=output, parse_mode='HTML')

        elif text.startswith('/view_'):
            try:
                poll = Poll.get_by_id(int(text[6:]))
                if not poll or poll.admin_uid != uid:
                    raise ValueError
                deliver_poll(poll)
            except ValueError:
                backend.send_message(chat_id=uid, text=self.HELP)

        elif responding_to == 'START':
            new_poll_key = Poll.new(admin_uid=uid, title=text).put()
            bold_title = util.make_html_bold_first_line(text)
            backend.send_message(chat_id=uid,
                                 text=self.FIRST_OPTION.format(bold_title),
                                 parse_mode='HTML')
            memcache.set(uid,
                         value='OPT {}'.format(new_poll_key.id()),
                         time=3600)
            return

        elif responding_to and responding_to.startswith('OPT '):
            poll = Poll.get_by_id(int(responding_to[4:]))
            poll.options.append(Option(text))
            poll.put()
            if len(poll.options) < 10:
                backend.send_message(chat_id=uid, text=self.NEXT_OPTION)
                return
            backend.send_message(chat_id=uid, text=self.DONE)
            deliver_poll(poll)

        else:
            backend.send_message(chat_id=uid, text=self.HELP)

        memcache.delete(uid)
Exemplo n.º 3
0
    def handle_callback_query(self):
        callback_query = self.update.callback_query

        extract_user_data = lambda user: (user.id, {
            'first_name': user.first_name,
            'last_name': user.last_name,
            'username': user.username
        })
        uid, user_profile = extract_user_data(callback_query.from_user)

        Respondent.populate_by_id(uid, **user_profile)

        imid = callback_query.inline_message_id
        is_admin = not imid
        chat_id = callback_query.message.chat.id if is_admin else None
        mid = callback_query.message.message_id if is_admin else None

        try:
            params = callback_query.data.split()
            poll_id = int(params[0])
            action = params[1]
        except (AttributeError, IndexError, ValueError):
            logging.warning('Invalid callback query data')
            self.answer_callback_query(
                'Invalid data. This attempt will be logged!')
            return

        poll = Poll.get_by_id(poll_id)
        if not poll:
            backend.api_call('edit_message_reply_markup',
                             inline_message_id=imid,
                             chat_id=chat_id,
                             message_id=mid)
            self.answer_callback_query('Sorry, this poll has been deleted')
            return

        if action.isdigit():
            poll, status = Poll.toggle(poll_id, int(action), uid, user_profile)
            backend.api_call(
                'edit_message_text',
                inline_message_id=imid,
                chat_id=chat_id,
                message_id=mid,
                text=poll.render_text(),
                parse_mode='HTML',
                reply_markup=poll.build_vote_buttons(admin=is_admin))

        elif action == 'refresh' and is_admin:
            status = 'Results updated!'
            backend.api_call('edit_message_text',
                             chat_id=chat_id,
                             message_id=mid,
                             text=poll.render_text(),
                             parse_mode='HTML',
                             reply_markup=poll.build_admin_buttons())

        elif action == 'vote' and is_admin:
            status = 'You may now vote!'
            backend.api_call('edit_message_reply_markup',
                             chat_id=chat_id,
                             message_id=mid,
                             reply_markup=poll.build_vote_buttons(admin=True))

        elif action == 'delete' and is_admin:
            poll.key.delete()
            status = 'Poll deleted!'
            backend.api_call('edit_message_reply_markup',
                             chat_id=chat_id,
                             message_id=mid)

        elif action == 'back' and is_admin:
            status = ''
            backend.api_call('edit_message_reply_markup',
                             chat_id=chat_id,
                             message_id=mid,
                             reply_markup=poll.build_admin_buttons())

        else:
            status = 'Invalid data. This attempt will be logged!'
            logging.warning('Invalid callback query data')

        self.answer_callback_query(status)