Example #1
0
def api_export_team_by_super(request, para):
    try:
        activity = Activity.objects.filter(id=int(para.activity_id)).first()
        if not activity:
            raise BusinessException(ERR_ACTIVITY_NOT_EXIST)
        user, role = get_user(activity, request.user)
        if not user:
            raise BusinessException(ERR_USER_AUTH)
        if not is_activity_owner(activity, request.user):
            raise BusinessException(ERR_USER_AUTH)
        team_id_list = [
            int(each)
            for each in para.team_id_list.strip().strip(',').split(',') if each
        ]
        # team_list = list(Team.objects.filter(id__in=team_id_list))
        wb = t_services.export_team_by_super(request.user, activity, user,
                                             role, team_id_list)

        fname = u'%s.xlsx' % suuid()
        response = HttpResponse(content_type='application/vnd.ms-excel')
        response[
            'Content-Disposition'] = 'attachment; filename=' + fname.encode(
                'utf-8')
        wb.save(response)
        return response

        # path = os.path.join(settings.BASE_DIR, 'media/%s.xlsx' % suuid())
        # wb.save(path)
        # return response200({'c': SUCCESS[0], 'm': SUCCESS[1], 'd': path})
    except Exception as ex:
        logger.exception(ex)
        return response_exception(ex, ex.message)
Example #2
0
def submit_score(account, activity, expert, work_list, is_final):
    rule = Rule.objects.filter(activity=activity).first().parse_rule()
    if is_1(is_final):
        qs = FinalScore.objects.filter(status=FALSE_INT,
                                       work__in=work_list,
                                       work__activity=activity,
                                       team_expert__expert=expert)
    else:
        qs = Score.objects.filter(status=FALSE_INT,
                                  work__in=work_list,
                                  work__activity=activity,
                                  team_expert__expert=expert)

    succ_count = 0
    for each in qs:
        if not is_activity_owner(activity, account):  # 非活动创建者则只能提交自己的得分
            if each.team_expert.expert.account != account:
                logger.info(
                    'account %s try to hack submit work %s score, but it is not his/her score'
                    % (account.id, each.work.id))
                continue
        each.status = TRUE_INT
        each.save()
        succ_count += 1
        rule.update_work_status(each.work)  # 更新每一个被提交得分的作品

    fail_count = len(work_list) - succ_count

    msg1 = (u'%d个作品提交分数成功. ' % succ_count) if succ_count > 0 else ''
    msg2 = (u'%d个作品提交分数失败, 可能还未评审或已提交, 或者您没有权限提交此作品' %
            fail_count) if fail_count > 0 else ''
    return dict(c=SUCCESS[0], m=SUCCESS[1], d=msg1 + msg2)
Example #3
0
def list_team_by_super(account,
                       activity,
                       keyword,
                       judger,
                       page,
                       rows,
                       id_list=None):
    rule = Rule.objects.filter(activity=activity).first()
    if not rule:
        raise BusinessException(ERR_RULE_NOT_EXIST)
    if not is_activity_owner(activity, account):
        raise BusinessException(ERR_USER_AUTH)
    raw = list_team(activity, keyword=keyword, judger=judger, id_list=id_list)

    paged_data, result = paging_by_page(raw, rows, page)
    rule = Rule.objects.filter(activity=activity).first()
    if not rule:
        raise BusinessException(ERR_RULE_NOT_EXIST)
    max = str(rule.parse_rule().expert_count())
    for each in paged_data:
        d = detail_team(each, max)
        result['items'].append(d)
    result['id_all'] = ','.join([str(each.id) for each in raw])
    result['judger_max'] = str(max)
    return result
Example #4
0
def score(account, work, data, commit):
    rule = Rule.objects.filter(activity=work.activity).first().parse_rule()
    expert = Expert.objects.filter(del_flag=FALSE_INT, account=account).first()
    # 权限校验
    is_super = is_activity_owner(work.activity, account)
    if not is_super:
        _perm_can_judge_work(work, expert)
    rule.score(work, data, commit, expert=expert, account=account)
