示例#1
0
 def __init__(self):
     self.client = zulip.Client(site="https://fazeup.zulipchat.com/api/")
     self.subscribe_all()
     self.hacknews = Hackernews()
     self.trans = Translate()
     self.movie = Movie()
     self.lyrics = Lyrics()
     self.holiday = Holiday()
     self.currency = Currency()
     self.cricket = Cricket()
     # self.chatbot.train("chatterbot.corpus.english")
     self.crypto = Crypto()
     self.trans = Translate()
     self.g = Giphy()
     self.w = WikiPedia()
     # self.tw = Twimega()
     # self.motivate = Motivate()
     self.shortenedurl = Urlshortener()
     self.geo = Geocode()
     self.weather = Weather()
     self.dict_ = Dictionary()
     self.joke = Joke()
     self.pnr = Pnr()
     self.mustread = Mustread()
     self.ss = Ss()
     self.cricket = Cricket()
     self.poll = Poll()
     print("done init")
     self.subkeys = [
         "crypto", "translate", "define", "joke", "weather", "giphy", "pnr",
         "mustread", "poll", "hackernews", "hn", "HN", "motivate",
         "twitter", "screenshot", "memo", "cricnews", "help", "shorturl",
         "movie", "currency", "holiday", "lyrics"
     ]
示例#2
0
    async def get_config_from_user(self, ctx, poll: Poll, new_poll=False):
        # protected, number of choices per vote, max votes per poll
        await ctx.send(
            f'Thanks for making a poll! Do you want this poll to be private? **({random.choice(YES)}/{random.choice(NO)})**\n'
            +
            'This means **only you** can see the results, but everyone can still vote.'
        )

        def check(msg):
            return msg.author.id == ctx.author.id and msg.channel == ctx.channel

        r = await self.bot.wait_for('message', check=check)
        while True:
            if r.content.lower() in YES:
                poll.protected = True
                break
            elif r.content.lower() in NO:
                poll.protected = False
                break
            else:
                await ctx.send(
                    f'I didn\'t quite catch that. Respond with **{random.choice(YES)}** or **{random.choice(NO)}**.'
                )
                r = await self.bot.wait_for('message', check=check)
        print(f'set poll {poll.title} to protected={poll.protected}')
示例#3
0
def handler(event, contest):
    logger.info("Start!")

    executor = ThreadPoolExecutor(max_workers=1000)
    main_loop = asyncio.new_event_loop()
    main_loop.set_default_executor(executor)
    asyncio.set_event_loop(main_loop)

    poll = Poll(main_loop)
    cal = poll.cal
    update_and_delete = UpdateAndDelete(main_loop, executor)

    table = event['table']
    queue_url = event['queueUrl']
    message_count = event['messageCount']

    poll.messages(sqs, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))

    update_and_delete.execute(sqs_client, db, queue_url, table, cal.stats)

    logger.info("Update API count: {}".format(update_and_delete.update_count))
    logger.info("Delete API count: {}".format(update_and_delete.delete_count))
    logger.info("Delete Message count: {}".format(
        update_and_delete.deleted_message_count))

    main_loop.close()
    executor.shutdown()

    return "Lambda job finished successfully."
