Exemplo n.º 1
0
    def submit_job(file_bytes, config):
        """
        传输文件脚本
        :param file_bytes: 文件
        :param config:
        :return:
        """
        try:
            config = eval(config)
            job = Job()
            job.name = config['name']
            job.job_id = config['job_id']
            job.cron = config['cron']
            job.type = 1 if 'type' not in config else config['type']
            job.instance_cnt = 1 if 'instance_cnt' not in config else config[
                'instance_cnt']
        except Exception as e:
            raise ServiceException(ErrorCode.PARAM_ERROR.value, '配置信息不正确')

        check_exist = session.query(Job).filter(
            Job.job_id == job.job_id).first()
        if check_exist is not None:
            raise ServiceException(ErrorCode.NOT_FOUND, '重复的job_id')

        file_name = os.path.dirname(
            os.path.abspath(__file__)) + '/../job/' + job.job_id + '.py'

        with open(file_name, 'wt') as f:
            f.write(file_bytes)

        session.add(job)
        job.create_time = datetime.now()
        session.commit()
        return job.job_id
Exemplo n.º 2
0
def update_user(username, real_name, point):
    acre_info = get_user_by_username(username)
    acre_info.real_name = real_name
    acre_info.point = point
    acre_info.executed_times += 1
    session.add(acre_info)
    session.commit()
Exemplo n.º 3
0
def update_info(info):
    """
    插入或更新信息
    :param info:
    :return:
    """
    username = info['username']
    leetcode_info = get_info_by_username(username)
    if leetcode_info is None:
        leetcode_info = LeetcodeInfo()
        leetcode_info.create_time = datetime.now()
    leetcode_info.username = info['username']
    leetcode_info.password = info['password']
    leetcode_info.user_slag = info['user_slag']
    leetcode_info.real_name = info['real_name']
    leetcode_info.avatar = info['avatar']
    leetcode_info.location = info['location']
    leetcode_info.school = info['school']
    leetcode_info.finished_contests = info['finished_contests']
    leetcode_info.rating = info['rating']
    leetcode_info.global_rank = info['global_ranking']
    leetcode_info.solved_question = info['solved_question']
    leetcode_info.accepted_submission = info['accepted_submission']
    leetcode_info.points = info['points']
    leetcode_info.executed_times += 1
    leetcode_info.status = LeetcodeInfo.Status.RUNNING.value
    session.add(leetcode_info)
    session.commit()
Exemplo n.º 4
0
def delete_notebook_by_id(notebook_id):
    to_delete_notebook = session.query(Notebook) \
                                .filter_by(id=notebook_id) \
                                .first()
    user_id = to_delete_notebook.user_id
    index = to_delete_notebook.index
    session.delete(to_delete_notebook)
    session.commit()
    shift_notebooks(user_id, index + 1, maxint, True)
Exemplo n.º 5
0
def setup():
    session.add(User(**dict(password="******")))
    for index in xrange(1, 51):
        new_notebook = Notebook(**dict(
            user_id=2,
            name="notebook_name",
            index=index))
        session.add(new_notebook)
    session.commit()
Exemplo n.º 6
0
def modify_notebook_position(notebook_id, index):
    to_modify_notebook = find_notebook_by_id(notebook_id)
    user_id = to_modify_notebook.user_id
    if to_modify_notebook.index > index:
        shift_notebooks(user_id, index, to_modify_notebook.index)
        to_modify_notebook.index = index
    elif to_modify_notebook.index < index:    
        shift_notebooks(user_id, to_modify_notebook.index + 1, index + 1, True)
        to_modify_notebook.index = index
    session.commit()    
Exemplo n.º 7
0
def shift_notebooks(user_id, _from, to, back=False):
    to_shift_notebooks = session.query(Notebook).filter(
        Notebook.user_id==user_id,
        Notebook.index>=_from,
        Notebook.index<to)
    for notebook in to_shift_notebooks:
        if back:
            notebook.index -= 1
        else:    
            notebook.index += 1
    session.commit()
Exemplo n.º 8
0
 def insert_job(job):
     if not job or not job.job_id or not job.name:
         raise ServiceException(ErrorCode.PARAM_ERROR,
                                'job参数错误,job_id或name不能为空')
     if session.query(Job).filter(
             Job.job_id == job.job_id).first() is not None:
         raise ServiceException(ErrorCode.FAIL, 'job_id重复,该任务已经存在')
     job.create_time = datetime.now()
     session.add(job)
     session.commit()
     return job.job_id
Exemplo n.º 9
0
 def resume_scheduler():
     """
     重启调度器
     :return:
     """
     jobs = JobService.list_jobs_by_status(Job.Status.SUSPENDED.value)
     for job in jobs:
         job.status = Job.Status.RUNNING.value
         session.add(job)
     scheduler.resume()
     session.commit()
