Esempio n. 1
0
    def register():
        try:
            request_data = request.data
            request_data = json.loads(request_data)
        except:
            return jsonify({
                "message": "Không thể lấy dữ liệu!",
                "code": 412
            }), 412
        username = request_data.get("username")
        password = request_data.get("password")
        permission = request_data.get("permission", "sinhvien")

        password_md5 = generate_md5_by_str(password)

        check_user_name = UserModel.find_username(username=username)
        if check_user_name:
            return jsonify({
                "message": f"Tài khoản {username} đã tồn tại!",
                "code": 413
            }), 413
        lienket = None
        if permission == "sinhvien":
            student_data = {
                "Ten": request_data.get("ten"),
                "MaSV": request_data.get("masv"),
                "SDT": request_data.get("sdt", ""),
                "Email": request_data.get("email", ""),
                "IDLop": request_data.get("idlop", ""),
                "MaGVHD": "",
                "IDDeTai": "",
                "TrangThai": "TaoMoi",
                "ThoiGianTao": get_current_time(),
                "ThoiGianCapNhat": get_current_time()
            }

            lienket = StudentModel.insert_one_student(data=student_data)
        if permission == "giaovien":
            teacher_data = {
                "Ten":
                request_data.get("ten"),
                "SoLuong":
                0,
                "MaGV":
                request_data.get("magv"),
                "SDT":
                request_data.get("sdt"),
                "Email":
                request_data.get("email"),
                "ChucVu":
                request_data.get("chucvu")
                if request_data.get("chucvu") else "GiaoVien",
                "TrangThai":
                "",
                "ThoiGianTao":
                get_current_time(),
                "ThoiGianCapNhat":
                get_current_time()
            }

            lienket = TeacherModel.insert_many_teacher(teacher_data)
        user_data = {
            "TaiKhoan": username,
            "MatKhau": str(password_md5),
            "Quyen": permission,
            "LienKet": lienket,
            "ThoiGianTao": get_current_time(),
            "ThoiGianCapNhat": get_current_time()
        }
        user_id = UserModel.insert_user(data_insert=user_data)
        if not user_id:
            return jsonify({
                "message": "Tạo mới thông tin tài khoản thành công!",
                "code": 413
            }), 413

        data = {"username": username, "permission": permission}
        return jsonify({
            "message": "Tạo mới tài khoản thành công!",
            "data": data,
            "code": 200
        }), 200
Esempio n. 2
0
def create_data(n=10):
    all_data = list()
    for i in range(1, n):
        email = f"sinhvien{i}[email protected]"
        password = generate_md5_by_str(default_pass)
        student_data = {
            "Ten": "Nguyen Van A " + str(i),
            "MaSV": "Ma101152" + str(i),
            "SDT": "03133312323" + str(i),
            "Email": email,
            "IDLop": 1,
            "MaGVHD": "",
            "IDDeTai": "",
            "Cap": random.randrange(1, 3, 1),
            "TrangThai": "TaoMoi",
            "ThoiGianTao": get_current_time(),
            "ThoiGianCapNhat": get_current_time()
        }
        student_id = StudentModel.insert_one_student(student_data)
        user_data = {
            "TaiKhoan": email,
            "MatKhau": str(password),
            "Quyen": "sinhvien",
            "LienKet": student_id,
            "ThoiGianTao": get_current_time(),
            "ThoiGianCapNhat": get_current_time()
        }
        user_id = UserModel.insert_user(data_insert=user_data)
    for i in range(1, n):
        email = f"giaovien{i}[email protected]"
        password = generate_md5_by_str(default_pass)
        teacher_data = {
            "Ten": "Le Van A" + str(i),
            "SoLuong": 0,
            "MaGV": f"Gv12983192{i}",
            "SDT": f"19831321312{i}",
            "Email": email,
            "ChucVu": "GiaoVien",
            "TrangThai": "",
            "ThoiGianTao": get_current_time(),
            "ThoiGianCapNhat": get_current_time()
        }
        teacher_id = TeacherModel.insert_one_teacher(teacher_data)
        user_data = {
            "TaiKhoan": email,
            "MatKhau": str(password),
            "Quyen": "giaovien",
            "LienKet": teacher_id,
            "ThoiGianTao": get_current_time(),
            "ThoiGianCapNhat": get_current_time()
        }
        user_id = UserModel.insert_user(data_insert=user_data)

    email = f"*****@*****.**"
    password = generate_md5_by_str(default_pass)
    teacher_data = {
        "Ten": "Truong khoa",
        "SoLuong": 0,
        "MaGV": "Tk1231231",
        "SDT": "192831892313",
        "Email": email,
        "ChucVu": "TruongKhoa",
        "TrangThai": "",
        "ThoiGianTao": get_current_time(),
        "ThoiGianCapNhat": get_current_time()
    }
    teacher_id = TeacherModel.insert_one_teacher(teacher_data)
    user_data = {
        "TaiKhoan": email,
        "MatKhau": str(password),
        "Quyen": "truongkhoa",
        "LienKet": teacher_id,
        "ThoiGianTao": get_current_time(),
        "ThoiGianCapNhat": get_current_time()
    }
    user_id = UserModel.insert_user(data_insert=user_data)