Example #5
0
def edit_team(account, activity, team, new_name):
    if not is_activity_owner(activity, account):
        raise BusinessException(ERR_USER_AUTH)
    if team:  # MODIFY
        if Team.objects.filter(activity=activity,
                               name=new_name).exclude(id=team.id).exists():
            raise BusinessException(ERR_TEAM_NAME_CONFLICT)
        team.name = new_name
        team.save()
    else:  # CREATE
        Team.objects.create(activity=activity, name=new_name)
    return TRUE_INT
Example #6
0
def load_all_score(account, work):
    rule = Rule.objects.filter(activity=work.activity).first().parse_rule()
    expert = Expert.objects.filter(account=account, del_flag=FALSE_INT).first()
    # 权限校验
    if not is_activity_owner(work.activity, account):
        return rule.get_score(work,
                              team_expert=None,
                              ranks=rule.get_ranks(is_creator=FALSE_INT),
                              account=account)
    else:
        return rule.get_score(work,
                              team_expert=None,
                              ranks=rule.get_ranks(is_creator=TRUE_INT),
                              account=account)
Example #7
0
def delete_team(account, activity, team_list):
    if not is_activity_owner(activity, account):
        raise BusinessException(ERR_USER_AUTH)
    succ_count = 0
    for each in team_list:
        if Work.objects.filter(activity=activity, team=each).exists():
            logger.warn('team %s contains works, so can not delete' % each.id)
            continue
        succ_count += 1
        each.del_flag = TRUE_INT
        each.save()
        TeamExpert.objects.filter(team=each).update(del_flag=TRUE_INT)
    msg1 = (u'%d个分组删除成功' % succ_count) if succ_count > 0 else ''
    fail_count = len(team_list) - succ_count
    msg2 = (u'%d个分组删除失败或不支持删除' % fail_count) if fail_count > 0 else ''
    return msg1 + msg2
Example #8
0
def list_expert_area_in_team(account, team):
    owner_user = is_activity_owner(team.activity, account)
    if not owner_user:
        raise BusinessException(ERR_USER_AUTH)
    result = list()
    area_id_list_raw = set(
        TeamExpert.objects.filter(team=team).values_list('expert__area__id',
                                                         flat=True))
    area_list_raw = Area.objects.filter(del_flag=FALSE_INT,
                                        id__in=area_id_list_raw)
    for each in area_list_raw:
        area = area_by_pov(each, owner_user.area)
        if area:
            result.append({
                'area_id': str(area.id),
                'area_name': area.area_name
            })
    return result
Example #9
0
def update_expert_in_team(account, user, role, team, new_expert_json):
    if not is_activity_owner(team.activity, account):
        raise BusinessException(ERR_USER_AUTH)
    """
        [{"sn":"2", "expert_id": "1167"}]
    """
    new_expert_list = json.loads(new_expert_json)
    new_expert_id_list = [int(each['expert_id']) for each in new_expert_list]
    new_expert_id_dict = {
        each['expert_id']: each['sn']
        for each in new_expert_list
    }
    t_e_old = TeamExpert.objects.filter(team=team).all()
    handled = list()
    for old in t_e_old:
        if old.expert.id in new_expert_id_list:
            new_sn = new_expert_id_dict[str(old.expert.id)]
            old.sn = new_sn
            if int(new_sn) == 1:
                old.is_leader = TRUE_INT
            else:
                old.is_leader = FALSE_INT
            handled.append(old.expert.id)
            old.save()
        else:
            Score.objects.filter(del_flag=FALSE_INT,
                                 team_expert=old).update(del_flag=TRUE_INT)
            FinalScore.objects.filter(
                del_flag=FALSE_INT, team_expert=old).update(del_flag=TRUE_INT)
            old.del_flag = TRUE_INT
            old.save()
    new_add = set(new_expert_id_list) - set(handled)
    for each in new_add:
        is_leader = TRUE_INT if int(
            new_expert_id_dict[str(each)]) == 1 else FALSE_INT
        TeamExpert.objects.create(team=team,
                                  expert_id=int(each),
                                  sn=new_expert_id_dict[str(each)],
                                  is_leader=is_leader)

    # 增加或者减少组内专家,更新最终得分数据(针对平均分制)
    # TODO

    return TRUE_INT
