Exemplo n.º 1
0
def smartschool_cront_task(opaque):
    update_teachers = msettings.get_configuration_setting(
        'smartschool-update-teachers')
    update_students = msettings.get_configuration_setting(
        'smartschool-update-students')
    with flask_app.app_context():
        read_from_smartschool_database(update_teachers, update_students)
Exemplo n.º 2
0
def collect_course_data():
    from app import flask_app
    from flask import current_app

    with flask_app.app_context():
        all_course = Course.query.filter_by().all()
        for course in all_course:
            course_url = f'https://ke.qq.com/course/list?mt=1001&st={course.st}&tt={course.tt}'
            url = course_url + '&page={page}'
            resp = requests.get(url.format(page=1))
            if resp.status_code != 200:
                return
            count = 0
            # 页数
            _html = BeautifulSoup(resp.text, features='html.parser')
            page_num = _html.find_all(
                'a', class_='page-btn')[-1].text if _html.find_all(
                    'a', class_='page-btn') else 1
            count += 20 * (int(page_num) - 1)
            # 最后一页数量
            resp = requests.get(url.format(page=page_num))
            _html = BeautifulSoup(resp.text, features='html.parser')
            current_page_size = len(
                _html.find_all('li', class_='js-course-card-item'))
            count += current_page_size

            course_count = CourseCount(course_id=course.id,
                                       count=count,
                                       update_at=datetime.now().date())
            db.session.add(course_count)
            db.session.commit()

        current_app.logger.info('collect_course_data done.')
Exemplo n.º 3
0
def InsertAsync(name):
    print("Starting")
    sleep(15)
    with flask_app.app_context():
        with db_mssql.connect() as connection:
            connection.execute(f"Insert into product (product_name) values ('{name}')")
            print(f"Success")
Exemplo n.º 4
0
def update_papercut(opaque):
    with flask_app.app_context():
        try:
            update_students = msettings.get_configuration_setting(
                'papercut-update-students')
            if update_students:
                username = msettings.get_configuration_setting(
                    'papercut-login')
                password = msettings.get_configuration_setting(
                    'papercut-password')
                url = msettings.get_configuration_setting('papercut-url')
                remote_file = msettings.get_configuration_setting(
                    'papercut-file').replace('\\\\', '/')
                filename = Path(remote_file).name
                local_file = f'{PAPERCUT_LOCATION}/{filename}'
                local_temp_file = f'{PAPERCUT_LOCATION}/temp.xlsm'
                ssh_client.connect(url, username=username, password=password)
                transport = ssh_client.get_transport()
                sftp = sftp_client.SFTPClient.from_transport(transport)
                sftp.get(remote_file, local_temp_file)
                sftp.close()
                ssh_client.close()
                try:
                    file_updated = not filecmp.cmp(local_file, local_temp_file)
                except FileNotFoundError:
                    file_updated = True
                if file_updated:
                    students = mperson.get_persons(role=mperson.ROLE.STUDENT)
                    students_cache = {s.ss_internal_nbr: s for s in students}
                    lines = pyexcel.iget_records(file_name=local_temp_file)
                    nbr_updated_students = 0
                    for line in lines:
                        if str(line[HEADING_STUDENT_ID]) in students_cache:
                            student = students_cache[str(
                                line[HEADING_STUDENT_ID])]
                            if student.rfid_code != line[HEADING_BADGE]:
                                student.rfid_code = line[HEADING_BADGE]
                                mperson.update_flag(student, True)
                                nbr_updated_students += 1
                    mperson.end_update_bulk_person()
                    log.info(
                        f'papercut students: updated: {nbr_updated_students}/')
                    shutil.copyfile(local_temp_file, local_file)
        except Exception as e:
            log.error(f'papercut job task: {e}')
Exemplo n.º 5
0
def group_courses(semester="2016F", call_numbers=[]):
    with flask_app.app_context():
        return mongo_client.schedule[semester].aggregate([{
            "$match": {
                "_id": {
                    "$in": call_numbers
                }
            }
        }, {
            "$group": {
                "_id": {
                    "prefix": "$section.prefix",
                    "number": "$section.number",
                    "activity": "$activity"
                },
                "offerings": {
                    "$push": "$$ROOT"
                }
            }
        }])
Exemplo n.º 6
0
def send_email_task():
    nbr_sent_per_minute = 0
    while run_email_task:
        with flask_app.app_context():
            at_least_one_email_sent = True
            start_time = datetime.datetime.now()
            job_interval = int(
                Setting.query.filter(
                    Setting.key == 'email_task_interval').first().value)
            emails_per_minute = int(
                Setting.query.filter(
                    Setting.key == 'emails_per_minute').first().value)
            while at_least_one_email_sent:
                at_least_one_email_sent = False
                for send_email in send_email_config:
                    if run_email_task:
                        ret = send_email['function'](**send_email['args'])
                        if ret:
                            nbr_sent_per_minute += 1
                            now = datetime.datetime.now()
                            delta = now - start_time
                            if (nbr_sent_per_minute >= emails_per_minute) and (
                                    delta < datetime.timedelta(seconds=60)):
                                time_to_wait = 60 - delta.seconds + 1
                                log.info(
                                    f'send_email_task: have to wait for {time_to_wait} seconds'
                                )
                                time.sleep(time_to_wait)
                                nbr_sent_per_minute = 0
                                start_time = datetime.datetime.now()
                            at_least_one_email_sent = True
        if run_email_task:
            now = datetime.datetime.now()
            delta = now - start_time
            if delta < datetime.timedelta(seconds=job_interval):
                time_to_wait = job_interval - delta.seconds
                time.sleep(time_to_wait)
