Пример #1
0
def add_user():
    if current_user.email != 'developer':
        return json.dumps({'code': 1, 'msg': 'Access denied for user.'})
    email = request.form.get('email', '')
    password = request.form.get('password', '')
    group_id = request.form.get('group_id', -1)
    if not (email and password):
        email = request.args.get('email', '')
        password = request.args.get('password', '')
        group_id = request.args.get('group_id', -1)
    if not (email and password):
        return json.dumps({
            'code':
            1,
            'msg':
            'miss email or password. email= %s, password=%s' %
            (email, password)
        })
    user = AdminUser.query.filter_by(email=email).first()
    if not user:
        db.session.add(AdminUser(email, utils.hash_pwd(password), group_id))
        db.session.commit()
        return json.dumps({
            'code':
            0,
            'msg':
            'success. email= %s, password=%s' % (email, password)
        })
    else:
        return json.dumps({'code': 1, 'msg': 'user is exist.'})
Пример #2
0
def login():
    email = request.form.get("email")
    password = request.form.get("password")
    if not (email and password):
        email = request.args.get('email', '')
        password = request.args.get('password', '')
    pwd = utils.hash_pwd(password)
    user = AdminUser.query.filter_by(email=email).first()
    if not user:
        return json.dumps({'code': 1, 'msg': 'The user does not exist'})
    elif user.password != pwd and user.password != password:
        return json.dumps({'code': 2, 'msg': 'password error.'})

    login_user(user)
    return json.dumps({'code': 0, 'data': user.to_admin_dict()})
Пример #3
0
def update_password():
    if current_user.email != 'developer':
        return json.dumps({'code': 1, 'msg': 'Access denied for user.'})
    password = request.args.get('password', '')
    user_id = request.args.get('user_id', 0, int)
    if user_id and password:
        au = AdminUser.query.filter_by(id=user_id).first()
        if au:
            au.password = utils.hash_pwd(password)
            db.session.add(au)
            db.session.commit()
            return json.dumps({'code': 0, 'msg': 'ok.'})
        else:
            return json.dumps({
                'code': 1,
                'msg': 'user_id: %s, is not exist.' % (user_id)
            })
    else:
        return json.dumps({
            'code':
            1,
            'msg':
            'error, user_id: %s, password: %s' % (user_id, password)
        })
Пример #4
0
    def __init__(self):
        print('\n' + '注册中'.center(20, '-'))
        #  取出所有学校对象
        school_lst = School.get_all_objects()
        if not school_lst:
            print('\033[1;35m 对不起,目前没有校区 \033[0m')
            return
        while True:
            print()
            print('分校列表'.center(30, '-'))
            print('学校名称'.ljust(10), '地址'.ljust(15))
            for school in school_lst:
                print(
                    str(school.name).ljust(10),
                    str(school.address).ljust(15))
            # 1. 选择学校
            school_name = input('请选择学校(退出:q)>>> ').strip()
            if school_name == 'q':  # 选择退出,返回主界面
                return
            if not School.get_obj_by_name(school_name):  # 判断学校名称输入是否有误
                print('\033[1;35m 学校名称输入有误,请重新输入 \033[0m')
            else:
                # 2. 取出学校对象
                school = School.get_obj_by_name(school_name)

                # 3. 取出学校的多有班级
                if not school.school_classes:
                    print('\033[1;35m 对不起,当前学校没有创建班级 \033[0m')
                else:
                    self.school_id = school.id_
                    school.display_school_classes()  # 展示班级列表
                    # 获取当前学习的所有班级
                    class_name_lst = [
                        class_.name for class_ in school.school_classes
                    ]  # 班级名称列表
                    while True:
                        # 4. 选择要加入的班级
                        class_name = input('请选择班级 >>> ').strip()
                        if class_name not in class_name_lst:
                            print('\033[1;35m 班级名输入有误,请重新输入 \033[0m')

                        else:
                            # 5. 获取班级对象
                            class_ = Class.get_obj_by_name(class_name)
                            if not class_:  # 通过名称获取班级
                                print('\033[1;35m 班级名称有误,请重新输入 \033[0m')

                            else:
                                while True:
                                    # 6. 输入密码
                                    login_pwd = input(
                                        '请设置登录密码(至少六位数)>>> ').strip()
                                    if len(login_pwd
                                           ) < 6 or not login_pwd.isalnum():
                                        print(
                                            '\033[1;35m密码至少需要六位字母或数字 \033[0m')
                                    else:
                                        self.login_pwd = hash_pwd(login_pwd)
                                        self.active = 0
                                        self.class_id = Class.get_obj_by_name(
                                            class_name).id_
                                        # 7. 输入姓名
                                        name = input(
                                            '输入姓名(中文名字优先) >>> ').strip()
                                        # 判断是否存在重复姓名
                                        student_name_lst = [
                                            stu.name for stu in
                                            class_.class_students_info
                                        ]
                                        if name in student_name_lst:
                                            print('\033[1;35m 学生姓名重复 \033[0m')
                                        else:
                                            # 8. 输入年龄和性别等信息
                                            age = input(
                                                '年龄(必须为数字) >>> ').strip()
                                            gender = input(
                                                '性别(男|女) >>> ').strip()
                                            if not name or gender not in (
                                                    '男',
                                                    '女') or not age.isdigit():
                                                print(
                                                    '\033[1;35m 名字,性别(male|female)或者年龄输入有误 \033[0m',
                                                    end='\n\n')

                                            else:
                                                super().__init__(name)
                                                self.age = age
                                                self.gender = gender
                                                while True:
                                                    # 9. 输入联系方式
                                                    mobile = input(
                                                        '联系方式 >>> ').strip()
                                                    if not re.match(
                                                            '1[358]\d{9}',
                                                            mobile):
                                                        print(
                                                            '\033[1;35m电话格式有误 \033[0m'
                                                        )
                                                    else:
                                                        self.mobile = mobile
                                                        address = input(
                                                            '请输入您的住址>>> '
                                                        ).strip()
                                                        if not address:
                                                            print(
                                                                '\033[1;35m住址不能为空 \033[0m'
                                                            )
                                                        else:
                                                            self.address = address
                                                            print(
                                                                '%s同学,恭喜您注册成功!'
                                                                % name)
                                                            print()
                                                            self.save()
                                                            log_generate(
                                                                log_type=
                                                                'student',
                                                                id=self.id_,
                                                                message={
                                                                    'type':
                                                                    '注册',
                                                                    'name':
                                                                    self.name,
                                                                    'school':
                                                                    school.
                                                                    name,
                                                                    'course':
                                                                    self.name,
                                                                    'class':
                                                                    class_name,
                                                                    'address':
                                                                    address
                                                                })
                                                            return
