def delete(e):
    try:
        delUser = request.get_json()
        user_id = delUser.get('delUserID')
        User.deleteRecord(user_id)

        return jsonify({'status': 'success'}), 200
    except Exception as e:
        return jsonify({'status': 'bad-request', 'error_message': e.__str__()}), 400
def create(e):
    try:
        new_student = request.get_json()
        code = new_student.get('new_code')
        username = new_student.get('new_username')
        name = new_student.get('new_name')
        email = new_student.get('new_email')
        dob = new_student.get('new_dob')
        class_course = new_student.get('new_class_cource')
        new_course_id = new_student.get('new_course_id')
        permission = 'Sinh viên'
        create_at = datetime.now(
            pytz.timezone('Asia/Ho_Chi_Minh')).strftime("%Y-%m-%d %H:%M:%S")
        actived = new_student.get('new_actived')
        is_lock = new_student.get('new_is_lock')

        print(request.get_json())
        isStudentCreated = User.createRecord(
            str(username).strip(),
            str(name).strip(),
            str(email).strip(), create_at, permission, actived, is_lock,
            str(code).strip(), dob,
            str(class_course).strip(), new_course_id)
        if isStudentCreated is True:
            return jsonify({'status': 'success'}), 200
        else:
            return jsonify({'status': 'already-exist'}), 202
    except Exception as e:
        return jsonify({
            'status': 'bad-request',
            'error_message': e.__str__()
        }), 400
Пример #3
0
def sign_in():
    user_form = request.get_json()
    username = user_form.get('username')
    check_username = re.search('[!#$%^&*()=' ',?";:{}|<>]', username)
    password = user_form.get('password')
    check_password = re.search('[!#$%^&*()=' ',?";:{}|<>]', username)
    if (check_username is None) and (check_password is None):
        check_user = User.checkUser(username, password)
        if check_user == 'Not found':
            return jsonify({'message': 'not found'}), 401
        elif check_user == 'Student':
            return jsonify({'message': 'student-unauthorized'}), 401
        else:
            token = jwt.encode(
                {
                    'sub': username,  # representing username
                    'iat': datetime.utcnow(),  # issued at timestamp in seconds
                    'exp': datetime.utcnow() + timedelta(minutes=480)
                },
                # the time in which the token will expire as seconds
                current_app.config['SECRET_KEY'])
            return jsonify({
                'type': check_user[1],
                'message': 'login successful',
                'token': token.decode('UTF-8')
            }), 200
    else:
        return jsonify({'message': 'unauthorized'}), 401
Пример #4
0
    def _verify(*args, **kwargs):
        auth_headers = request.headers.get('Authorization', '').split()
        invalid_msg = {
            'message':
            'Invalid token. Registeration and / or authentication required',
            'authenticated': False
        }
        expired_msg = {
            'message': 'Expired token. Authentication is required.',
            'authenticated': False
        }

        if len(auth_headers) != 2:
            return jsonify(invalid_msg), 401

        try:
            token = auth_headers[1]
            data = jwt.decode(token, current_app.config['SECRET_KEY'])
            user_data = User.getUser(data['sub'])
            if user_data is None:
                raise RuntimeError('User not found')
            print(user_data, flush=True)
            return f(user_data, *args, **kwargs)
        except jwt.ExpiredSignatureError:
            return jsonify(
                expired_msg), 401  # 401 is Unauthorized HTTP status code
        except (jwt.InvalidTokenError, Exception) as e:
            print(e, flush=True)
            return jsonify(invalid_msg), 401
def search_record(e):
    try:
        searchUsername = request.args.get('searchUsername')
        searchRecord = User.searchUserRecord(str(searchUsername))

        return jsonify({
            'status': 'success',
            'search_results': searchRecord,
        }), 200
    except:
        return jsonify({'status': 'bad-request'}), 400