Example #10
0
def decide_subjudge(account, user, role, activity, is_active, data):
    # 活动创建者、末端用户均不可创建子级评审
    if is_activity_owner(activity, account):
        raise BusinessException(ERR_USER_AUTH)
    if user.area.area_level == 0:
        raise BusinessException(ERR_USER_AUTH)

    subjg, is_created = SubJudge.objects.update_or_create(
        activity=activity,
        user=user,
        area=user.area,
        defaults={'is_active': int(is_active)},
    )
    if int(is_active) == TRUE_INT:  # 评审后审批模式
        '''
        {
            'rank': [
                {'sort': '1', 'desc':'xxx'},
                {'sort': '2', 'desc':'xxx'},
                {'sort': '3', 'desc':'xxx'},
            ],
            'rule': {
                'code': '2',
                'judge_count': '6',
                'max': '100'
                'ignore_maxmin': '0'
            }
        }
        '''
        d = json.loads(data, strict=False)
        rank_data = d['rank']
        rule_data = d['rule']
        for rk in rank_data:
            SubJudgeRank.objects.create(subjudge=subjg,
                                        sn=int(rk['sort']),
                                        name=rk['desc'])
        SubJudgeRule.objects.create(subjudge=subjg,
                                    code=int(rule_data['code']),
                                    content=json.dumps(rule_data))
    return {'subjudge_id': str(subjg.id)}
Example #11
0
def api_edit_activity_role(request, para):
    try:
        modify_role = Role.objects.filter(id=int(para.role_id)).first()
        if not modify_role:
            raise BusinessException(ERR_ACTIVITY_NOT_EXIST)
        activity = modify_role.activity
        user, role = get_user(activity, request.user)
        if not user:
            raise BusinessException(ERR_USER_AUTH)

        # 非活动创建者则只能修改自己加入的用户(Role)
        if not is_activity_owner(activity, request.user):
            if (not role) or (modify_role.parent_role != role):
                raise BusinessException(ERR_USER_AUTH)

        result = activity_s.edit_activity_role(activity, user, role,
                                               modify_role, para.new_username,
                                               para.new_name, para.new_sex,
                                               para.new_max)
    except Exception as ex:
        logger.exception(ex)
        return response_exception(ex, ex.message)
    return response200({'c': SUCCESS[0], 'm': SUCCESS[1], 'd': result})