Пример #5
0
    def __init__(self, school):
        print()
        print('招聘讲师中'.center(20, '-'))
        while True:
            # 1. 输入课程
            teaching_course = input('擅长课程(退出:q)>>> ').strip()
            if teaching_course == 'q':
                return
            if teaching_course not in HOT_COURSES:  # 只招收特定课程的教师
                print(
                    "对不起,您的课程不符合招聘要求['python', 'linux', 'go', 'java', 'php', 'c', 'c++']"
                )

            else:
                # 2. 输入教龄
                teaching_years = input('经验(年) >>> ').strip()
                if not teaching_years.isdigit() or int(
                        teaching_years) not in range(1, 50):
                    print('\033[1;35m对不起,我们招聘的教师至少需要2年工作经验 \033[0m')
                else:
                    while True:
                        # 3. 输入姓名
                        name = input('姓名(不能为空, 退出:q) >>> ').strip()
                        if name == 'q':
                            return

                        if school.school_teachers:
                            teacher_name_lst = [
                                teacher.name
                                for teacher in school.school_teachers
                                if school.school_teachers
                            ]
                            if name in teacher_name_lst:
                                print('\033[1;35m 对不起,该教师已经招聘 \033[0m')
                                continue

                        # 4. 输入年龄, 性别
                        age = input('年龄(数字) >>> ').strip()
                        gender = input('性别(男|女) >>> ').strip()
                        if not name or not age.isdigit() or gender not in (
                                '男', '女'):
                            print('\033[1;35m姓名或性别输入有误\033[0m')
                        else:
                            while True:
                                # 5. 输入密码
                                login_pwd = input(
                                    '请输入您的登录密码(至少六位数)>>> ').strip()
                                if len(login_pwd) < 6 or not login_pwd.isalnum(
                                ):
                                    print('\033[1;35m密码至少需要六位字母或数字 \033[0m')

                                else:
                                    # 6. 对象属性赋值及保存, 保存至对应的路径
                                    super().__init__(name)
                                    self.login_pwd = hash_pwd(login_pwd)
                                    self.school_id = school.id_
                                    self.age = age
                                    self.gender = gender
                                    self.teaching_course = teaching_course
                                    self.teaching_years = teaching_years
                                    print('招聘讲师成功'.center(20, '-'))
                                    self.save()
                                    log_generate(log_type='admin',
                                                 id='admin',
                                                 message={
                                                     'type': '招收教师',
                                                     '教师姓名': name,
                                                     '性别': gender,
                                                     '教授课程': teaching_course,
                                                     '经验(年)':
                                                     int(teaching_years)
                                                 })
                                    return