def get_records(e):
    try:
        page_index = request.args.get('page_index')
        per_page = request.args.get('per_page')
        sort_field = request.args.get('sort_field')
        sort_order = request.args.get('sort_order')
        record = User.getRecord(page_index, per_page, sort_field, sort_order)

        return jsonify({
            'status': 'success',
            'records': record[0],
            'page_number': record[1].page_number,
            'page_size': record[1].page_size,
            'num_pages': record[1].num_pages,
            'total_results': record[1].total_results
        }), 200
    except:
        return jsonify({'status': 'bad-request'}), 400
def create(e):
    try:
        new_user = request.get_json()
        username = new_user.get('new_username')
        name = new_user.get('new_name')
        email = new_user.get('new_email')
        permission = new_user.get('new_permission')
        create_at = datetime.now(pytz.timezone('Asia/Ho_Chi_Minh')).strftime("%Y-%m-%d %H:%M:%S")
        actived = new_user.get('new_actived')
        is_lock = new_user.get('new_is_lock')

        isCreated = User.createRecord(str(username).strip(), str(name).strip(), str(email).strip(), create_at,
                                      str(permission).strip(), actived, is_lock, None, None, None, None)
        if isCreated is True:
            return jsonify({'status': 'success'}), 200
        else:
            return jsonify({'status': 'already-exist'}), 202
    except Exception as e:
        return jsonify({'status': 'bad-request', 'error_message': e.__str__()}), 400
def update_record(e):
    try:
        new_update = request.get_json()
        user_id = new_update.get('user_id')
        username = new_update.get('update_username')
        name = new_update.get('update_name')
        permission = new_update.get('update_permission')
        email = new_update.get('update_email')
        updated_at = datetime.now(pytz.timezone('Asia/Ho_Chi_Minh')).strftime("%Y-%m-%d %H:%M:%S")
        actived = new_update.get('update_actived')
        is_lock = new_update.get('update_is_lock')

        isUpdated = User.updateRecord(int(user_id), str(username).strip(), str(name).strip(), str(email).strip(),
                                      updated_at, actived, is_lock, str(permission).strip())
        if isUpdated is True:
            return jsonify({'status': 'success'}), 200
        else:
            return jsonify({'status': 'already-exist'}), 202
    except Exception as e:
        return jsonify({'status': 'bad-request', 'error_message': e.__str__()}), 400
def create(e):
    try:
        new_lecture = request.get_json()
        username = new_lecture.get('new_username')
        name = new_lecture.get('new_name')
        email = new_lecture.get('new_email')
        create_at = datetime.now(
            pytz.timezone('Asia/Ho_Chi_Minh')).strftime("%Y-%m-%d %H:%M:%S")
        permission = 'Giảng viên'
        actived = new_lecture.get('new_actived')
        is_lock = new_lecture.get('new_is_lock')

        isCreated = User.createRecord(
            str(username).strip(),
            str(name).strip(),
            str(email).strip(), create_at, permission, actived, is_lock, '',
            '', '', '')
        if isCreated is True:
            return jsonify({'status': 'success'}), 200
        else:
            return jsonify({'status': 'already-exist'}), 202
    except:
        return jsonify({'status': 'bad-request'}), 400
