Esempio n. 1
0
def voting(group_id, voter, nick, msg):
    """
    voting
    """
    tip = ""
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_values = _VOTE_FLAG_RE.findall(msg)
        task = wechatdb.get_vote_task({"id": vote_id})
        item_number = len(task["items"])
        record_id = wechatdb.next_id("vote_record")
        vote_record = {
            "id": record_id,
            "vote_id": vote_id,
            "vote": vote_values,
            "group_id": group_id,
            "msg": msg,
            "voter": voter,
            "nick": nick,
            "created": timestamp_now(),
        }
        if item_number == len(vote_values):
            vote_record["valid"] = True
            wechatdb.update_vote_task({"id": vote_id}, {"$set": {"last_voter": nick}})
        else:
            vote_record["valid"] = False
        wechatdb.update_vote_record({"vote_id": vote_id, "voter": voter}, vote_record, upsert=True)
    return {"tip": tip}
Esempio n. 2
0
def voting(group_id, voter, nick, msg):
    '''
    voting
    '''
    tip = ''
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_values = _VOTE_FLAG_RE.findall(msg)
        task = wechatdb.get_vote_task({'id': vote_id})
        item_number = len(task['items'])
        record_id = wechatdb.next_id('vote_record')
        vote_record = {
            'id': record_id,
            'vote_id': vote_id,
            'vote': vote_values,
            'group_id': group_id,
            'msg': msg,
            'voter': voter,
            'nick': nick,
            'created': timestamp_now(),
        }
        if item_number == len(vote_values):
            vote_record['valid'] = True
        else:
            vote_record['valid'] = False
        wechatdb.update_vote_record({'vote_id': vote_id, 'voter': voter}, vote_record, upsert=True)
        wechatdb.update_vote_task({'id': vote_id}, {'$set': {'last_voter': nick}})
    return {'tip': tip}
Esempio n. 3
0
def vote_start(group_id, starter, nick, msg, voted):
    '''
    vote start
    '''
    tip = u'------\n我是开始投票分割线\n顶[强]\n踩[弱]\n------'
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        tip = u'本群有一个未结束的投票 将被覆盖 现在开始新的投票'
    if START_FLAG in msg:
        items_str = msg.split(START_FLAG)[1]
        if END_FLAG in items_str:
            items_str = items_str.split(END_FLAG)[0]
            items = items_str.split(SPLIT_FLAG)
            vote_id = wechatdb.next_id('vote')
            vote_group = {
                'group_id': group_id,
                'vote_id': vote_id,
            }
            wechatdb.update_vote_group({'group_id': group_id},
                                       vote_group,
                                       upsert=True)
            vote_task = {
                'id': vote_id,
                'weight': 0.5,
                'not_vote': True,
                'group_id': group_id,
                'start_msg': msg,
                'starter': starter,
                'nick': nick,
                'voted': voted,
                'created': timestamp_now(),
                'status': Status.START,
                'items': items,
            }
            group_config = wechatdb.get_group_config({'group_id': group_id})
            if group_config:
                if 'weight' in group_config:
                    vote_task['weight'] = group_config['weight']
                if 'not_vote' in group_config:
                    vote_task['not_vote'] = group_config['not_vote']
            wechatdb.update_vote_task({'id': vote_id}, vote_task, upsert=True)
        else:
            tip = HELP
    else:
        tip = HELP
    return {'tip': tip}
Esempio n. 4
0
def vote_start(group_id, starter, nick, msg, voted):
    '''
    vote start
    '''
    tip = u'------\n我是开始投票分割线\n顶[强]\n踩[弱]\n------'
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        tip = u'本群有一个未结束的投票 将被覆盖 现在开始新的投票'
    if START_FLAG in msg:
        items_str = msg.split(START_FLAG)[1]
        if END_FLAG in items_str:
            items_str = items_str.split(END_FLAG)[0]
            items = items_str.split(SPLIT_FLAG)
            vote_id = wechatdb.next_id('vote')
            vote_group = {
                'group_id': group_id,
                'vote_id': vote_id,
            }
            wechatdb.update_vote_group({'group_id': group_id}, vote_group, upsert=True)
            vote_task = {
                    'id': vote_id,
                    'weight': 0.5,
                    'not_vote': True,
                    'group_id': group_id,
                    'start_msg': msg,
                    'starter': starter,
                    'nick': nick,
                    'voted': voted,
                    'created': timestamp_now(),
                    'status': Status.START,
                    'items': items,
                }
            group_config = wechatdb.get_group_config({'group_id': group_id})
            if group_config:
                if 'weight' in group_config:
                    vote_task['weight'] = group_config['weight']
                if 'not_vote' in group_config:
                    vote_task['not_vote'] = group_config['not_vote']
            wechatdb.update_vote_task({'id': vote_id}, vote_task, upsert=True)
        else:
            tip = HELP
    else:
        tip = HELP
    return {'tip': tip}
