智慧校园考试比赛系统-Python+Django

     阅读:44

作者主页:编程指南针

 简介:Java领域优质创作者、CSDN博客专家  Java项目、简历模板、学习资料、面试题库、技术互助

文末获取源码

项目编号:BS-Python-002

1.项目说明

  • 项目名称:智慧答题系统
  • 项目版本:V 1.0
  • 版本变化:无
  • 完成日期:2022年1月20日

2.  系统环境

Windows或Linux发行版(Ubuntu16.04 / CentOS等)

MySQL 5.5以上版本

Python3.5以上版本

Redis任意新版本即可

Django版本2.1

本项目基于Python+Django技术开发实现,开发工具IDEA,数据库MYSQL5.7

主要实现题库设置,比赛生成,在线答题,自动排名,自动组卷,自动改卷等相关功能。

下面展示一下系统的相关功能:

用户注册

登陆

快速出题

配置比赛

上传题库

参与比赛

开始作题

自动阅卷排名

修改个人密码

查看所有比赛

以上是智慧校园比赛系统的主要内容

系统部分核心代码:

# -*- coding: utf-8 -*-

from django.contrib.auth.models import User
from django.conf import settings
from django.db import transaction
from django.views.decorators.csrf import csrf_exempt

from account.models import Profile
from business.models import BusinessAccountInfo

from utils.response import json_response
from utils.errors import BizError, UserError


def check_biz(request):
    email = request.GET.get('email', '')  # 获取邮箱
    try:  # 检查数据库中是否由该邮箱注册过的数据
        biz = BusinessAccountInfo.objects.get(email=email)
    except BusinessAccountInfo.DoesNotExist:
        biz = None
    return json_response(200, 'OK', {  # 返回是否已经被注册过和是否已经有此用户
        'userexists': User.objects.filter(email=email).exists(),
        'bizaccountexists': bool(biz)
    })


@csrf_exempt
@transaction.atomic
def registry_biz(request):
    email = request.POST.get('email', '')  # 获取填写的邮箱
    name = request.POST.get('name', '')  # 获取填写的机构名
    username = request.POST.get('username', '')  # 获取填写的机构联系人
    phone = request.POST.get('phone', '')  # 获取填写的手机号
    ctype = request.POST.get('type', BusinessAccountInfo.INTERNET)  # 获取机构类型
    flag = int(request.POST.get('flag', 2))  # 获取一个标记位,代表用户是创建新用户还是使用绑定老用户的方式
    uname = email.split('@')[0]  # 和之前的注册逻辑没什么区别,创建一个账户名
    if not User.objects.filter(username__exact=name).exists():
        final_name = username
    elif not User.objects.filter(username__exact=uname).exists():
        final_name = uname
    else:
        final_name = email
    if flag == 2:  # 如果标记位是2,那么将为他创建新用户
        user = User.objects.create_user(
            username=final_name,
            email=email,
            password=settings.INIT_PASSWORD,
            is_active=False,
            is_staff=False
        )
    if flag == 1:  # 如果标记位是1,那么为他绑定老用户
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            return json_response(*UserError.UserNotFound)
    pvalues = {
        'phone': phone,
        'name': final_name,
        'user_src': Profile.COMPANY_USER,
    }
    profile, _ = Profile.objects.select_for_update().get_or_create(email=email)  # 获取或创建用户信息
    for k, v in pvalues.items():
        setattr(profile, k, v)
    profile.save()
    bizvalues = {
        'company_name': name,
        'company_username': username,
        'company_phone': phone,
        'company_type': ctype,
    }
    biz, _ = BusinessAccountInfo.objects.select_for_update().get_or_create(  # 获取或创建机构账户信息
        email=email,
        defaults=bizvalues
    )
    return json_response(200, 'OK', {  # 响应JSON格式数据,这个标记位在发送验证邮件的时候还有用
        'name': final_name,
        'email': email,
        'flag': flag
    })

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.db import models
from django.utils.translation import ugettext_lazy as _

from shortuuidfield import ShortUUIDField
from utils.basemodels import CreateUpdateMixin