def import_excel(e):
    excel_file = request.files['student_list_excel']
    new_students = []
    update_students = []
    error_students = []
    course_id = request.form.get('course_id')
    try:
        if excel_file.content_type == 'application/vnd.ms-excel':  # xls file type
            # load data
            data = pandas.read_excel(request.files['student_list_excel'],
                                     encoding='ISO-8859-1')

            processed_data = json.dumps(data, ensure_ascii=False)

            excel_json = json.loads(processed_data)
            for item in excel_json['DSLMH']:
                print(item)
                print('\n')
            return jsonify({'status': 'success'}, excel_json), 200
        elif excel_file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':  # xlsx file type
            data = load_workbook(excel_file)

            sheet = data.active
            # get max row count
            max_row = sheet.max_row + 1
            # get max column count
            max_column = sheet.max_column + 1
            print('BEBE')
            for i in range(12, max_row):
                student = {
                    'STT': None,
                    'Mã SV': None,
                    'Họ và tên': None,
                    'Ngày sinh': None,
                    'Lớp khóa học': None,
                    'Ghi chú': None,
                }
                # Iterate over the dict and all the columns
                for j, index in zip(range(1, max_column), student):
                    # add data to excel_data dict
                    student[index] = sheet.cell(row=i, column=j).value
                print(student, flush=True)
                # add students to database
                # check validation
                if student['STT'] is not None and student['Mã SV'] is not None:
                    check_code = re.search(
                        '^\d{8}$',
                        str(student['Mã SV']).replace(' ', ''))
                    check_name = re.search(
                        "^[a-zA-Z_ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶ"
                        +
                        "ẸẺẼỀẾỂưăạảấầẩẫậắằẳẵặẹẻẽềếểỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợ"
                        + "ụủứừỬỮỰỲỴÝỶỸửữựỳýỵỷỹ\s ]+$",
                        str(student['Họ và tên']))
                    check_dob = re.search(
                        '^(((0)[1-9])|((1)[0-2]))(\/)([0-2][0-9]|(3)[0-1])(\/)\d{4}',
                        str(
                            datetime.strptime(
                                student['Ngày sinh'],
                                '%d/%m/%Y').strftime('%m/%d/%Y')))
                    # gender = re.search('(Nam|Nữ)', str(excel_data['gender']))
                    # courseID = re.search('^[K|k][1-9][0-9][A-Za-z]+[1-9]*', str(excel_data['courseID']).replace(' ', ''))
                    # subjectID = re.search('(^(([A-Z]|[a-z]){3})([1-9][(0-9)]{3})$)',
                    #                       str(excel_data['subjectID']).replace(' ', ''))
                    # status = re.search('(đủ điều kiện|không đủ điều kiện)', excel_data['status'].lower())
                    print(check_code, flush=True)
                    print(check_name, flush=True)
                    print(check_dob, flush=True)
                    # print(gender, flush=True)
                    # print(courseID, flush=True)
                    # print(subjectID, flush=True)
                    # print(status, flush=True)

                    if (check_code
                            is not None) and (check_name
                                              is not None) and (check_dob
                                                                is not None):
                        isStudentCreated = User.createRecord(
                            str(student['Mã SV']).strip(),
                            str(student['Họ và tên']).strip(),
                            str(student['Mã SV']) + '@vnu.edu.vn',
                            datetime.now(
                                pytz.timezone('Asia/Ho_Chi_Minh')).strftime(
                                    "%Y-%m-%d %H:%M:%S"), 'Sinh viên', 1, 0,
                            str(student['Mã SV']).strip(),
                            datetime.strptime(
                                student['Ngày sinh'],
                                '%d/%m/%Y').strftime("%Y-%m-%d %H:%M:%S"),
                            str(student['Lớp khóa học']).strip(), course_id)
                        if isStudentCreated is True:
                            new_students.append(student)
                        else:
                            current_student = Student.searchStudentRecord(
                                str(student['Mã SV']).strip())[0]
                            Student.updateRecord(
                                current_student['user']['user_id'],
                                current_student['student_id'],
                                str(student['Mã SV']).strip(),
                                str(student['Mã SV']).strip(),
                                str(student['Họ và tên']).strip(),
                                str(student['Mã SV']) + '@vnu.edu.vn',
                                datetime.strptime(
                                    student['Ngày sinh'],
                                    '%d/%m/%Y').strftime("%Y-%m-%d %H:%M:%S"),
                                str(student['Lớp khóa học']).strip(),
                                course_id,
                                datetime.now(pytz.timezone('Asia/Ho_Chi_Minh')
                                             ).strftime("%Y-%m-%d %H:%M:%S"),
                                1, 0)

                            update_students.append(student)
                    else:
                        error_students.append(student)

            return jsonify({
                'status': 'success',
                'new_students': new_students,
                'error_students': error_students,
                'updated_students': update_students,
            }), 200
        else:
            return jsonify({
                'status':
                'bad-request',
                'error_message':
                excel_file + ' không đúng định dạng xlsx hoặc xls'
            }), 400
    except Exception as e:
        return jsonify({
            'status': 'bad-request',
            'error_message': e.__str__()
        }), 400