Esempio n. 5
0
def vote_start(group_id, starter, nick, msg, voted):
    """
    vote start
    """
    tip = u"------\n我是开始投票分割线\n顶[强]\n踩[弱]\n------"
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        tip = u"本群有一个未结束的投票 将被覆盖 现在开始新的投票"
    if START_FLAG in msg:
        items_str = msg.split(START_FLAG)[1]
        if END_FLAG in items_str:
            items_str = items_str.split(END_FLAG)[0]
            items = items_str.split(SPLIT_FLAG)
            vote_id = wechatdb.next_id("vote")
            vote_group = {"group_id": group_id, "vote_id": vote_id}
            wechatdb.update_vote_group({"group_id": group_id}, vote_group, upsert=True)
            vote_task = {
                "id": vote_id,
                "weight": 0.5,
                "not_vote": True,
                "group_id": group_id,
                "start_msg": msg,
                "starter": starter,
                "nick": nick,
                "voted": voted,
                "created": timestamp_now(),
                "status": Status.START,
                "items": items,
            }
            group_config = wechatdb.get_group_config({"group_id": group_id})
            if group_config:
                if "weight" in group_config:
                    vote_task["weight"] = group_config["weight"]
                if "not_vote" in group_config:
                    vote_task["not_vote"] = group_config["not_vote"]
            wechatdb.update_vote_task({"id": vote_id}, vote_task, upsert=True)
        else:
            tip = HELP
    else:
        tip = HELP
    return {"tip": tip}
Esempio n. 6
0
def voting(group_id, voter, nick, msg):
    '''
    voting
    '''
    tip = ''
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_values = _VOTE_FLAG_RE.findall(msg)
        task = wechatdb.get_vote_task({'id': vote_id})
        item_number = len(task['items'])
        record_id = wechatdb.next_id('vote_record')
        vote_record = {
            'id': record_id,
            'vote_id': vote_id,
            'vote': vote_values,
            'group_id': group_id,
            'msg': msg,
            'voter': voter,
            'nick': nick,
            'created': timestamp_now(),
        }
        if item_number == len(vote_values):
            vote_record['valid'] = True
            wechatdb.update_vote_task({'id': vote_id},
                                      {'$set': {
                                          'last_voter': nick
                                      }})
        else:
            vote_record['valid'] = False
        wechatdb.update_vote_record({
            'vote_id': vote_id,
            'voter': voter
        },
                                    vote_record,
                                    upsert=True)
    return {'tip': tip}