Exemplo n.º 10
0
    def stop_all_jobs():
        """
        停止所有job
        :return:
        """

        jobs = JobService.list_jobs_by_status()
        for job in jobs:
            job.status = Job.Status.STOPPED.value
            session.add(job)
        session.commit()
Exemplo n.º 11
0
def add_new_user(user_data):
    new_user = User(**user_data)
    session.add(new_user)
    session.commit()
    notebook_data = dict(
        name=new_user.username,
        user_id=new_user.id,
        index=1)
    new_notebook = notebook.add_new_notebook(notebook_data)
    new_user.active_notebook_id = new_notebook.id
    session.commit()
    return new_user, new_notebook
Exemplo n.º 12
0
 def add_executed_times(job_id, cnt):
     """
     增加任务运行次数
     :param job_id: job_id
     :param cnt: 次数
     :return:
     """
     job = JobService.get_job(job_id)
     job.executed_times += cnt
     session.add(job)
     session.commit()
     return job.job_id
Exemplo n.º 13
0
 def stop_job(job_id):
     """
     任务停止
     :param job_id:
     :return:
     """
     job = JobService.get_job(job_id)
     if job.status == Job.Status.STOPPED.value:
         raise ServiceException(ErrorCode.FAIL, '该任务已经停止')
     job.status = Job.Status.STOPPED.value
     session.add(job)
     session.commit()
     scheduler.remove_job(job)
Exemplo n.º 14
0
def save_tag_into_db(tag):
    check = session.query(LeetcodeTagInfo).filter(
        LeetcodeTagInfo.name == tag.name).first()
    if check:
        check.name = tag.name
        check.slug = tag.slug
        check.questions = tag.questions
        check.update_time = datetime.now()
        session.add(check)
    else:
        tag.create_time = datetime.now()
        session.add(tag)
    session.commit()
Exemplo n.º 15
0
def add_new_notebook(notebook_data):
    user_id, index = notebook_data["user_id"], notebook_data["index"]
    shift_notebooks(user_id, index, maxint)
    new_notebook = Notebook(**notebook_data)
    new_page = Page(**dict(
        notebook_id=new_notebook.id,
        content="",
        index=1))
    new_notebook.pages = [new_page]
    session.add(new_notebook)
    session.commit()
    new_notebook.active_page_id = new_page.id
    return new_notebook
Exemplo n.º 16
0
 def pause_job(job_id):
     """
     暂停任务
     :param job_id:
     :return:
     """
     job = JobService.get_job(job_id)
     if job.status != Job.Status.RUNNING.value:
         raise ServiceException(
             ErrorCode.FAIL,
             '该任务无法暂停,当前任务状态:%s' % job.Status.label(job.status))
     job.status = Job.Status.SUSPENDED.value
     session.add(job)
     session.commit()
     scheduler.pause_job(job)
Exemplo n.º 17
0
 def change_job_status(job_id, status):
     """
     改变任务状态
     :param job_id:
     :param status:
     :return:
     """
     all_status = [
         Job.Status.DELETED, Job.Status.STOPPED, Job.Status.RUNNING,
         Job.Status.SUSPENDED
     ]
     if status not in all_status:
         raise ServiceException(ErrorCode.PARAM_ERROR, 'status参数错误')
     session = Session()
     job = JobService.get_job(job_id)
     job.status = status.value
     session.add(job)
     session.commit()
     return job.job_id
Exemplo n.º 18
0
 def start_job(job_id):
     """
     开启一个停止的任务或者暂停的任务
     :param job_id:
     :return:
     """
     if scheduler.status() != RUNNING:
         raise ServiceException(ErrorCode.FAIL, '无法启动任务,调度器没有运行')
     job = JobService.get_job(job_id)
     if job.status != Job.Status.STOPPED.value and job.status != Job.Status.SUSPENDED.value:
         raise ServiceException(
             ErrorCode.FAIL,
             '该任务无法启动,当前任务状态:%s' % job.Status.label(job.status))
     if job.status == Job.Status.STOPPED.value:
         scheduler.add_job(job)
     else:
         scheduler.resume_job(job)
     job.status = Job.Status.RUNNING.value
     session.add(job)
     session.commit()
Exemplo n.º 19
0
 def modify_job(job_id, config):
     """
     修改任务规则
     :param job_id:
     :return:
     """
     job = JobService.get_job(job_id)
     if job.status != Job.Status.STOPPED.value:
         raise ServiceException(
             ErrorCode.FAIL, '请先停止任务,当前状态:' + job.Status.label(job.status))
     try:
         config = eval(config)
         job.name = config['name'] if 'name' in config else job.name
         job.cron = config['cron'] if 'cron' in config else job.cron
         job.type = config['type'] if 'type' in config else job.type
         job.instance_cnt = config[
             'instance_cnt'] if 'instance_cnt' in config else job.instance_cnt
     except Exception as e:
         raise ServiceException(ErrorCode.PARAM_ERROR.value, '配置信息不正确')
     session.add(job)
     session.commit()