Exemplo n.º 7
0
def cleanup(socket_id):
    """
    Performs cleanup after a user leaves.

    Args:
        socket_id (str): Socket ID that was disconnected
    """
    leaving_user = UserData.query.filter_by(sid=socket_id).first()
    if leaving_user is not None:
        log.info("Got user for disconnected socket. Performing cleanup.")
        with flask_app.app_context():
            for func in clean_up_methods:
                func(leaving_user)
            room = leaving_user.room
            state_change = {}
            if room.admin == leaving_user:
                new_admin = UserData.query.filter(
                    UserData.room_id == room.id,
                    UserData.id != leaving_user.id).first()
                if new_admin is not None:
                    room.admin = new_admin
                    state_change["roomAdmin"] = new_admin.id
            db.session.delete(leaving_user)
            db.session.commit()
            state_change["userList"] = [{
                "username": user.username,
                "userId": user.id
            } for user in room.users]
            socketio.emit("set_state", state_change, room=room.id)
            if not room.users:
                db.session.delete(room)
                db.session.commit()

    else:
        log.info(
            "Did not recieve user data for socket ID. User possibly reconnected."
        )
def send_message_task():
    nbr_sent_per_minute = 0
    while run_message_task:
        with flask_app.app_context():
            at_least_one_message_sent = True
            start_time = datetime.datetime.now()
            job_interval = msettings.get_configuration_setting(
                'message-task-interval')
            messages_per_minute = msettings.get_configuration_setting(
                'message-per-minute')
            while at_least_one_message_sent:
                at_least_one_message_sent = False
                for send_message in send_message_config:
                    if run_message_task and msettings.get_configuration_setting(
                            'enable-send-message'):
                        ret = send_message['function'](**send_message['args'])
                        if ret:
                            nbr_sent_per_minute += 1
                            now = datetime.datetime.now()
                            delta = now - start_time
                            if (nbr_sent_per_minute >= messages_per_minute
                                ) and (delta < datetime.timedelta(seconds=60)):
                                time_to_wait = 60 - delta.seconds + 1
                                log.info(
                                    f'send_message_task: have to wait for {time_to_wait} seconds'
                                )
                                time.sleep(time_to_wait)
                                nbr_sent_per_minute = 0
                                start_time = datetime.datetime.now()
                            at_least_one_message_sent = True
        if run_message_task:
            now = datetime.datetime.now()
            delta = now - start_time
            if delta < datetime.timedelta(seconds=job_interval):
                time_to_wait = job_interval - delta.seconds
                time.sleep(time_to_wait)
Exemplo n.º 9
0
#!/usr/bin/env python
""" Database Update """
import argparse
from app import flask_app, db

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--courses", action="store_true")
    parser.add_argument("--degrees", action="store_true")
    parser.add_argument("--schedule", action="store_true")
    args = parser.parse_args()

    with flask_app.app_context():
        db.update(update_courses=args.courses,
                  update_degrees=args.degrees,
                  update_schedule=args.schedule)
Exemplo n.º 10
0
def pytest_sessionfinish(session, exitstatus):
    from app import flask_app, db
    with flask_app.app_context():
        db_name = db.get_db().name
        db.connection.drop_database(db_name)
    print("\n\nTearing Down Test DataBase ...")
Exemplo n.º 11
0
from app import flask_app
from app.models import db, Luggage, User

def create_users_and_luggage(db):
    user1 = User('user1', 1234)
    user2 = User('user2', 5678)

    luggage1 = Luggage('luggage1', 'ticket1', 'location1', 3, user1)
    luggage2 = Luggage('luggage2', 'ticket2', 'location2', 1, user2)

    db.session.add(user1)
    db.session.add(user2)
    db.session.add(luggage1)
    db.session.add(luggage2)

    db.session.commit()

with flask_app.app_context():
    db.drop_all()
    db.create_all()

    create_users_and_luggage(db)
Exemplo n.º 12
0
def cardpresso_cront_task(opaque):
    update_students = msettings.get_configuration_setting(
        'cardpresso-update-students')
    with flask_app.app_context():
        read_from_cardpresso_database(update_students)
Exemplo n.º 13
0
from app import flask_app
from app.models import sql_model

# 离线创建数据库表脚本

# 注意:在sql_model=SQLAlchemy()创建之前,需要先到导入所有的数据表的model模块

with flask_app.app_context():   #加载配置信息到local中,然后在create_all()中会被调用
    sql_model.create_all()

Exemplo n.º 14
0
def InsertAsync2(name):
    print("Starting")
    sleep(15)
    with flask_app.app_context():
        Product(product_name=name).insertproduct()
        print(f"Success")