Esempio n. 7
0
def vote_finish(group_id, sender, nick, msg, group_users):
    """
    vote finished
    """
    tip = ""
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user["name"] != ROBOT and user["id"] not in ROBOT_IDS:
            user_nick[user["id"]] = user["name"]
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({"id": vote_id})
        items = vote_task["items"]
        records = wechatdb.get_vote_records({"vote_id": vote_id})
        voter_set = set([record["voter"] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record["valid"]]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u"总票数: %d\n有效票数: %d\n无效票数: %d" % (vote_number, vote_valid_number, vote_invalid_number)
        if vote_task["voted"]:
            tip = u"%s\n%s" % (tip, vote_task["voted"])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([record for record in valid_records if record["vote"][i] == STRONG])
            weak_number = len([record for record in valid_records if record["vote"][i] == WEAK])
            weight = vote_task["weight"]
            passed = passed and (strong_number > (strong_number + weak_number) * weight)
            strong_rate = (
                strong_number * 1.0 / (strong_number + weak_number) if (strong_number + weak_number) > 0 else 0.0
            )
            item_str = u"%s:\n%d%%顶" % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                "name": item,
                "strong": strong_number,
                "strong_rate": strong_rate,
                "weak": weak_number,
                "passed": passed,
            }
            item_results.append(item_result)
        item_tip = "\n".join(item_tip_list)
        tip = u"%s\n%s\n投票%s\n最后投票人员: %s\n" % (tip, item_tip, PASSED_MAP[passed], vote_task.get("last_voter", ""))
        if vote_task["not_vote"]:
            tip = u"%s未投票人员: %s" % (tip, ",".join(not_vote_nicks) or u"无")

        update = {
            "$set": {
                "not_vote": not_vote_users,
                "finished_time": timestamp_now(),
                "item_results": item_results,
                "status": Status.FINISHED,
                "group_users": user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({"id": vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u"投票还未开始"
    return {"tip": tip}
Esempio n. 8
0
def vote_finish(group_id, sender, nick, msg, group_users):
    '''
    vote finished
    '''
    tip = ''
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user['name'] != ROBOT and user['id'] not in ROBOT_IDS:
            user_nick[user['id']] = user['name']
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({'id': vote_id})
        items = vote_task['items']
        records = wechatdb.get_vote_records({'vote_id': vote_id})
        voter_set = set([record['voter'] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record['valid']]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u'总票数: %d\n有效票数: %d\n无效票数: %d' % (vote_number, vote_valid_number, vote_invalid_number)
        if vote_task['voted']:
            tip = u'%s\n%s' % (tip, vote_task['voted'])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([record for record in valid_records if record['vote'][i] == STRONG])
            weak_number = len([record for record in valid_records if record['vote'][i] == WEAK])
            weight = vote_task['weight']
            passed = passed and (strong_number > (strong_number + weak_number) * weight)
            strong_rate = strong_number * 1.0 / (strong_number + weak_number) if (strong_number + weak_number) > 0 else 0.0
            item_str = u'%s:\n%d%%顶' % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                'name': item,
                'strong': strong_number,
                'strong_rate': strong_rate,
                'weak': weak_number,
                'passed': passed,
            }
            item_results.append(item_result)
        item_tip = '\n'.join(item_tip_list)
        tip = u'%s\n%s\n投票%s\n最后投票人员: %s\n' % (tip, item_tip, PASSED_MAP[passed], vote_task.get('last_voter', ''))
        if vote_task['not_vote']:
            tip = u'%s未投票人员: %s' % (tip, ','.join(not_vote_nicks) or u'无')

        update = {
            '$set': {
                'not_vote': not_vote_users,
                'finished_time': timestamp_now(),
                'item_results': item_results,
                'status': Status.FINISHED,
                'group_users': user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({'id': vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u'投票还未开始'
    return {'tip': tip}
Esempio n. 9
0
def vote_finish(group_id, sender, nick, msg, group_users):
    '''
    vote finished
    '''
    tip = ''
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user['name'] != ROBOT and user['id'] not in ROBOT_IDS:
            user_nick[user['id']] = user['name']
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({'id': vote_id})
        items = vote_task['items']
        records = wechatdb.get_vote_records({'vote_id': vote_id})
        voter_set = set([record['voter'] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record['valid']]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u'总票数: %d\n有效票数: %d\n无效票数: %d' % (vote_number, vote_valid_number,
                                                vote_invalid_number)
        if vote_task['voted']:
            tip = u'%s\n%s' % (tip, vote_task['voted'])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([
                record for record in valid_records
                if record['vote'][i] == STRONG
            ])
            weak_number = len([
                record for record in valid_records if record['vote'][i] == WEAK
            ])
            weight = vote_task['weight']
            passed = passed and (strong_number >
                                 (strong_number + weak_number) * weight)
            strong_rate = strong_number * 1.0 / (
                strong_number + weak_number) if (strong_number +
                                                 weak_number) > 0 else 0.0
            item_str = u'%s:\n%d%%顶' % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                'name': item,
                'strong': strong_number,
                'strong_rate': strong_rate,
                'weak': weak_number,
                'passed': passed,
            }
            item_results.append(item_result)
        item_tip = '\n'.join(item_tip_list)
        tip = u'%s\n%s\n投票%s\n最后投票人员: %s\n' % (
            tip, item_tip, PASSED_MAP[passed], vote_task.get('last_voter', ''))
        if vote_task['not_vote']:
            tip = u'%s未投票人员: %s' % (tip, ','.join(not_vote_nicks) or u'无')

        update = {
            '$set': {
                'not_vote': not_vote_users,
                'finished_time': timestamp_now(),
                'item_results': item_results,
                'status': Status.FINISHED,
                'group_users': user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({'id': vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u'投票还未开始'
    return {'tip': tip}