Exemplo n.º 20
0
def save_problem_into_db(problem):
    check = session.query(LeetcodeProblem).filter(
        LeetcodeProblem.lid == problem.lid).first()
    if check:
        check.lid = problem.lid
        check.title = problem.title
        check.desc = problem.desc
        check.difficulty = problem.difficulty
        check.is_locked = problem.is_locked
        check.type = problem.type
        check.submit_url = problem.submit_url
        check.code_def = problem.code_def
        check.qid = problem.qid
        check.frequency = problem.frequency
        check.title_slug = problem.title_slug
        check.update_time = datetime.now()
        session.add(check)
    else:
        problem.create_time = datetime.now()
        session.add(problem)
    session.commit()
Exemplo n.º 21
0
def test_delete_page():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        new_page = Page(**dict(notebook_id=1))    
        session.add(new_page)
        session.commit()
        rv = http(c, "delete", "/pages/%s" % new_page.id)
        assert rv.status_code == 404
        assert "Page is not found." in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        to_delete_page = session.query(Page).filter_by(notebook_id=notebook_id).first()    
        previous_count = len(session.query(Page).filter_by(notebook_id=notebook_id).all())
        rv = http(c, "delete", "/pages/%s" % to_delete_page.id)
        current_count = len(session.query(Page).filter_by(notebook_id=notebook_id).all())
        assert rv.status_code == 200
        assert "OK." in rv.data
        assert current_count == previous_count - 1
Exemplo n.º 22
0
        def wrapper(*args, **kwargs):

            utc_now = int(time.time())

            loggedin_data = loggedin_parser.parse_args(data_parser("loggedin"))

            # Query for Token with given user id and hash
            token = session.query(Token).filter(Token.user_id == loggedin_data['user_id'],
                                                Token.hash == loggedin_data['token_hash']).first()

            if not token:
                abort(401, message="No match found for given user id and token hash")

            # If token creation date is longer than 7 days ago (604800 seconds) abort
            if (token.create_date + 604800) < utc_now:
                abort(401, message="Token hash expired")
            # If token creation date is longer than 10 minutes ago (600 seconds),
            # refresh creation date to prevent users for loggin out after 7 days (with activity)
            elif (token.create_date + 6000) < utc_now:
                token.create_date = utc_now
                session.add(token)
                session.commit()

            return f(*args, **kwargs)
Exemplo n.º 23
0
def test_modify_page_belonging_notebook():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        new_notebook = Notebook(**dict(user_id=user_id))
        session.add(new_notebook)
        session.commit()
        to_modify_page = session.query(Page).filter_by(notebook_id=notebook_id).first()
        page_id = to_modify_page.id
        rv = http(c, "patch", "/pages/%s" % page_id, dict(notebook_id=new_notebook.id))
        newer_page = session.query(Page).filter_by(id=page_id).first()
        assert rv.status_code == 200
        assert newer_page.notebook_id == new_notebook.id
        assert "OK." in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        page_id = to_modify_page.id
        rv = http(c, "patch", "/pages/%s" % page_id, dict(notebook_id=1))
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data
Exemplo n.º 24
0
def setup():
    global user_id
    global notebook_id
    new_user = User(**dict(password="******"))
    session.add(new_user)
    session.commit()
    user_id = new_user.id
    for index in xrange(1, 51):
        new_notebook = Notebook(**dict(
            user_id=user_id,
            name="notebook_name_of_%s" % user_id,
            index=index))
        session.add(new_notebook)
    session.commit()
    notebook_id = new_notebook.id
    for index in xrange(1, 51):
        new_page = Page(**dict(
            notebook_id=notebook_id,
            content="page_content",
            index=index))
        session.add(new_page)
    session.commit()
Exemplo n.º 25
0
def change_active_notebook(user_id, notebook_id):
    user = session.query(User).filter_by(id=user_id).first()
    user.active_notebook_id = notebook_id
    session.commit()
Exemplo n.º 26
0
def shutdown_session(response_or_exc):
    if response_or_exc is None:
        session.commit()
    session.remove()
    return response_or_exc
Exemplo n.º 27
0
def create_feedback(feedback_data):
    session.add(Feedback(**feedback_data))
    session.commit()
Exemplo n.º 28
0
def modify_notebook_name(notebook_id, name):
    notebook = session.query(Notebook).filter_by(id=notebook_id).first()
    notebook.name = name
    session.commit()