Example #12
0
    def get_score(self, work, team_expert=None, ranks=[], account=None):
        def score_item(rule,
                       team_expert=None,
                       status=FALSE_INT,
                       is_final=FALSE_INT,
                       ranks=[],
                       score_id='',
                       score='',
                       rank='',
                       comment=''):
            return {
                'rule':
                str(REVIEW_RULE_1),
                'score_id':
                str(score_id) if score_id else '',
                'expert_id':
                str(team_expert.expert.id) if team_expert else '',
                'team_expert_id':
                str(team_expert.id) if team_expert else '',
                'expert_name':
                str(team_expert.expert.account.name) if team_expert else '',
                'expert_area_name_full':
                area_name(team_expert.expert.area.id) if team_expert else '',
                'expert_area_name_simple':
                area_name(team_expert.expert.area.id, full=False)
                if team_expert else '',
                'status':
                str(status),
                'is_final':
                str(is_final),
                'items': [
                    {
                        'name': 'score',
                        'value': str(score),
                        'desc': u'得分',
                        'type': INPUT_NUM,
                        'range_min': '1',
                        'range_max': str(rule.max_score),
                        'enum': []
                    },
                    {
                        'name': 'rank',
                        'value': str(rank),
                        'desc': u'等级',
                        'type': INPUT_LIST,
                        'range_min': '',
                        'range_max': '',
                        'enum': ranks
                    },
                    {
                        'name': 'comment',
                        'value': str(comment),
                        'desc': u'评语',
                        'type': INPUT_TEXTAREA,
                        'range_min': '',
                        'range_max': '',
                        'enum': []
                    },
                ]
            }

        qs_score = Score.objects.filter(work=work)
        qs_final_score = FinalScore.objects.filter(work=work)

        result = {'score': [], 'final_score': []}
        if team_expert:
            s = qs_score.filter(team_expert=team_expert).first()
            fs = qs_final_score.filter(team_expert=team_expert).first()
            if team_expert.is_leader == TRUE_INT:
                fs_id = str(fs.id) if fs else ''
                fs_status = str(fs.status) if fs else FALSE_STR
                fs_score = str(fs.score) if fs else ''
                fs_rank = fs.rank.name if fs and fs.rank else ''
                fs_comment = fs.comments if fs else ''
                result['final_score'].append(
                    score_item(self,
                               team_expert=team_expert,
                               status=fs_status,
                               is_final=TRUE_INT,
                               ranks=ranks,
                               score_id=fs_id,
                               score=fs_score,
                               rank=fs_rank,
                               comment=fs_comment))
            s_id = str(s.id) if s else ''
            s_status = str(s.status) if s else FALSE_STR
            s_score = str(s.score) if s else ''
            s_rank = s.rank.name if s and s.rank else ''
            s_comment = s.comments if s else ''
            result['score'].append(
                score_item(self,
                           team_expert=team_expert,
                           status=s_status,
                           is_final=FALSE_INT,
                           ranks=ranks,
                           score_id=s_id,
                           score=s_score,
                           rank=s_rank,
                           comment=s_comment))
        else:
            # 只有赛事创建者和该组组长可以抓取本作品的所有评分
            if not is_activity_owner(work.activity, account):
                if not TeamExpert.objects.filter(team=work.team,
                                                 expert__del_flag=FALSE_INT,
                                                 expert__account=account,
                                                 is_leader=TRUE_INT).exists():
                    raise BusinessException(ERR_USER_AUTH)

            qs_te = TeamExpert.objects.filter(
                team=work.team, expert__del_flag=FALSE_INT).order_by('sn')
            for each in qs_te:
                s = qs_score.filter(team_expert=each).first()
                s_id = str(s.id) if s else ''
                s_status = str(s.status) if s else FALSE_STR
                s_score = str(s.score) if s else ''
                s_rank = s.rank.name if s and s.rank else ''
                s_comment = s.comments if s else ''
                result['score'].append(
                    score_item(self,
                               team_expert=each,
                               status=s_status,
                               is_final=FALSE_INT,
                               ranks=ranks,
                               score_id=s_id,
                               score=s_score,
                               rank=s_rank,
                               comment=s_comment))
            leader = qs_te.filter(is_leader=TRUE_INT).first()
            fs = qs_final_score.filter(
                team_expert=leader).first() if leader else None
            fs_id = str(fs.id) if fs else ''
            fs_status = str(fs.status) if fs else FALSE_STR
            fs_score = str(fs.score) if fs else ''
            fs_rank = fs.rank.name if fs and fs.rank else ''
            fs_comment = fs.comments if fs else ''
            result['final_score'].append(
                score_item(self,
                           team_expert=leader,
                           status=fs_status,
                           is_final=TRUE_INT,
                           ranks=ranks,
                           score_id=fs_id,
                           score=fs_score,
                           rank=fs_rank,
                           comment=fs_comment))
        return result