class BusinessAccountInfo(CreateUpdateMixin):
    """ 出题帐户类 """

    INTERNET = 0
    FINANCE = 1
    ENERGY = 2
    INFRASTRUCTURE = 3
    TRANSPORTATION = 4
    COMMUNICATION = 5

    TYPE_CHOICES = (
        (INTERNET, '互联网'),
        (FINANCE, '金融'),
        (ENERGY, '能源'),
        (INFRASTRUCTURE, '基础建设'),
        (TRANSPORTATION, '交通'),
        (COMMUNICATION, '通信')
    )

    account_id = ShortUUIDField(_(u'出题账户id'), max_length=32, help_text=u'出题账户唯一标识', db_index=True)

    # 帐户信息
    email = models.CharField(_(u'邮箱'), max_length=40, blank=True, null=True, help_text=u'邮箱', db_index=True, unique=True)

    # 公司信息
    company_name = models.CharField(_(u'公司名称'), max_length=60, blank=True, null=True, help_text=u'公司名称')
    company_type = models.IntegerField(_(u'公司类型'), choices=TYPE_CHOICES, default=INTERNET, help_text=u'公司类型')
    company_description = models.TextField(_(u'公司描述'), blank=True, null=True, help_text=u'公司描述')
    company_username = models.CharField(_(u'联系人'), max_length=32, blank=True, null=True, help_text=u'公司联系人')
    company_phone = models.CharField(_(u'联系电话'), max_length=16, blank=True, null=True, help_text=u'公司联系电话', db_index=True)
    company_location = models.TextField(_(u'公司位置'), blank=True, null=True, help_text=u'公司联系地址')

    class Meta:
        verbose_name = _(u'出题账户')
        verbose_name_plural = _(u'出题账户')

    def __unicode__(self):
        return str(self.pk)

    @property
    def data(self):
        return {
            'email': self.email,
            'company_name': self.company_name,
            'company_type': self.company_type,
            'company_location': self.company_location,
            'company_username': self.company_username,
            'company_phone': self.company_phone,
        }


class BusinessAppInfo(CreateUpdateMixin):
    """ 应用信息类 """

    account_id = models.CharField(_(u'出题账户id'), max_length=32, help_text=u'出题账户唯一标识', db_index=True)

    # APP 配置信息
    app_id = ShortUUIDField(_(u'应用id'), max_length=32, help_text=u'应用唯一标识', db_index=True)
    app_name = models.CharField(_(u'应用名'), max_length=40, blank=True, null=True, help_text=u'应用名')
    app_description = models.TextField(_(u'应用描述'), blank=True, null=True, help_text=u'应用描述')

    class Meta:
        verbose_name = _(u'应用信息')
        verbose_name_plural = _(u'应用信息')

    def __unicode__(self):
        return str(self.pk)

    @property
    def data(self):
        return {
            'app_id': self.app_id,
            'app_name': self.app_name,
            'account_id': self.account_id,
        }


class AppConfigInfo(CreateUpdateMixin):
    """ 应用配置信息类 """

    app_id = models.CharField(_(u'应用id'), max_length=32, help_text=u'应用唯一标识', db_index=True)
    app_name = models.CharField(_(u'应用名'), max_length=40, blank=True, null=True, help_text=u'应用名')

    # 文案配置
    rule_text = models.TextField(_(u'比赛规则'), max_length=255, blank=True, null=True, help_text=u'比赛规则')

    # 显示信息
    is_show_userinfo = models.BooleanField(_(u'展示用户表单'), default=False, help_text=u'是否展示用户信息表单')
    userinfo_fields = models.CharField(_(u'用户表单字段'), max_length=128, blank=True, null=True, help_text=u'需要用户填写的字段#隔开')
    userinfo_field_names = models.CharField(_('用户表单label'), max_length=128, blank=True, null=True, help_text=u'用户需要填写的表单字段label名称')
    option_fields = models.CharField(_(u'下拉框字段'), max_length=128, blank=True, null=True, help_text=u'下拉框字段选项配置,#号隔开,每个字段由:h和,号组成。 如 option1:吃饭,喝水,睡觉#option2:上班,学习,看电影')

    class Meta:
        verbose_name = _(u'应用配置信息')
        verbose_name_plural = _(u'应用配置信息')

    def __unicode__(self):
        return str(self.pk)

    # 页面配置数据
    @property
    def show_info(self):
        return {
            'is_show_userinfo': self.is_show_userinfo,
            'userinfo_fields': self.userinfo_fields,
            'userinfo_field_names': self.userinfo_field_names,
            'option_fields': self.option_fields,
        }

    @property
    def text_info(self):
        return {
            'rule_text': self.rule_text,
        }

    @property
    def data(self):
        return {
            'show_info': self.show_info,
            'text_info': self.text_info,
            'app_id': self.app_id,
            'app_name': self.app_name
        }


class UserInfoImage(CreateUpdateMixin):
    """
    用户表单图片配置类
    """

    uii_name = models.CharField(_(u'配置名称'), max_length=32, blank=True, null=True, help_text=u'信息图片配置名称')
    # 用户信息
    name = models.CharField(_(u'姓名'), max_length=60, blank=True, null=True, help_text=u'姓名')
    sex = models.CharField(_(u'性别'), max_length=60, blank=True, null=True, help_text=u'性别')
    age = models.CharField(_(u'年龄'), max_length=60, blank=True, null=True, help_text=u'年龄')
    phone = models.CharField(_(u'手机号'), max_length=60, blank=True, null=True, help_text=u'电话')
    wxid = models.CharField(_(u'微信号'), max_length=60, blank=True, null=True, help_text=u'微信号')
    email = models.CharField(_(u'邮箱'), max_length=60, blank=True, null=True, help_text=u'邮箱')
    pid = models.CharField(_(u'身份证号'), max_length=60, blank=True, null=True, help_text=u'身份证号')
    graduated_from = models.CharField(_(u'毕业院校'), max_length=60, blank=True, null=True, help_text=u'毕业院校')
    address = models.CharField(_(u'地址'), max_length=60, blank=True, null=True, help_text=u'联系地址')
    resume = models.CharField(_(u'简历'), max_length=60, blank=True, null=True, help_text=u'简历')

    class Meta:
        verbose_name = _(u'用户信息图片配置')
        verbose_name_plural = _(u'用户信息图片配置')

    def __unicode__(self):
        return str(self.pk)

    @property
    def data(self):
        return {
            'name': self.name,
            'sex': self.sex,
            'age': self.age,
            'phone': self.phone,
            'wxid': self.wxid,
            'email': self.email,
            'pid': self.pid,
            'graduated_from': self.graduated_from,
            'address': self.address,
        }


