Skip to content

1mht/mdb-project1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mdb-project1 — 基于 MongoDB 的在线问卷系统

数据库课程阶段 1 项目。系统支持用户注册登录、创建问卷、重复编辑题目与跳转逻辑、发布分享、收集答卷,以及查看整卷和单题统计。

技术栈

  • 后端:Python + FastAPI + PyMongo
  • 数据库:MongoDB(userssurveysresponses 三个集合)
  • 前端:Jinja2 服务端渲染 + 原生 JavaScript + Pico CSS
  • 测试:pytest + FastAPI TestClient

快速开始

前提

  • Python 3.10+
  • 本地已启动 MongoDB,默认监听 27017
  • 当前目录为项目根目录 project1
pip install -r requirements.txt

按需可新建 .env 文件覆盖默认配置:

MONGODB_URI=mongodb://localhost:27017
DB_NAME=survey_db
SECRET_KEY=replace-this-in-production
uvicorn app.main:app --reload

启动后可访问:

  • 首页:http://localhost:8000
  • API 文档:http://localhost:8000/docs

运行测试

pytest -q

测试使用独立的 survey_test 数据库,当前共 62 个用例,覆盖问卷 CRUD、重复编辑、跳转逻辑、答卷提交与统计功能。

当前实现要点

  • 统一的问卷编辑器:问卷设置、题目内容、校验规则、跳转规则在同一套前端状态里编辑
  • 题目可重复编辑:同一题可以多次打开、修改并保存
  • 跳转规则支持三种目标:已有 qid__order__:N__end__
  • 服务端限制跳转为“只能前跳”,避免自跳、倒跳和循环
  • 填写页支持 progressiveall_at_once 两种展示模式
  • 统计结果支持整卷统计和单题统计:
    • 单选题:各选项人数、总回答人数
    • 多选题:各选项被选择次数
    • 文本题:全部填写内容
    • 数字题:全部数值与平均值

配置

通过环境变量配置(可新建 .env 文件):

变量 默认值 说明
MONGODB_URI mongodb://localhost:27017 MongoDB 连接字符串
DB_NAME survey_db 数据库名
SECRET_KEY dev-secret-key-... Session 签名密钥,生产环境必须修改

项目结构

├── app/
│   ├── main.py                     # FastAPI 入口
│   ├── config.py                   # 环境变量与配置读取
│   ├── database.py                 # MongoDB 连接与集合访问
│   ├── models/                     # Pydantic 模型定义
│   ├── routes/                     # API 路由与页面路由
│   └── services/
│       ├── validation_service.py   # 校验引擎
│       ├── jump_service.py         # 跳转引擎
│       ├── response_service.py     # 答卷提交
│       └── stats_service.py        # 统计计算
├── templates/                      # Jinja2 页面模板
│   ├── base.html                   # 公共布局
│   ├── dashboard.html              # 问卷列表页
│   ├── survey_edit.html            # 问卷编辑页模板
│   ├── survey_fill.html            # 问卷填写页模板
│   └── survey_stats.html           # 统计页模板
├── static/
│   ├── css/
│   │   ├── style.css               # 通用与填写页样式
│   │   └── survey_edit.css         # 编辑页样式
│   └── js/
│       ├── fill.js                 # 前端填写/跳转引擎
│       └── survey_edit.js          # 统一问卷编辑器
├── tests/                          # pytest 测试套件
│   ├── test_surveys.py             # 问卷管理测试
│   ├── test_questions.py           # 题目与跳转编辑测试
│   ├── test_responses.py           # 答卷提交测试
│   └── test_stats.py               # 统计功能测试
├── docs/                           # 开发者文档
└── local/                          # 本地说明与非交付材料

API 文档

启动服务后访问 http://localhost:8000/docs

文档

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors