예제 #1
0
    def handle_inline_query(self):
        inline_query = self.update.inline_query

        text = inline_query.query.lower()

        uid = str(inline_query.from_user.id)
        query = Poll.query(Poll.admin_uid == uid, Poll.title_short >= text,
                           Poll.title_short < text + u'\ufffd')

        results = []
        polls = sorted(query.fetch(50),
                       key=lambda poll: poll.created,
                       reverse=True)
        for poll in polls:
            qr_id = str(poll.key.id())
            qr_title = poll.title
            qr_description = poll.generate_options_summary()
            content = {
                'message_text': poll.render_text(),
                'parse_mode': 'HTML'
            }
            reply_markup = poll.build_vote_buttons()
            result = {
                'type': 'article',
                'id': qr_id,
                'title': qr_title,
                'description': qr_description,
                'input_message_content': content,
                'reply_markup': reply_markup,
                'thumb_url': self.THUMB_URL
            }
            results.append(result)

        self.answer_inline_query(results)
예제 #2
0
    def get(self):
        try:
            cursor = Cursor.from_websafe_string(self.request.get('cursor'))
        except BadValueError:
            cursor = None

        try:
            limit = int(self.request.get('limit'))
            if limit <= 0:
                raise ValueError
        except (TypeError, ValueError):
            limit = 100

        query = Poll.query().order(-Poll.created)
        polls, next_cursor, has_more = query.fetch_page(limit,
                                                        start_cursor=cursor)

        for poll in polls:
            self.response.write(poll.render_html() + '\n\n<hr>\n\n')

        if not has_more:
            return

        more_url = '?cursor={}&limit={}'.format(
            next_cursor.to_websafe_string(), limit)
        self.response.write('<p><a href="{}">More</a></p>'.format(more_url))
예제 #3
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)
예제 #4
0
 def getAllPolls(self, opt_user):
     polls = Poll.query().fetch()
     pollsJsons = map(lambda poll: self.pollToJson(poll, opt_user), polls)
     self.response.out.write(json.dumps(pollsJsons))
예제 #5
0
 def getMostRecentPoll(self, opt_user):
     mostRecentPoll = Poll.query().order(-Poll.publishedOn).get()
     self.response.out.write(
         json.dumps(self.pollToJson(mostRecentPoll, opt_user)))