Beispiel #1
0
def book():
    if BookRec.get_stu_book_count_now(current_user.stu_id) >= BOOKING.MAX_BOOK:
        # Clear session
        session.pop('vercode', None)
        logger.info('Try to initiate book exceeding limit')
        abort(409, 'Sponsor Exceeded')
    book_form = BookForm()
    room_id, raw_ids, students, date, start, end = book_form.validate()
    checking = book_form.test.data
    raw_stu_id = request.args.get('rawId')
    if not raw_stu_id or cryptor.hashit(raw_stu_id) != current_user.stu_id:
        abort(403, 'Bad Authorization')
    raw_ids = [raw_stu_id] + raw_ids
    students = [current_user.stu_id] + students
    students = list(dict((s, None) for s in students))
    # if len(students) != 3:
    #     logger.info('Duplicate invitation: %s', students)
    #     abort(400, 'Duplicate')
    if not is_valid_booking_time(room_id, date, start, end):
        # is_valid_booking_time shall handle logging stuff
        abort(400, 'Invalid Time')
    if checking:
        return jsonify(message='Vercode needed')

    book_id = BookRec.commit_booking(
        zip(students, raw_ids), room_id, date, start, end,
        book_form.sponsor.data, book_form.contact.data,
        book_form.stu_num.data, book_form.description.data)
    BookRec.stu_confirm(current_user.stu_id, book_id)
    # for stu_id in students:
    #     BookRec.stu_confirm(stu_id, book_id)
    BookRec.query.get(book_id).update_confirm()
    logger.info('Book %s initiated with %s', book_id, students)
    return jsonify(message='Successfully Booked')
Beispiel #2
0
def vercode():
    '''send twice verification code to a login user via GET'''
    raw_stu_id = request.args.get('rawId')
    if not raw_stu_id or not cryptor.hashit(raw_stu_id) == request.args.get(
            'id'):
        abort(403, 'Bad Authorization')
    session['vercode'] = cryptor.encrypt(get_send_vercode(raw_stu_id))
    return jsonify(message='Vercode Sent')
Beispiel #3
0
def add(stu_ids):
    '''
    添加单个学生
    '''
    from bookB116 import cryptor, db
    from bookB116.database import Student

    inserter = Student.__table__.insert().prefix_with('IGNORE')

    for stu_id in stu_ids:
        db.session.execute(inserter.values(stu_id=cryptor.hashit(stu_id)))

    db.session.commit()
Beispiel #4
0
def app():
    app = create_app()
    app.app_context().push()
    if not STU_IDS:
        STU_IDS.extend([cryptor.hashit(i) for i in RAW_STU_IDS])
    db.create_all()
    try:
        for stu_id in STU_IDS:
            db.session.add(Student(stu_id=stu_id))
        db.session.commit()
    except Exception as e:
        db.session.close()
        db.drop_all()
        raise e

    yield app
    db.session.close()
    db.drop_all()
Beispiel #5
0
def build(path):
    '''
    Build student databse from csv file.

    通过一系列csv文件建立一个物院学生的数据库。
    数据库只要求存储姓名和学号之间对应关系,暂不涉及入学年份。

    The csv file should have a header
    '''
    from csv import reader

    from bookB116 import cryptor, db
    from bookB116.database import Student

    # There is no short hand method to IGNORE duplicate ids
    # session.add will raise an exception when duplicate
    # and session.merge will update the record,
    # which will erase other field and that's terrible.
    # And checking if the student exists every time is slow.
    # The code below creates an INSERT IGNORE sentence,
    # adding 2000 records in 3 sec in my VM
    inserter = Student.__table__.insert().prefix_with('IGNORE')

    for csv_file in path:
        with open(csv_file, encoding='utf-8-sig', newline='') as csvin:
            csvreader = reader(csvin)
            next(csvreader)  # Remove header line
            for row in csvreader:
                stu_id = row[0]
                if stu_id == '':
                    logger.info('Interesting empty lines by Excel')
                    continue
                db.session.execute(
                    inserter.values(stu_id=cryptor.hashit(stu_id)))
    db.session.commit()
    logger.info('Successfully built database ' 'from %s', ''.join(path))
Beispiel #6
0
 def add_raw_id(cls, raw_student_id: str):
     inserter = Student.__table__.insert().prefix_with('IGNORE')
     db.session.execute(inserter.values(
         stu_id=cryptor.hashit(raw_student_id)))
     db.session.commit()
     return cls.by_raw_id(raw_student_id)
Beispiel #7
0
 def by_raw_id(cls, raw_student_id: str):
     return cls.query.get(cryptor.hashit(raw_student_id))