Exemplo n.º 1
0
    def post(self):
        form = request.form
        print(form)
        content = form.get("content")
        category_id = form.get("category_id")
        right_answer = form.get("right_answer")
        options = form.get("options")
        # 判断参数是否为空
        if not content or not right_answer or not category_id:
            return error.error_1001()

        # 判断right_answer是否合法
        answers_lst = right_answer.split(",")
        for answer in answers_lst:
            if answer not in ["A", "B", "C", "D", "E"]:
                return error.error_1007()

        # 判断题目是否重复
        content_count = Question.Query().filter_by(content=content).count()
        if content_count:
            return error.error_1002("题目不能重重")

        # 将题目添加到到数据库
        question = Question(form)
        db.session.add(question)
        db.session.flush()
        # 获取题目的临时id
        question_id = question.id

        # 解析选项
        options = json.loads(options)
        for r in options:
            r["question_id"] = question_id
            # 判断label是否合法
            if not r.get("label") or r.get("label") not in [
                    "A", "B", "C", "D", "E"
            ]:
                return error.error_1007()
            # 同一题目A,B,C,D,E出现两次
            label_count = Option.Query().filter_by(question_id=question_id,
                                                   label=r["label"]).count()
            print("label_count ", label_count)
            if label_count:
                return error.error_1002(r["label"] + "重复")
            option = Option(r)
            db.session.add(option)
        db.session.commit()
        return error.success()
Exemplo n.º 2
0
	def post(self):
		form = request.form
		id = form.get("id")
		if not id:
			return error.error_1001()
		user = User.Query().filter_by(id=id).first()
		# 可以修改的信息:real_name, password, phone
		real_name = form.get("real_name")
		password = form.get("password")
		phone = form.get("phone")
		# 判断real_name是否存在
		if real_name:
			user.real_name = real_name
		# 判断password是否存在
		if password:
			user.password = bcrypt.generate_password_hash(password).decode("utf-8")
		# 判断phone是否存在
		if phone:
			phone_count = User.Query().filter(and_(User.phone == phone, User.id != id)).count()
			if phone_count:
				return error.error_1002("此手机号已存在")
			user.phone = phone

		# 提交到数据库
		db.session.add(user)
		try:
			db.session.commit()
		except:
			return error.error_1003()
		return error.success()
Exemplo n.º 3
0
	def post(self):
		form = request.form
		name = form.get("name")
		phone = form.get("phone")
		print(form)
		print(request.get_data())
		print(type(request.get_data()))
		password = form.get("password")
		openid = form.get("openid")
		#phone,password不可以为空,username可以为空
		if not phone or (not password):
			return error.error_1001()

		# 判断phone手机号是否存在
		phone_count = User.Query().filter_by(phone=phone).count()
		if phone_count:
			return error.error_1002("此手机号已存在")

		# 判断name用户名是否存在
		name_count = User.Query().filter_by(name=name).count()
		if name_count:
			return error.error_1002("此用户已存在")

		# 判断openid是否存在
		if openid:
			openid_count = User.Query().filter_by(openid=openid).count()
			if openid_count:
				return error.error_1002("此微信号已存在")

		# 提交数据到数据库
		user = User(form)
		try:
			db.session.add(user)
			db.session.commit()
		except:
			return error.error_1003()
		return error.success()
Exemplo n.º 4
0
 def post(self):
     id = request.form.get("id")
     name = request.form.get("name")
     # 判断id和name是否为空
     if not id or not name:
         return error.error_1001()
     # 查询数据库
     paper = Paper.Query().filter_by(id=id).first()
     if paper:
         # 判断name是否重复
         name_count = Paper.Query().filter(
             and_(Paper.name == name, Paper.id != id)).count()
         if name_count:
             return error.error_1002("此考试名称已存在")
         # 提交到数据库
         paper.name = name
         db.session.add(paper)
         db.session.commit()
     return error.success()
Exemplo n.º 5
0
 def post(self):
     form = request.form
     name = form.get("name")
     subject = form.get("subject")
     # 判断name和subject是否为空
     if not name or not subject:
         return error.error_1001()
     # 判断sucject是否合法
     if subject not in config.subjects:
         return error.error_1006()
     # 判断name是否存在
     paper_count = Paper.Query().filter_by(name=name).count()
     if paper_count:
         return error.error_1002("此考试名称已经存在")
     # 提交到数据库
     paper = Paper(form)
     try:
         db.session.add(paper)
         db.session.commit()
     except:
         return error.error_1003()
     return error.success()
Exemplo n.º 6
0
	def post(self):
		id = request.form.get("id")
		name = request.form.get("name")
		chapter = request.form.get("chapter")
		# 判断id是否为空
		if not id or not name:
			return error.error_1001()
		# 获取category
		category = Category.Query().filter_by(id=id).first()

		# 判处category是否存在
		if category:
			# 判断name是否存在
			category_count = Category.Query().filter(and_(Category.name == name, Category.id != id)).count()
			if category_count:
				return error.error_1002("此分类已存在")
			category.name = name
			if chapter:
				category.chapter = chapter
			db.session.add(category)
			db.session.commit()
		return error.success()
Exemplo n.º 7
0
    def post(self):
        form = request.form
        # 获取参数
        question_id = form.get("question_id")
        user_answer = form.get("user_answer")

        # 判断参数是否存在
        if not question_id or not user_answer:
            return error.error_1001()
        # 判断用户是否重复回答
        answer_count = Answer.Query().filter_by(
            user_id=g.uid, question_id=question_id).count()
        if answer_count:
            return error.error_1002("此题已经回答")

        # 查询数据库
        question = Question.Query().filter_by(id=question_id).first()
        if not question:
            return error.error_1009("题目不存在")

        is_right = user_answer == question.right_answer
        postData = {
            "question_id": question_id,
            "user_answer": user_answer,
            "user_id": g.uid,
            "is_right": is_right
        }
        answer = Answer(postData)
        db.session.add(answer)
        db.session.commit()

        result = {
            "id": answer.id,
            "is_right": is_right,
            "right_answer": question.right_answer,
            "user_answer": user_answer
        }
        return error.success(result)
Exemplo n.º 8
0
	def post(self):
		form = request.form
		name = form.get("name")
		subject = form.get("subject")
		print(form)
		# 判断name和subject是否为空
		if not name or not subject:
			return error.error_1001()
		# 同一科目下分类名称不能重复
		name_count = Category.Query().filter_by(name=name, subject=subject).count()
		if name_count:
			return error.error_1002("此分类已存在")
		# 判断subject是否合法
		if subject not in config.subjects:
			return error.error_1006()

		# 提交到数据库
		category = Category(form)
		try:
			db.session.add(category)
			db.session.commit()
		except:
			return error.error_1003()
		return error.success()