예제 #1
0
def game(request):
    """
    返回比赛题目信息的视图
    :param request: 请求对象
    :return: 渲染视图: user_info: 用户信息;kind_id: 比赛唯一标识;kind_name: 比赛名称;cop_finishat: 比赛结束时间;rule_text: 大赛规则;
    """
    uid = request.GET.get('uid', '')  # 获取uid
    kind_id = request.GET.get('kind_id', '')  # 获取kind_id
    try:  # 获取比赛信息
        kind_info = CompetitionKindInfo.objects.get(kind_id=kind_id)
    except CompetitionKindInfo.DoesNotExist:  # 未获取到渲染错误视图
        return render(request, 'err.html', CompetitionNotFound)
    try:  # 获取题库信息
        bank_info = BankInfo.objects.get(bank_id=kind_info.bank_id)
    except BankInfo.DoesNotExist:  # 未获取到,渲染错误视图
        return render(request, 'err.html', BankInfoNotFound)
    try:  # 获取用户信息
        profile = Profile.objects.get(uid=uid)
    except Profile.DoesNotExist:  # 未获取到,渲染错误视图
        return render(request, 'err.html', ProfileNotFound)
    if kind_info.question_num > bank_info.total_question_num:  # 检查题库大小
        return render(request, 'err.html', QuestionNotSufficient)
    pageconfig = get_pageconfig(kind_info.app_id)  # 获取页面配置信息
    return render(
        request,
        'competition/game.html',
        {  # 渲染视图信息
            'user_info': profile.data,
            'kind_id': kind_info.kind_id,
            'kind_name': kind_info.kind_name,
            'cop_finishat': kind_info.cop_finishat,
            'period_time': kind_info.period_time,
            'rule_text': pageconfig.get('text_info', {}).get('rule_text', '')
        })
예제 #2
0
def home(request):
    """
    比赛首页首页视图
    :param request: 请求对象
    :return: 渲染视图: user_info: 用户信息; kind_info: 比赛信息;is_show_userinfo: 是否展示用户信息表单;user_info_has_entered: 是否已经录入表单;
             userinfo_fields: 表单字段;option_fields: 表单字段中呈现为下拉框的字段;
    """

    uid = request.GET.get('uid', '')
    kind_id = request.GET.get('kind_id', '')
    created = request.GET.get('created', '0')

    try:
        kind_info = CompetitionKindInfo.objects.get(kind_id=kind_id)
    except CompetitionKindInfo.DoesNotExist:
        return render(request, 'err.html', CompetitionNotFound)

    try:
        bank_info = BankInfo.objects.get(bank_id=kind_info.bank_id)
    except BankInfo.DoesNotExist:
        return render(request, 'err.html', BankInfoNotFound)

    try:
        profile = Profile.objects.get(uid=uid)
    except Profile.DoesNotExist:
        return render(request, 'err.html', ProfileNotFound)

    if kind_info.question_num > bank_info.total_question_num:
        return render(request, 'err.html', QuestionNotSufficient)

    show_info = get_pageconfig(kind_info.app_id).get('show_info', {})
    is_show_userinfo = show_info.get('is_show_userinfo', False)
    form_fields = collections.OrderedDict()
    form_regexes = []

    if is_show_userinfo:
        userinfo_fields = show_info.get('userinfo_fields', '').split('#')
        for i in userinfo_fields:
            form_regexes.append(get_form_regex(i))
        userinfo_field_names = show_info.get('userinfo_field_names',
                                             '').split('#')
        for i in range(len(userinfo_fields)):
            form_fields.update({userinfo_fields[i]: userinfo_field_names[i]})

    return render(
        request, 'competition/index.html', {
            'user_info': profile.data,
            'kind_info': kind_info.data,
            'bank_info': bank_info.data,
            'is_show_userinfo': 'true' if is_show_userinfo else 'false',
            'userinfo_has_enterd':
            'true' if get_enter_userinfo(kind_id, uid) else 'false',
            'userinfo_fields':
            json.dumps(form_fields) if form_fields else '{}',
            'option_fields': json.dumps(show_info.get('option_fields', '')),
            'field_regexes': form_regexes,
            'created': created
        })
예제 #3
0
def home(request):
    """
    比赛首页首页视图
    :param request: 请求对象
    :return: 渲染视图: user_info: 用户信息; kind_info: 比赛信息;is_show_userinfo: 是否展示用户信息表单;user_info_has_entered: 是否已经录入表单;
             userinfo_fields: 表单字段;option_fields: 表单字段中呈现为下拉框的字段;
    """
    uid = request.GET.get('uid', '')  # 获取uid
    kind_id = request.GET.get('kind_id', '')  # 获取kind_id
    created = request.GET.get('created', '0')  # 获取标志位,以后会用到
    try:  # 获取比赛数据
        kind_info = CompetitionKindInfo.objects.get(kind_id=kind_id)
    except CompetitionKindInfo.DoesNotExist:  # 不存在渲染错误视图
        return render(request, 'err.html', CompetitionNotFound)
    try:  # 获取题库数据
        bank_info = BankInfo.objects.get(bank_id=kind_info.bank_id)
    except BankInfo.DoesNotExist:  # 不存在渲染错误视图
        return render(request, 'err.html', BankInfoNotFound)
    try:  # 获取用户数据
        profile = Profile.objects.get(uid=uid)
    except Profile.DoesNotExist:  # 不存在渲染错误视图
        return render(request, 'err.html', ProfileNotFound)
    if kind_info.question_num > bank_info.total_question_num:  # 比赛出题数量是否小于题库总大小
        return render(request, 'err.html', QuestionNotSufficient)
    show_info = get_pageconfig(kind_info.app_id).get('show_info',
                                                     {})  # 从redis获取页面配置信息
    is_show_userinfo = show_info.get('is_show_userinfo',
                                     False)  # 页面配置信息,用来控制答题前是否展示一张表单
    form_fields = collections.OrderedDict()  # 生成一个有序的用来保存表单字段的字典
    form_regexes = []  # 生成一个空的正则表达式列表
    if is_show_userinfo:
        userinfo_fields = show_info.get('userinfo_fields', '').split(
            '#')  # 从页面配置中获取userinfo_fields
        for i in userinfo_fields:  # 将页面配置的每个正则表达式取出来放入正则表达式列表
            form_regexes.append(get_form_regex(i))
        userinfo_field_names = show_info.get('userinfo_field_names',
                                             '').split('#')
        for i in range(len(userinfo_fields)):  # 将每个表单字段信息保存到有序的表单字段字典中
            form_fields.update({userinfo_fields[i]: userinfo_field_names[i]})
    return render(
        request,
        'competition/index.html',
        {  # 渲染页面
            'user_info': profile.data,
            'kind_info': kind_info.data,
            'bank_info': bank_info.data,
            'is_show_userinfo': 'true' if is_show_userinfo else 'false',
            'userinfo_has_enterd':
            'true' if get_enter_userinfo(kind_id, uid) else 'false',
            'userinfo_fields':
            json.dumps(form_fields) if form_fields else '{}',
            'option_fields': json.dumps(show_info.get('option_fields', '')),
            'field_regexes': form_regexes,
            'created': created
        })