Example #13
0
    def score(self, work, data, commit, expert=None, account=None):
        data = json.loads(data)
        te = TeamExpert.objects.filter(
            expert=expert, expert__del_flag=FALSE_INT,
            team=work.team).first() if expert else None
        if te:
            logger.info('expert %s try to score work %s' %
                        (expert.id, work.id))
        else:
            logger.info('activity(%s) creator try to score work %s' %
                        (work.activity.id, work.id))
        '''
            {   "score_id": "2",
                "is_final": "0",
                "score": "95",
                "rank_id": "not used",
                "comment": "xxxxxx"  }               
        '''
        id = data['score_id'] if 'score_id' in data else None
        is_final = str(data['is_final']) if 'is_final' in data else FALSE_STR
        # 修改得分
        if id:
            if is_final == FALSE_STR:
                s = Score.objects.filter(id=int(id)).first()
                if not s:
                    raise BusinessException(ERR_USER_AUTH)
                if te:
                    if s.status == TRUE_INT:
                        raise BusinessException(
                            ERR_SCORE_SUBMITTED)  # 已提交的分数不能再修改
                    if s.team_expert != te:
                        raise BusinessException(ERR_USER_AUTH)  # 只能修改自己打的分

                s.score = int(data['score'])
                # s.rank = Ranks.objects.filter(id=int(data['rank_id'])).first()  # 平均分模式下普通评分只能给分数
                s.comments = data['comment']
                s.status = int(commit)
                s.save()
                self.update_work_status(work)
            else:
                fs = FinalScore.objects.filter(id=int(id)).first()
                if not fs:
                    raise BusinessException(ERR_USER_AUTH)
                # 平均分模式下最终得分只能由活动创建者修改
                if not is_activity_owner(work.activity, account):
                    raise BusinessException(ERR_USER_AUTH)

                fs.team_expert = None
                if int(data['score']) != fs.score:
                    logger.warn(
                        '<average mode> activity creator try to modify score, OLD: %s, NEW: %s'
                        % (fs.score, int(data['score'])))
                    raise BusinessException(
                        ERR_SCORE_AVERAGE_MODE_FORBID_CREATOR_CHANGE_SCORE)
                fs.rank = Ranks.objects.filter(id=int(data['rank_id'])).first()
                fs.comments = data['comment']
                fs.status = TRUE_INT
                fs.save()
                work.status = WORK_STATUS_HAVE_REVIEWED[0]
                work.ranks = fs.rank
                work.save()
        # 新增得分
        else:
            if is_final == FALSE_STR:
                if not te:
                    # 暂时不允许创建者直接替非组长打分
                    raise BusinessException(ERR_USER_AUTH)
                if Score.objects.filter(work=work,
                                        team_expert=te,
                                        status=TRUE_INT).exists():
                    raise BusinessException(ERR_SCORE_SUBMITTED)
                Score.objects.update_or_create(  # 防止重复打分
                    work=work,
                    team_expert=te,
                    del_flag=FALSE_INT,
                    defaults=dict(
                        status=int(commit),
                        score=int(data['score']) if 'score' in data else None,
                        rank=None,
                        comments=data['comment'] if 'comment' in data else '',
                    ))
                self.update_work_status(work)
            else:
                # 平均分模式下最终得分只能由活动创建者修改
                if not is_activity_owner(work.activity, account):
                    raise BusinessException(ERR_USER_AUTH)

                # 平均分模式下最终成绩只能修改等级,不能修改得分
                if 'score' in data and data['score'] and str(
                        data['score']) != '-1' and str(data['score']) != '0':
                    logger.warn(
                        '<average mode> activity creator try to give score: %s'
                        % data['score'])
                    raise BusinessException(
                        ERR_SCORE_AVERAGE_MODE_FORBID_CREATOR_CHANGE_SCORE)
                rk = Ranks.objects.filter(id=int(
                    data['rank_id'])).first() if data['rank_id'] else None
                FinalScore.objects.update_or_create(  # 防止重复打分
                    work=work,
                    del_flag=FALSE_INT,
                    defaults=dict(
                        status=TRUE_INT,
                        rank=rk,
                        comments=data['comment'] if 'comment' in data else '',
                    ))
                work.status = WORK_STATUS_HAVE_REVIEWED[0]
                work.ranks = rk
                work.save()