示例#4
0
文件: tap.py 项目: fuzzpedal/chatapp
 def handle_request(self, request):
     args = {}
     for k, v in request.args.items():
         args.update({k: v[0]},)
     
     if len(request.postpath[0]) and not args.get('callback'):
         request.setResponseCode(400)
         return HTML
     
     if 'register' in request.postpath:
         register = Register(request, args)
         return register.response()
     
     if 'login' in request.postpath:
         login = Login(request, args)
         return login.response()
     
     if 'poll' in request.postpath:
         poll = Poll(request, args)
         return poll.response()
     
     if 'message' in request.postpath:
         message = Message(request, args)
         return message.response()
     
     if 'join' in request.postpath:
         join = Join(request, args)
         return join.response()
     
     else:
         return """
示例#5
0
def handler(event, contest):
    logger.info("Start!")

    executor = ThreadPoolExecutor(max_workers=1000)
    main_loop = asyncio.new_event_loop()
    main_loop.set_default_executor(executor)
    asyncio.set_event_loop(main_loop)

    poll = Poll(main_loop)
    cal = poll.cal
    update_and_delete = UpdateAndDelete(main_loop, executor)

    table = event['table']
    queue_url = event['queueUrl']
    message_count = event['messageCount']

    poll.messages(sqs, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))

    update_and_delete.execute(sqs_client, db, queue_url, table, cal.stats)

    logger.info("Update API count: {}".format(update_and_delete.update_count))
    logger.info("Delete API count: {}".format(update_and_delete.delete_count))
    logger.info("Delete Message count: {}".format(
        update_and_delete.deleted_message_count))

    main_loop.close()
    executor.shutdown()

    return "Lambda job finished successfully."
示例#6
0
文件: user.py 项目: TheTwon/iOS_vote
	def getUserPoll(self, pollId, formated=True):
		p = Poll(pollId)

		if formated:
			return p.formated()

		return p
示例#7
0
文件: user.py 项目: TheTwon/iOS_vote
	def hasAnsweredPoll(self, pollId):
		p = Poll(pollId)

		if p.pollExists(pollId):
			return not p.hasUserAnswer(self.getUserId())

		return False
示例#8
0
 def test_add_option(self):
     poll = Poll("test", "fake id")
     poll.add_option(None, ["option #1", "This is the first option"])
     self.assertEqual(
         str(poll), r"test{'option #1': {'id': 'option #1', 'description'"
         r": 'This is the first option', 'voters': [], 'index'"
         r": 0}}{}fake id")
def ExtractPollsFromTable(table, riding_number, riding_name):
    polls = []
    rows = table.find_all('tr')
    date_column, sample_size_column, party_columns = FindColumnHeaders(rows[0])
    for row in rows[1:]:
        columns = row.find_all('td')
        date_string = columns[date_column].find('span', '').get_text()
        parsed_date = datetime.datetime.strptime(date_string, '%B %d, %Y')
        sample_size_string = columns[sample_size_column].get_text().strip()
        if sample_size_string:
            sample_size = float(sample_size_string.replace(',', ''))
        else:
            sample_size = 0
        poll = Poll(parsed_date, sample_size, riding_number, riding_name)
        for party_name, party_index in party_columns.items():
            if party_index >= 0:
                number_string = columns[party_index].get_text()
                try:
                    support = float(number_string) / 100
                except:
                    support = 0
                party_code = party_names[party_name]
                poll.party_support[party_code] = support
        polls.append(poll)
    return polls
示例#10
0
 def __init__(self):
     self.client = zulip.Client(site="https://chunkzz.zulipchat.com/api/")
     self.subscribe_all()
     self.chatbot = ChatBot(
         "Omega", trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
     self.chatbot.train("chatterbot.corpus.english")
     self.crypto = Crypto()
     self.trans = Translate()
     self.g = Giphy()
     self.w = WikiPedia()
     self.tw = Twimega()
     self.motivate = Motivate()
     self.shortenedurl = Urlshortener()
     self.hacknews = Hackernews()
     self.geo = Geocode()
     self.weather = Weather()
     self.dict_ = Dictionary()
     self.joke = Joke()
     self.pnr = Pnr()
     self.mustread = Mustread()
     self.ss = Ss()
     self.cricket = Cricket()
     self.poll = Poll()
     self.subkeys = [
         "crypto", "translate", "define", "joke", "weather", "giphy", "pnr",
         "mustread", "poll", "hackernews", "hn", "HN", "motivate",
         "twitter", "screenshot", "memo", "cricnews", "help", "shorturl"
     ]
示例#11
0
    def __init__(self, curr_directory, output_directory, max_file_size,
                 plugins_file, es_url):
        """Initializes the Efetch Helper"""
        # Setup directory references
        self.curr_dir = curr_directory
        self.resource_dir = self.curr_dir + os.path.sep + u'resources' + os.path.sep
        self.icon_dir = self.resource_dir + u'icons' + os.path.sep
        self.output_dir = output_directory
        self.max_file_size = max_file_size
        if not os.path.isdir(self.icon_dir):
            logging.error(u'Could not find icon directory ' + self.icon_dir)

        self.pathspec_helper = PathspecHelper(output_directory, max_file_size)

        # Create plugin manager and begin polling for changes to plugins
        self.plugin_manager = EfetchPluginManager(plugins_file, self.curr_dir)
        self.poll = Poll(self.plugin_manager)
        self.poll.start()

        self.standard_office_2007_extensions = [
            'xlsx', 'docx', 'pptx', 'dotx', 'docm', 'doct', 'xlsm', 'xltx',
            'xltm', 'pptx', 'pptm', 'potx', 'ppam', 'ppsx', 'ppsm', 'sldx',
            'sldm'
        ]

        # Elastic Search DB setup
        if es_url:
            self.db_util = DBUtil()
        else:
            self.db_util = DBUtil(es_url)
示例#12
0
def handler(event, contest):
    logger.info("Start!")

    main_loop = IOLoop.instance()

    poll = Poll(main_loop)
    cal = poll.cal
    delete = Delete()
    update_and_delete = UpdateAndDelete(main_loop, delete)

    table = event['table']
    queue_url = event['queueUrl']
    message_count = event['messageCount']

    poll.messages(sqs_receive_message, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))

    update_and_delete.execute(sqs_delete_message_batch, db_update_item,
                              queue_url, table, cal.stats)

    logger.info("Update API count: {}".format(update_and_delete.update_count))
    logger.info("Delete API count: {}".format(delete.delete_count))
    logger.info("Delete Message count: {}".format(delete.message_count))

    return "Lambda job finished successfully."
示例#13
0
def prepare_songs_for_poll(client: WebClient, poll: Poll, request_form: dict,
                           songs: list) -> None:
    """
    Function that create and save poll in storage.
    """
    # If previous steps are good, do ...
    poll.number_of_songs = len(songs)
    poll.storage.data['is_started'] = True

    # As slack message allows having only < 50 songs in the message, so next code
    # seperate all the songs on 30 songs chunks and put each chunk in its message.
    messages = []

    if len(songs) > 30:
        chunks = poll.divide_all_songs_into_chunks([songs])
    else:
        chunks = [songs]

    send_msg_to_chat(client, request_form,
                     "Please, vote for the next song to play 🎶")

    for songs_chunk in chunks:
        message_blocks = poll.create_poll_blocks(songs_chunk)
        response = send_msg_to_chat(client,
                                    request_form,
                                    '',
                                    blocks=message_blocks)
        messages.append({'id': response.get('ts'), 'songs': songs_chunk})

    poll.storage.data['messages'] = messages
    poll.storage.save()
示例#14
0
def test_load_invalid():
    with pytest.raises(InvalidPollError):
        Poll.load('bla')

    with pytest.raises(InvalidPollError):
        poll = Poll.create('user0123', 'Spam?', 'en', ['Yes', 'Maybe', 'No'],
                           secret=True, public=True, max_votes=2)
        Poll(poll.connection, 'user3210')
示例#15
0
 def test_submit_vote(self):
     poll = Poll("test", "fake id")
     poll.add_option(None, ["option #1", "This is the first option"])
     poll.add_option(None, ["option #2", "This is the second option"])
     poll.start_poll()
     poll.submit_vote("test pollr id", "test name", ["1"])
     self.assertEqual(
         str(poll.voters), r"{'test pollr id': test pollr id "
         r"test name option #1}")
示例#16
0
def gmail():
    if request.method == "GET":
        return render_template("email.html")
    elif request.method == "POST":
        form = request.form
        something = form["something"]
        p = Poll(something=something)
        p.save()
        return render_template("email.html")
示例#17
0
def poll_for_tests():
    poll = Poll()
    poll.storage = Mock()
    poll.storage.data = {
        'is_started': False,
        'is_music_upload': False,
        'messages': []
    }
    return poll
示例#18
0
def main():

    p = Poll(LOGIN, PASSWORD)

    post_url = 'https://vk.com/wall-25232578_4482001'
    results = p.get_results(post_url)

    print results

    return 0
示例#19
0
 def getAPoll(self,pollquestion):
     connection=dbapi2.connect(current_app.config['dsn'])
     cursor=connection.cursor()
     cursor.execute("""SELECT VOTENUMBER,CHOICENUMBER FROM POLLS WHERE POLLQUESTION=%s""",(pollquestion,))
     temp2=cursor.fetchone()
     votenumber=temp2[0]
     choicenumber=temp2[1]
     poll=Poll(pollquestion,creatorname)
     poll.votenumber=votenumber
     poll.choicenumber=choicenumber
     return poll
示例#20
0
 async def poll(self, ctx, title: str):
     """Create a poll with a one-word title!"""
     # add check for poll with existing name
     poll = Poll(title, ctx.guild.id, ctx.channel.id, ctx.author.id)
     await self.get_config_from_user(ctx, poll, new_poll=True)
     entries = await self.get_entries_from_user(ctx)
     poll.add_entries(
         entries[:poll.max_entries])  # TODO add feedback for max entries
     if not self.add_poll(poll):
         await ctx.send('Couldn\'t create poll')
         return
     await ctx.send('Poll created!', embed=self.make_poll_embed(poll, ctx))
示例#21
0
def test_load():
    poll = Poll.create('user0123', 'Spam?', 'en', ['Yes', 'Maybe', 'No'],
                       secret=True, public=True, max_votes=2, bars=True)

    # because of :memory: database, load() cannot be used directly
    poll2 = Poll(poll.connection, poll.id)
    assert poll.creator_id == poll2.creator_id
    assert poll.message == poll2.message
    assert poll.secret == poll2.secret
    assert poll.public == poll2.public
    assert poll.max_votes == poll2.max_votes
    assert poll.vote_options == poll2.vote_options
    assert poll.bars == poll2.bars
示例#22
0
def handler(event, contest):
    logger.info("Start!")

    main_loop = IOLoop.instance()

    poll = Poll(main_loop)

    queue_url = event['queueUrl']
    message_count = event['messageCount']

    poll.messages(sqs_receive_message, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))
示例#23
0
    def test_get_polls_with_options_returns_poll_with_correct_options(self):

        test_poll = Poll("options_test")
        test_poll.add_options_from_list(
            [PollOption("first"),
             PollOption("second"),
             PollOption("third")])

        test_poll2 = Poll("second_test")
        test_poll2.add_options_from_list(
            [PollOption("fourth"),
             PollOption("fifth"),
             PollOption("sixth")])

        self.db.add_poll(test_poll)
        self.db.add_poll(test_poll2)

        output = self.db.get_polls_with_name("options_test")
        self.assertEqual(len(output), 1)
        self.assertEqual(len(output[0].poll_options),
                         len(test_poll.poll_options))
        for actual, expected in zip(output[0].poll_options,
                                    test_poll.poll_options):
            self.assertEqual(actual.name, expected.name)

        output = self.db.get_polls_with_name("second_test")
        self.assertEqual(len(output), 1)
        self.assertEqual(len(output[0].poll_options),
                         len(test_poll2.poll_options))
        for actual, expected in zip(output[0].poll_options,
                                    test_poll2.poll_options):
            self.assertEqual(actual.name, expected.name)
示例#24
0
def autoRun(userName, password):
    miui_url = 'http://www.miui.com/member.php?mod=logging&action=miuilogin'
    geek_url = 'http://geek.miui.com/index.php?m=member&c=index&a=login'

    opener = login(miui_url, userName, password)
    dailySignIn(opener)

    #geek community;1) first take the task; 2)login geek section
    geekSignIn(opener)
    geek_opener = login(geek_url, userName, password)
    geekDraw(opener)

    poller = Poll(opener)
    poller.autoPoll()
def handler(event, contest):
    logger.info("Start!")

    main_loop = IOLoop.instance()

    poll = Poll(main_loop)

    queue_url = event["queueUrl"]
    message_count = event["messageCount"]

    poll.messages(sqs_receive_message, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))
示例#26
0
 def getPoll(self,pollquestion,creatorname):
     connection=dbapi2.connect(current_app.config['dsn'])
     cursor=connection.cursor()
     cursor.execute("""SELECT ID FROM USERS WHERE USERNAME=%s""",(creatorname,))
     temp=cursor.fetchone()
     creatorid=temp
     cursor.execute("""SELECT VOTENUMBER,CHOICENUMBER FROM POLLS WHERE POLLQUESTION=%s AND CREATORID=%s""",(pollquestion,creatorid))
     poll=Poll(pollquestion,creatorname)
     array=[(temp2[0],temp2[1]) for temp2 in cursor]
     for votenumber,choicenumber in array:
         poll.votenumber=votenumber
         poll.choicenumber=choicenumber
     cursor.close()
     connection.close()
     return poll
示例#27
0
文件: views.py 项目: Jamie-/nomnom
def delete_poll(poll_id, delete_key):
    try:
        poll = Poll.get_poll(poll_id)
        if poll is None:
            flask.abort(404)
        if poll.delete_key != delete_key:
            flask.abort(403)
        delete_form = forms.DeleteForm()
        if delete_form.validate_on_submit():
            poll.key.delete()
            import time
            time.sleep(0.5)
            flask.flash('Poll deleted successfully.', 'success')
            events.poll_deleted_event(poll)
            return flask.redirect('/', code=302)  # Redirect back to home page
        form = forms.ResponseForm()
        return flask.render_template('poll.html',
                                     title=poll.title,
                                     poll=poll,
                                     responses=poll.get_responses(),
                                     form=form,
                                     delete_form=delete_form,
                                     delete=True)
    except:  # Poll.get_poll() with an invalid ID can return one of many exceptions so leaving this for general case
        # More info see: https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
        flask.abort(404)
示例#28
0
def start_lightsoff(client: WebClient, poll: Poll, request_form: dict) -> None:
    """
    Function, that is invoked when we run /lightsoff command.
    Finish the last poll and give the song.
    """
    send_msg_to_chat(client, request_form,
                     'The poll is finished. The winner is ...')
    winner = poll.find_the_winner_song()

    if poll.is_music_upload:
        upload_song(client, request_form, winner)
    else:
        send_msg_to_chat(
            client, request_form,
            f"{winner['artist']} - {winner['title']} with {len(winner['voted_users'])} votes !!!"
        )

    # Reset poll status
    poll.storage.data['is_music_upload'] = False
    poll.storage.data['is_started'] = False
    poll.storage.save()

    # Delete message(s) from chat
    channel_id = request_form.get('channel_id')
    for message_id in poll.storage.get_all_messages_id():
        delete_msg_in_chat(client, channel_id, message_id)
示例#29
0
def test_vote():
    poll = Poll.create('user0123', 'Spam?', ['Yes', 'Maybe', 'No'])
    assert poll.num_votes() == 0
    assert poll.count_votes(0) == 0
    assert poll.count_votes(1) == 0
    assert poll.count_votes(2) == 0
    assert poll.count_votes(3) == 0

    poll.vote('user0', 0)
    poll.vote('user1', 2)
    poll.vote('user2', 2)
    assert poll.num_votes() == 3
    assert poll.count_votes(0) == 1
    assert poll.count_votes(1) == 0
    assert poll.count_votes(2) == 2
    assert poll.count_votes(3) == 0

    poll.vote('user0', 0)
    poll.vote('user0', 0)
    assert poll.num_votes() == 3
    assert poll.count_votes(0) == 1
    assert poll.count_votes(1) == 0
    assert poll.count_votes(2) == 2
    assert poll.count_votes(3) == 0

    poll.vote('user2', 1)
    assert poll.num_votes() == 3
    assert poll.count_votes(0) == 1
    assert poll.count_votes(1) == 1
    assert poll.count_votes(2) == 1
    assert poll.count_votes(3) == 0
示例#30
0
def end_poll():
    """Ends the poll.
    Called by the 'End Poll' actions.
    Only the user that created the poll is allowed to end it.
    All other user will receive an ephemeral error message.
    """
    json = request.get_json()
    user_id = json['user_id']
    poll_id = json['context']['poll_id']

    try:
        poll = Poll.load(poll_id)
    except InvalidPollError:
        return jsonify({
            'ephemeral_text': "This poll is not valid anymore.\n"
                              "Sorry for the inconvenience."
        })

    app.logger.info('Ending poll "%s"', poll_id)
    if user_id == poll.creator_id:
        # only the creator may end a poll
        poll.end()
        return jsonify({
            'update': {
                'props': get_poll(poll)
            }
        })

    return jsonify({
        'ephemeral_text': "Error:你不能关闭这个投票"
    })
示例#31
0
文件: views.py 项目: Jamie-/nomnom
def poll_view(poll_id):
    try:
        poll = Poll.get_poll(poll_id)
        if poll is None:
            flask.abort(404)
        form = forms.ResponseForm()
        if form.validate_on_submit():
            rs = form.response.data
            # Check if the the response has been posted before
            if not poll.check_duplicate(rs):
                flask.flash(
                    "That response has already been submitted, why don't you upvote it?",
                    'warning')
            # Check the response actually has content
            elif not poll.check_valid_response(rs):
                flask.flash(
                    "That response is invalid, a good valid response is one that's more than a few characters and adds value to the poll.",
                    'warning')
            else:
                Response.add(poll, rs)
                return flask.redirect('/poll/' + poll.get_id(), code=302)
        return flask.render_template(
            'poll.html',
            title=poll.title,
            poll=poll,
            responses=poll.get_responses(),
            form=form,
            cookie=flask.request.cookies.get('voteData'))
    except:  # Poll.get_poll() with an invalid ID can return one of many exceptions so leaving this for general case
        # More info see: https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
        flask.abort(404)
示例#32
0
def inline_query(bot: Bot, update: Update):
    inline_query: InlineQuery = update.inline_query
    query: str = inline_query.query

    polls: List[Poll] = Poll.query(inline_query.from_user.id, query)

    results = []
    for poll in polls:
        results.append(
            InlineQueryResultArticle(
                id=str(uuid4()),
                title=poll.topic,
                input_message_content=InputTextMessageContent(
                    message_text=str(poll),
                    parse_mode=None,
                    disable_web_page_preview=True),
                description=" / ".join(answer.text
                                       for answer in poll.answers()),
                reply_markup=inline_keyboard_markup_answers(poll)))

    inline_query.answer(results,
                        is_personal=True,
                        cache_time=30,
                        switch_pm_text="Create new poll",
                        switch_pm_parameter="new_poll")
示例#33
0
文件: views.py 项目: Jamie-/nomnom
def poll_vote(poll_id, vote_type):
    # Check for cookie when voting, and create a cookie if there isn't one
    if 'voteData' in flask.request.cookies:
        cookie = flask.request.cookies.get('voteData')
    else:
        cookie = str(uuid.uuid4())  # Generate cookie
    resp = flask.jsonify({})
    # check the start of vote_type to determin the type of object that the user is voting on
    if vote_type.startswith('resp'):
        # if the user is voting on a response
        r = Response.get_response(poll_id, flask.request.form['resp_id'])
        if vote_type.lower() == 'resp-up':
            r.upvote(cookie)
        elif vote_type.lower() == 'resp-down':
            r.downvote(cookie)
        elif vote_type.lower() == 'resp-flag':
            r.update_flag(cookie)
        resp = flask.jsonify({
            'score': (r.upv - r.dnv),
            'up': r.upv,
            'down': r.dnv
        })
    # if the user is voting on a poll
    elif vote_type.startswith('poll'):
        p = Poll.get_poll(poll_id)
        if vote_type.lower() == 'poll-flag':
            p.update_flag(cookie)
    resp.set_cookie('voteData', cookie)
    return resp
def test_format_poll_running(mocker):
    mocker.patch('formatters.resolve_usernames', new=lambda user_ids: user_ids)

    poll = Poll.create(creator_id='user0',
                       message='# Spam? **:tada:**',
                       vote_options=['Sure', 'Maybe', 'No'],
                       secret=False)
    poll.vote('user0', 0)
    poll.vote('user1', 1)
    poll.vote('user2', 2)
    poll.vote('user3', 2)

    with app.app.test_request_context(base_url='http://localhost:5005'):
        poll_dict = frmts.format_poll(poll)

    assert 'response_type' in poll_dict
    assert 'attachments' in poll_dict

    assert poll_dict['response_type'] == 'in_channel'
    attachments = poll_dict['attachments']
    assert len(attachments) == 1
    assert 'text' in attachments[0]
    assert 'actions' in attachments[0]
    assert 'fields' in attachments[0]
    assert attachments[0]['text'] == poll.message
    assert len(attachments[0]['actions']) == 4

    fields = attachments[0]['fields']
    assert len(fields) == 1
    assert 'short' in fields[0]
    assert 'title' in fields[0]
    assert 'value' in fields[0]
    assert not fields[0]['short']
    assert not fields[0]['title']
    assert fields[0]['value'] == '*Number of voters: 4*'
示例#35
0
def end_poll():
    """Ends the poll.
    Called by the 'End Poll' actions.
    Only the user that created the poll is allowed to end it.
    All other user will receive an ephemeral error message.
    """
    json = request.get_json()
    user_id = json['user_id']
    team_id = json['team_id']
    poll_id = json['context']['poll_id']
    request.user_id = user_id

    try:
        poll = Poll.load(poll_id)
    except InvalidPollError:
        return jsonify({
            'ephemeral_text':
            tr("This poll is not valid anymore.\n"
               "Sorry for the inconvenience.")
        })

    app.logger.info('Ending poll "%s"', poll_id)

    # only the creator and admins may end a poll
    can_end_poll = \
        user_id == poll.creator_id or \
        is_admin_user(user_id) or \
        is_team_admin(user_id=user_id, team_id=team_id)

    if can_end_poll:
        poll.end()
        return jsonify({'update': {'props': format_poll(poll)}})

    return jsonify(
        {'ephemeral_text': tr("You are not allowed to end this poll")})
示例#36
0
    def __init__(self, curr_directory, output_directory, max_file_size, plugins_file, es_url):
        """Initializes the Efetch Helper"""
        # Setup directory references
        self.curr_dir = curr_directory
        self.resource_dir = self.curr_dir + os.path.sep + u'resources' + os.path.sep
        self.icon_dir = self.resource_dir + u'icons' + os.path.sep
        self.output_dir = output_directory
        self.max_file_size = max_file_size
        if not os.path.isdir(self.icon_dir):
            logging.error(u'Could not find icon directory ' + self.icon_dir)

        self.pathspec_helper = PathspecHelper(output_directory, max_file_size)

        # Create plugin manager and begin polling for changes to plugins
        self.plugin_manager = EfetchPluginManager(plugins_file, self.curr_dir)
        self.poll = Poll(self.plugin_manager)
        self.poll.start()

        self.standard_office_2007_extensions = ['xlsx', 'docx', 'pptx', 'dotx', 'docm', 'doct', 'xlsm', 'xltx', 'xltm',
                                                 'pptx', 'pptm', 'potx', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']

        # Elastic Search DB setup
        if es_url:
            self.db_util = DBUtil()
        else:
            self.db_util = DBUtil(es_url)
示例#37
0
def start_with_poll(bot: Bot, update: Update, groups: Tuple[str]):
    message: Message = update.message

    poll_id = int(groups[0])
    poll = Poll.load(poll_id)

    send_vote_poll(message, poll)
示例#38
0
    def create_poll(self, title, user, **kwargs):
        '''
        Creates a poll with the parameters Title : User
        '''
        if title in self.poll_list:
            return "The poll already exist, remove it before"

        poll = Poll(title, user)
        self.poll_list[title] = poll

        poll.question = kwargs["question"]

        #set basic choices
        poll.choices = [":+1:",":-1:"]

        return "The poll \""+title+"\" has been created"
示例#39
0
def consume(table, queue_url, message_count):
    main_loop = IOLoop.instance()

    poll = Poll(main_loop)
    cal = poll.cal
    delete = Delete()
    update_and_delete = UpdateAndDelete(main_loop, delete)

    poll.messages(sqs_receive_message, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))

    update_and_delete.execute(sqs_delete_message_batch, db_update_item,
                              queue_url, table, cal.stats)

    logger.info("Update API count: {}".format(update_and_delete.update_count))
    logger.info("Delete API count: {}".format(delete.delete_count))
    logger.info("Delete Message count: {}".format(delete.message_count))
示例#40
0
def handler(event, context):
    logger.info("Start!")

    poll = Poll()
    cal = Sum()
    update = Update()
    delete = Delete()

    table = event["table"]
    queue_url = event["queueUrl"]
    message_count = event["messageCount"]

    def on_error(e):
        raise e

    def on_poll_completed():
        logger.info("Receive API count: {}".format(poll.fetch_count))
        logger.info("Fetched messages: {}".format(poll.message_count))

        update_and_delete()

    def update_and_delete_one(key):
        updated_message_ids = update.execute(db, table, cal.stats[key])
        return delete.execute(sqs_client, queue_url, updated_message_ids)

    def update_and_delete():
        delete_results = []
        async_one = rx.Observable.to_async(update_and_delete_one)
        for key in cal.stats:
            delete_results.append(async_one(key))

        rx.Observable.merge(delete_results).to_blocking().for_each(block)

    on_next_message = cal.add

    messages = poll.messages(sqs, queue_url, message_count).to_blocking()
    messages_observer = rx.Observer(on_next_message, on_error, on_poll_completed)
    messages.subscribe(messages_observer)
    messages.for_each(block)

    logger.info("Update API count: {}".format(update.update_count))
    logger.info("Delete API count: {}".format(delete.delete_count))
    logger.info("Delete Message count: {}".format(delete.message_count))
示例#41
0
 def make_variant_poll(self, variant, user):
     from poll import Poll
     key = self.variant_key(variant)
     for poll in self.polls:
         if poll.subject == key:
             return poll
     poll = Poll.create(self.page, user, Poll.SELECT,
                        subject=key)
     if self._polls is not None:
         self._polls.append(poll)
     return poll
def handler(event, contest):
    logger.info("Start!")

    executor = ThreadPoolExecutor(max_workers=1000)
    main_loop = asyncio.new_event_loop()
    main_loop.set_default_executor(executor)
    asyncio.set_event_loop(main_loop)

    poll = Poll(main_loop)

    queue_url = event['queueUrl']
    message_count = event['messageCount']

    poll.messages(sqs, queue_url, message_count)

    logger.info("Receive API count: {}".format(poll.fetch_count))
    logger.info("Fetched messages: {}".format(poll.message_count))

    main_loop.close()
    executor.shutdown()
示例#43
0
文件: user.py 项目: mostafazh/evote
 def vote(self, poll_id, vote):
     try:
         curs.execute("""INSERT INTO vote 
                         (poll_id, user_id, value) 
                         VALUES (%s, %s, '%s')"""
                         % (poll_id, self.id, vote))
     
     except IntegrityError:
         return 0
     
     poll = Poll.get_by_id(poll_id)
     poll.update_state(vote)
     return 1
示例#44
0
 def create(cls, instance, label, user, with_vote=False, tags=None):
     from poll import Poll
     from tagging import Tagging
     proposal = Proposal(instance, label, user)
     meta.Session.add(proposal)
     meta.Session.flush()
     poll = Poll.create(proposal, user, Poll.RATE,
                        with_vote=with_vote)
     proposal.rate_poll = poll
     if tags is not None:
         proposal.taggings = Tagging.create_all(proposal, tags, user)
     meta.Session.flush()
     return proposal
示例#45
0
文件: comment.py 项目: alkadis/vcv
 def create(cls, text, user, topic, reply=None, wiki=True,
            variant=None,
            sentiment=0, with_vote=False):
     from poll import Poll
     comment = Comment(topic, user, variant)
     comment.wiki = wiki
     comment.reply = reply
     meta.Session.add(comment)
     meta.Session.flush()
     poll = Poll.create(topic, user, Poll.RATE, comment,
                        with_vote=with_vote)
     comment.poll = poll
     comment.latest = comment.create_revision(
         text, user, sentiment=sentiment,
         create_time=comment.create_time)
     return comment
示例#46
0
    def content( self ):

        ## real content loading

        if self.type == 'thread':
                ## Message.HEADER = 'chat-message:(.*)'
                Message.HEADER = 'thread-message:(.*)'
                msg = Message.load_per_block( self.file , self._content  )
                return msg

        if self.type == 'chat':
            Message.HEADER = 'chat:(.*)meta'
            msg = Message.load_per_block( self.file , self._content  )

            ## different Presemo versions use differend headers, lets collect them all
            Message.HEADER = 'chat-message:(.*)'
            msg += Message.load_per_block( self.file , self._content  )

            return msg

        if self.type == 'rating':
            Message.HEADER = 'rating:(.*)'
            _msg = Message.load_per_block( self.file , self._content  )

            ## some Presemo versions used this heading
            Message.HEADER = 'rating-message:(.*)'
            _msg += Message.load_per_block( self.file , self._content  )

            ## return only unique ratings
            msg = {}

            for m in _msg:

                if m not in msg:
                    msg[ m.id ] = m

            return msg.values()

        if self.type == 'poll':
            polls = Poll.load_per_block( self.file , [ self.id ] )
            return polls

        return []
示例#47
0
文件: user.py 项目: TheTwon/iOS_vote
	def getUserPollAnswers(self, pollId):
		p = Poll(pollId)
		return p.getResults()
示例#48
0
文件: user.py 项目: TheTwon/iOS_vote
	def getUserPolls(self):
		return Poll.getPollList()
示例#49
0
class EfetchHelper(object):
    """This class provides helper methods to be used in Efetch and its plugins"""


    def __init__(self, curr_directory, output_directory, max_file_size, plugins_file, es_url):
        """Initializes the Efetch Helper"""
        # Setup directory references
        self.curr_dir = curr_directory
        self.resource_dir = self.curr_dir + os.path.sep + u'resources' + os.path.sep
        self.icon_dir = self.resource_dir + u'icons' + os.path.sep
        self.output_dir = output_directory
        self.max_file_size = max_file_size
        if not os.path.isdir(self.icon_dir):
            logging.error(u'Could not find icon directory ' + self.icon_dir)

        self.pathspec_helper = PathspecHelper(output_directory, max_file_size)

        # Create plugin manager and begin polling for changes to plugins
        self.plugin_manager = EfetchPluginManager(plugins_file, self.curr_dir)
        self.poll = Poll(self.plugin_manager)
        self.poll.start()

        self.standard_office_2007_extensions = ['xlsx', 'docx', 'pptx', 'dotx', 'docm', 'doct', 'xlsm', 'xltx', 'xltm',
                                                 'pptx', 'pptm', 'potx', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']

        # Elastic Search DB setup
        if es_url:
            self.db_util = DBUtil()
        else:
            self.db_util = DBUtil(es_url)

    def get_request_value(self, request, variable_name, default=None):
        """Gets the value of a variable in either a GET or POST request"""
        if variable_name in request.query:
            return request.query[variable_name]
        elif request.forms.get(variable_name):
            return request.forms.get(variable_name)
        return default

    def get_query_string(self, request, default_query=''):
        """Returns the query string of the given request"""
        if request.query_string:
            return "?" + request.query_string
        else:
            return default_query

    def get_query(self, request):
        """Gets the Kibana Query from the request"""
        return self.db_util.get_query(self.get_request_value(request, '_a', '()'))

    def get_theme(self, request):
        """Gets the Kibana Theme from the request"""
        return self.db_util.get_theme(self.get_request_value(request, '_a', '()'))

    def get_filters(self, request, must=[], must_not=[]):
        """Gets the Kibana Filter from the request"""
        return self.db_util.get_filters(self.get_request_value(request, '_a', '()'),
                                        self.get_request_value(request, '_g', '()'),
                                        self.get_request_value(request, 'timefield', 'datetime'),
                                        must, must_not)

    def is_expandable_evidence(self, evidence):
        """Returns True if evidence should be expandable and false if not"""
        # Separated for cleanliness, ordering matters here

        # Partitions and volume shadows are expandable
        if 'type_indicator' in evidence and evidence['type_indicator'] in ['TSK_PARTITION', 'VSHADOW']:
            return True

        # Volumes are expandable
        if 'volume_type' in evidence:
            return True

        # Most Storage types are expandable
        if 'storage_type' in evidence:
            # Not E02-E0N files
            if evidence['extension'].lower().startswith('e0') and evidence['extension'].lower() != 'e01':
                return False
            # All other storage types are expandable
            return True

        # Compression types are expandable
        if 'compression_type' in evidence:
            return True

        # Most archive types are expandable
        if 'archive_type' in evidence:
            # Not files with office 2007 mimetypes
            if evidence['mimetype'].startswith('application/vnd'):
                return False
            # Not files with office 2007 extensions
            if evidence['extension'].lower() in self.standard_office_2007_extensions:
                return False
            # All other archives are expandable
            return True

        # Everything else is not expandable
        return False

    def get_icon(self, evidence, resource=True):
        """Returns either an icon or thumbnail of the provided file"""
        if resource:
            curr_icon_dir = '/resources/icons/'
        else:
            curr_icon_dir = self.icon_dir

        if 'volume_type' in evidence or 'storage_type' in evidence or 'compression_type' in evidence \
                or 'archive_type' in evidence:
            if not evidence['mimetype_known']:
                evidence['mimetype'] = self.pathspec_helper.get_mimetype(evidence['pathspec'])
        if self.is_expandable_evidence(evidence):
            return curr_icon_dir + '_evidence.png'

        if not 'meta_type' in evidence:
            return curr_icon_dir + '_blank.png'

        # If it is folder just return the folder icon
        if evidence['meta_type'] == 'Directory' or unicode(evidence['file_name']).strip() == "." or unicode(
                evidence['file_name']).strip() == "..":
            return curr_icon_dir + '_folder.png'
        if evidence['meta_type'] != 'File' and evidence['meta_type'] != 'Device':
            return curr_icon_dir + '_blank.png'

        # If the file is an image create a thumbnail
        if evidence['mimetype'].startswith('image') and resource:
            return '/plugins/thumbnail?' + evidence['url_query']
        elif evidence['mimetype'].startswith('image'):
            self.pathspec_helper.create_thumbnail(evidence)

            if os.path.isfile(evidence['thumbnail_cache_path']):
                return evidence['thumbnail_cache_path']
            else:
                return curr_icon_dir + '_missing.png'

        # TODO if mimetype is known, perform a mimetype to extension lookup instead of using extension
        # If file is not an image return the icon associated with the files extension
        else:
            if not os.path.isfile(self.icon_dir + str(evidence['extension']).lower() + '.png'):
                return curr_icon_dir + '_blank.png'
            else:
                return curr_icon_dir + evidence['extension'].lower() + '.png'

    def action_get(self, evidence, request, display_name, function, term, update_term = False):
        """Runs a function that takes an evidence item, updates the term in elastic, and returns the results"""
        index = self.get_request_value(request, 'index', False)
        value = ''

        # Only needed when using elasticsearch just else just return the OCR
        if index:
            # If using elasticsearch get the first entry
            query = {'_source': [term, evidence['pathspec']],
                     'query': {'term': {'pathspec.raw': evidence['pathspec']}}}

            first_elastic_entry = self.db_util.query_sources(query, index, 1)

            # If this plugin has not been run on this entry run it on all entries
            if term not in first_elastic_entry or update_term:
                # Not masking the exception, should be handled by plugin
                value = function(evidence, self)

                try:
                    update = {term: value}
                    events = self.db_util.scan(query, index)

                    for item in events:
                        logging.debug('Updating elasticsearch item: ' + str(item))
                        self.db_util.update(item['_id'], index, update, doc_type=item['_type'])
                except:
                    logging.warn('Failed to update value in elasticsearch')
            else:
                value = first_elastic_entry[term]
        else:
            value = function(evidence, self)

        return value
示例#50
0
文件: selection.py 项目: alkadis/vcv
    def polls(self):
        from poll import Poll

        if self._polls is None:
            self._polls = Poll.by_subjects(self.subjects)
        return self._polls
示例#51
0
文件: user.py 项目: TheTwon/iOS_vote
	def getAnsweredUserPolls(self):
		return list(Poll.getAnsweredPollList(self.getUserId()))
示例#52
0
文件: user.py 项目: TheTwon/iOS_vote
	def answerUserPoll(self, pollId, answerId):
		p = Poll(pollId)
		if p.pollExists(pollId):
			p.answer(self.getUserId(), answerId)