class UserInfoRegex(CreateUpdateMixin):
    """
    用户正则表达式配置类
    """

    field_name = models.CharField(_(u'字段名'), max_length=16, blank=True, null=True, help_text=u'字段名')
    regex = models.CharField(_(u'正则表达式值'), max_length=40, blank=True, null=True, help_text=u'正则表达式')
    description = models.CharField(_(u'description'), max_length=40, blank=True, help_text=u'错误描述')

    class Meta:
        verbose_name = _(u'用户信息字段正则表达式')
        verbose_name_plural = _(u'用户信息字段正则表达式')

    def __unicode__(self):
        return self.field_name

    @property
    def data(self):
        return {
            'field_name': self.field_name,
            'regex': self.regex,
            'description': self.description,
        }

# -*- coding: utf-8 -*-

from django.contrib import admin

from competition.models import (BankInfo, ChoiceInfo, CompetitionKindInfo,
                                CompetitionQAInfo, FillInBlankInfo)


class CompetitionKindInfoAdmin(admin.ModelAdmin):
    """
    比赛信息后台
    """

    list_display = ('kind_id', 'account_id', 'app_id', 'bank_id', 'kind_name', 'total_score', 'question_num', 'total_partin_num', 'status', 'created_at', 'updated_at')
    list_filter = ('account_id', 'status')
    search_fields = ('kind_name', 'kind_id', 'app_id', 'account_id',)
    readonly_fields = ('kind_id', 'total_partin_num',)

    def save_model(self, request, obj, form, change):
        obj.save()

    def delete_model(self, request, obj):
        obj.delete()

    def get_readonly_fields(self, request, obj=None):
        return self.readonly_fields


class BankInfoAdmin(admin.ModelAdmin):
    """
    题库后台配置
    """

    list_display = ('bank_id', 'bank_type', 'kind_num', 'choice_num', 'fillinblank_num', 'partin_num')
    list_filter = ('bank_type', 'bank_id',)
    search_fields = ('bank_id',)
    readonly_fields = ('bank_id', 'choice_num', 'fillinblank_num', 'kind_num', 'partin_num')

    def save_model(self, request, obj, form, change):
        obj.choice_num = ChoiceInfo.objects.filter(bank_id=obj.bank_id).count()
        obj.fillinblank_num = FillInBlankInfo.objects.filter(bank_id=obj.bank_id).count()
        obj.save()


class ChoiceInfoAdmin(admin.ModelAdmin):
    """
    选择题配置后台
    """

    list_display = ('bank_id', 'question', 'answer', 'item1', 'item2', 'item3', 'item4', 'source', 'status', 'created_at', 'updated_at')
    list_filter = ('bank_id', 'status')
    search_fields = ('bank_id', 'question', 'answer', 'item1', 'item2', 'item3', 'item4')

    def save_model(self, request, obj, form, change):
        obj.save()

    def delete_model(self, request, obj):
        obj.delete()


class FillInBlankInfoAdmin(admin.ModelAdmin):
    """
    填空题配置后台
    """

    list_display = ('bank_id', 'question', 'answer', 'source', 'status', 'created_at', 'updated_at')
    list_filter = ('bank_id', 'status')
    search_fields = ('bank_id', 'question', 'answer')


class CompetitionQAInfoAdmin(admin.ModelAdmin):
    """
    答题记录信息后台
    """

    list_display = ('kind_id', 'status', 'uid', 'qa_id', 'score', 'created_at', 'updated_at')
    list_filter = ('kind_id', 'uid', 'qa_id', 'started', 'finished', 'status')
    search_fields = ('uid', 'kind_id', )
    readonly_fields = ('qa_id',)


admin.site.register(CompetitionKindInfo, CompetitionKindInfoAdmin)
admin.site.register(CompetitionQAInfo, CompetitionQAInfoAdmin)
admin.site.register(ChoiceInfo, ChoiceInfoAdmin)
admin.site.register(FillInBlankInfo, FillInBlankInfoAdmin)
admin.site.register(BankInfo, BankInfoAdmin)