widgets: Add question for poll widget in the message itself.

Use the command '/poll question?', to start a question.
This commit is contained in:
Rhea Parekh 2018-06-29 00:47:55 +05:30 committed by showell
parent 784e7249a5
commit b22d266667
2 changed files with 35 additions and 6 deletions

View File

@ -2,7 +2,7 @@ var voting_widget = (function () {
var exports = {};
var poll_data_holder = function (is_my_poll) {
var poll_data_holder = function (is_my_poll, question) {
// This object just holds data for a poll, although it
// works closely with the widget's concept of how data
// should be represented for rendering, plus how the
@ -10,7 +10,7 @@ var poll_data_holder = function (is_my_poll) {
var self = {};
var me = people.my_current_user_id();
var poll_question = '';
var poll_question = question;
var key_to_comment = {};
var my_idx = 1;
@ -141,9 +141,13 @@ var poll_data_holder = function (is_my_poll) {
exports.activate = function (opts) {
var elem = opts.elem;
var callback = opts.callback;
var question = '';
if (opts.extra_data) {
question = opts.extra_data.question;
}
var is_my_poll = people.is_my_user_id(opts.message.sender_id);
var poll_data = poll_data_holder(is_my_poll);
var poll_data = poll_data_holder(is_my_poll, question);
function render() {
var html = templates.render('poll-widget');

View File

@ -1,4 +1,4 @@
from typing import MutableMapping, Any
from typing import MutableMapping, Any, Optional, List, Tuple
from django.conf import settings
import re
@ -17,6 +17,32 @@ def do_widget_pre_save_actions(message: MutableMapping[str, Any]) -> None:
message['message'].content = 'We are running **1 server**.'
return
def get_widget_data(content: str) -> Tuple[Optional[str], Optional[str]]:
valid_widget_types = ['tictactoe', 'poll']
tokens = content.split(' ')
if not tokens:
return None, None
if tokens[0].startswith('/'):
widget_type = tokens[0][1:]
if widget_type in valid_widget_types:
extra_data = get_extra_data_from_widget_type(tokens, widget_type)
return widget_type, extra_data
return None, None
def get_extra_data_from_widget_type(tokens: List[str],
widget_type: Optional[str]) -> Any:
if widget_type == 'poll':
# This is used to extract the question from the poll command.
# The command '/poll question' will pre-set the question in the poll
question = ' '.join(tokens[1:])
if not question:
question = ''
extra_data = {'question': question}
return extra_data
return None
def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
'''
This is experimental code that only works with the
@ -30,9 +56,8 @@ def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
widget_type = None
extra_data = None
if content in ['/poll', '/tictactoe']:
widget_type = content[1:]
widget_type, extra_data = get_widget_data(content)
widget_content = message.get('widget_content')
if widget_content is not None:
# Note that we validate this data in check_message,