Exemplo n.º 1
0
def test_create_page():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s.clear()
        rv = http(c, "post", "/pages", dict(notebook_id=notebook_id, index=1))
        assert rv.status_code == 401
        assert "You have to login first." in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 1
        rv = http(c, "post", "/pages", dict(notebook_id=notebook_id, index=1))
        assert rv.status_code == 404
        assert "Notebook 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
        previous_count = len(session.query(Page).filter_by(notebook_id=notebook_id).all())
        rv = http(c, "post", "/pages", dict(notebook_id=notebook_id, index=1))
        current_count = len(session.query(Page).filter_by(notebook_id=notebook_id).all())
        assert rv.status_code == 201
        assert "id" in rv.data
        assert "index" in rv.data
        assert current_count == previous_count + 1
Exemplo n.º 2
0
def test_change_active_notebook():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_set_id = session.query(Notebook).filter_by(user_id=2).first().id
        rv = http(c, "put", "/notebooks/active_notebook", dict(notebook_id=to_set_id))
        assert session.query(User).filter_by(id=2).first().active_notebook_id == to_set_id
        assert "OK." in rv.data
Exemplo n.º 3
0
    def list_jobs_by_status(status=None):
        """
        列出所有Job
        :param status: -1删除 0停止 1执行 2暂停 3待加入
        :return: [Job]
        """
        if status is None:
            return session.query(Job).all()

        return session.query(Job).filter(Job.status == status).all()
Exemplo n.º 4
0
def test_save_content_of_page():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        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(content="FUCKYOU"))
        newer_page = session.query(Page).filter_by(id=page_id).first()
        assert rv.status_code == 200
        assert "OK." in rv.data
        assert "FUCKYOU" == newer_page.content
Exemplo n.º 5
0
def test_modify_page_position():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        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(index=25))
        newer_page = session.query(Page).filter_by(id=page_id).first()
        assert rv.status_code == 200
        assert newer_page.index == 25
        assert "OK." in rv.data
Exemplo n.º 6
0
def test_signup():
    with app.test_client() as c:

        # Test for successfully user creation.
        previous_users_count = len(session.query(User).all())
        user_data = dict(
            email="*****@*****.**",
            username="******",
            password="******")
        rv = http(c, "post", "/users", user_data)
        result = json.loads(rv.data)
        present_users_count = len(session.query(User).all())
        assert rv.status_code == 201
        assert present_users_count == previous_users_count + 1
        assert result["email"] == user_data["email"]
        assert result["username"] == user_data["username"]
        new_user = session.query(User).filter_by(id=result["id"]).first()
        assert new_user.password == utils.encrypt(user_data["password"])
        assert new_user.is_vip == False
        assert new_user.notebooks[0].index == 1
        assert new_user.active_notebook_id == new_user.notebooks[0].id
        assert new_user.notebooks[0].pages[0].index == 1
        assert new_user.notebooks[0].active_page_id == new_user.notebooks[0].pages[0].id
        assert len(result["notebooks"]) == 1
        assert len(result["notebooks"][0]["pages"]) == 1

        # Test for email conflicts
        user_data = dict(
            email="*****@*****.**",
            username="******",
            password="******")
        rv = http(c, "post", "/users", user_data)
        assert rv.status_code == 409
        assert "Email has already existed." in rv.data

        # For username conflicts
        user_data = dict(
            email="*****@*****.**",
            username="******",
            password="******")
        rv = http(c, "post", "/users", user_data)
        assert rv.status_code == 409
        assert "Username has already existed." in rv.data

        user_data = dict(
            email="iammfw-fuck163.com",
            username="******",
            password="******")
        rv = http(c, "post", "/users", user_data)
        assert rv.status_code == 400
        assert "Email is not valid." in rv.data
Exemplo n.º 7
0
def get_info_by_username(username):
    """
    通过用户名获取用户信息
    :param username:
    :return:
    """
    return session.query(LeetcodeInfo).filter(LeetcodeInfo.username == username).first()
Exemplo n.º 8
0
def list_leetcode_info_by_status(status):
    """
    列出信息
    :param status:
    :return:
    """
    return session.query(LeetcodeInfo).filter(LeetcodeInfo.status == status).all()
Exemplo n.º 9
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.º 10
0
def test_retrieve_all_notebooks():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "get", "/notebooks")    
        result = json.loads(rv.data)
        all_notebooks = session.query(Notebook).filter_by(user_id=2).all()
        assert len(result["notebooks"]) == len(all_notebooks)
Exemplo n.º 11
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.º 12
0
def test_retrieve_specific_notebook():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_get = session.query(Notebook).filter_by(user_id=2).first()
        rv = http(c, "get", "/notebooks/%s" % to_get.id)    
        assert to_get.name in rv.data
        assert "pages" in rv.data
        assert rv.status_code == 200
Exemplo n.º 13
0
def test_create_feedback():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "post", "/feedbacks", dict(content="FUCKYOU"))
        assert "OK." in rv.data
        assert rv.status_code == 201
        feedback = session.query(Feedback).first()
        assert feedback.content == "FUCKYOU"
Exemplo n.º 14
0
 def get_job(job_id):
     """
     根据job_id获取job
     :param job_id:
     :return:
     """
     job = session.query(Job).filter(Job.job_id == job_id).first()
     if job is None:
         raise ServiceException(ErrorCode.NOT_FOUND, '该任务不存在')
     return job
Exemplo n.º 15
0
 def stop_scheduler():
     """
     停止调度器
     :return:
     """
     jobs = session.query(Job).filter(
         or_(Job.status == Job.Status.RUNNING.value,
             Job.status == Job.Status.SUSPENDED.value)).all()
     for job in jobs:
         JobService.stop_job(job.job_id)
     scheduler.shutdown(waited=True)
Exemplo n.º 16
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.º 17
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.º 18
0
 def _handle_list_of_relations(self, prop, ids):
     related_ids = map(lambda item: item.id, getattr(self, prop))
     if set(ids) == set(related_ids):
         return None
     target_class = getattr(self.__class__, prop).property.mapper.class_
     if len(ids) > 0:
         instances = session.query(target_class).filter(
             target_class.id.in_(ids)).all()
         setattr(self, prop, instances)
     else:
         setattr(self, prop, [])
Exemplo n.º 19
0
def test_change_active_page():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        page_id = session.query(Page).filter_by(notebook_id=notebook_id).first().id
        rv = http(c, "put", "/pages/active_page", dict(notebook_id=notebook_id, page_id=page_id))
        notebook = session.query(Notebook).filter_by(id=notebook_id).first()
        assert rv.status_code == 200
        assert "OK." in rv.data
        assert notebook.active_page_id == page_id

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 1
        page_id = session.query(Page).filter_by(notebook_id=notebook_id).first().id
        rv = http(c, "put", "/pages/active_page", dict(notebook_id=notebook_id, page_id=page_id))
        notebook = session.query(Notebook).filter_by(id=notebook_id).first()
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data
Exemplo n.º 20
0
def test_get_page_info():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = user_id
        page_id = session.query(Page).filter_by(notebook_id=notebook_id).first().id
        rv = http(c, "get", "/pages/%s" % page_id)
        assert "content" in rv.data
        assert "id" in rv.data
        assert "notebook_id" in rv.data
        assert "index" in rv.data
        assert rv.status_code == 200
Exemplo n.º 21
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.º 22
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.º 23
0
def test_delete_a_notebook():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_delete_notebook = session.query(Notebook).filter_by(user_id=2, index=25).first()
        previous_cout = len(session.query(Notebook) \
            .filter(
                Notebook.user_id==2,
                Notebook.index>=to_delete_notebook.index) \
            .all())
        rv = http(c, "delete", "/notebooks/%s" % to_delete_notebook.id)
        assert rv.status_code == 200
        assert "OK." in rv.data
        current_count = len(session.query(Notebook) \
            .filter(
                Notebook.user_id==2,
                Notebook.index>to_delete_notebook.index) \
            .all())
        assert current_count == previous_cout - 2

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_delete_notebook = session.query(Notebook).filter_by(user_id=1).first()
        rv = http(c, "delete", "/notebooks/%s" % to_delete_notebook.id)
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "delete", "/notebooks/3000000")
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data
Exemplo n.º 24
0
def test_create_a_notebook():
    assert len(session.query(Notebook).filter_by(user_id=2).all()) == 50
    assert len(session.query(Notebook).filter(Notebook.user_id==2, Notebook.index>=26).all()) == 25

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "post", "/notebooks", dict(
            name="notebook_name2",
            index=1))
        assert rv.status_code == 201
        assert len(session.query(Notebook).filter(Notebook.user_id==2).all()) == 51
        assert len(session.query(Notebook).filter(Notebook.user_id==2, Notebook.index > 1).all()) == 50
        notebook = session.query(Notebook).filter_by(user_id=2, index=1).first()
        assert notebook.name == "notebook_name2"
        assert notebook.pages[0].index == 1
        assert notebook.active_page_id == notebook.pages[0].id
        assert "name" in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "post", "/notebooks", dict(
            name="notebook_name2",
            index=1))
        assert rv.status_code == 409
        assert "Name has already existed." in rv.data

    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        rv = http(c, "post", "/notebooks", dict(index=1))
        assert rv.status_code == 400
        assert "Name is not valid." in rv.data
Exemplo n.º 25
0
def test_modify_notebook_name():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_modify_notebook = session.query(Notebook).filter_by(user_id=2).first()
        notebook_id = to_modify_notebook.id
        rv = http(c, "patch", "/notebooks/%s?field=name" % notebook_id, dict(name="new_notebook_name"))
        assert rv.status_code == 200
        to_modify_notebook = session.query(Notebook).filter_by(user_id=2).first()
        assert to_modify_notebook.name == "new_notebook_name"

        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_modify_notebook = session.query(Notebook).filter_by(user_id=2).all()[2]
        notebook_id = to_modify_notebook.id
        rv = http(c, "patch", "/notebooks/%s?field=name" % notebook_id, dict(name="new_notebook_name"))
        assert rv.status_code == 409
        to_modify_notebook = session.query(Notebook).filter_by(user_id=2).first()
        assert to_modify_notebook.name == "new_notebook_name"

        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        to_modify_notebook = session.query(Notebook).filter_by(user_id=1).first()
        notebook_id = to_modify_notebook.id
        rv = http(c, "patch", "/notebooks/%s?field=name" % notebook_id, dict(name="new_notebook_name1"))
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data

        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 1
        rv = http(c, "patch", "/notebooks/2304314321?field=name", dict(name="new_notebook_name1"))
        assert rv.status_code == 404
        assert "Notebook is not found." in rv.data
Exemplo n.º 26
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.º 27
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.º 28
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.º 29
0
def get_feedbacks(start, end):
    feedbacks = session.query(Feedback).all()
    return feedbacks[start:end]
Exemplo n.º 30
0
def get_user_by_email(email):
    return session.query(User).filter_by(email=email).first()
Exemplo n.º 31
0
def get_user_by_id(id):
    return session.query(User).filter_by(id=id).first()
Exemplo n.º 32
0
def get_user_by_name(name):
    return session.query(User).filter_by(username=name).first()
Exemplo n.º 33
0
def is_email_existed(email):
    return session.query(User).filter_by(email=email).first()
Exemplo n.º 34
0
def get_user_by_username(username):
    acre_info = session.query(AcreInfo).filter(
        AcreInfo.username == username).first()
    if acre_info is None:
        raise Exception('一亩三分地用户%s不存在' % username)
    return acre_info
Exemplo n.º 35
0
def find_notebook_by_name_with_user_id(name, user_id):
    return session.query(Notebook) \
                  .filter_by(name=name, user_id=user_id) \
                  .first()
Exemplo n.º 36
0
def get_all_notebooks_by_user_id(user_id):
    return session.query(Notebook) \
                  .filter_by(user_id=user_id) \
                  .order_by(Notebook.index) \
                  .all()
Exemplo n.º 37
0
def find_notebook_by_id(notebook_id):
    return session.query(Notebook).filter_by(id=notebook_id).first()
Exemplo n.º 38
0
def modify_notebook_name(notebook_id, name):
    notebook = session.query(Notebook).filter_by(id=notebook_id).first()
    notebook.name = name
    session.commit()
Exemplo n.º 39
0
def is_username_existed(username):
    return session.query(User).filter_by(username=username).first()
Exemplo n.º 40
0
def test_modify_notebook_position():
    with app.test_client() as c:
        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        notebooks = session.query(Notebook).filter_by(user_id=2).all()
        # simulate 25 -> 10
        to_move_index = 25
        target_index = 10
        id_of_target_pos = session.query(Notebook).filter_by(user_id=2, index=target_index).first().id
        id_of_previous_pos = session.query(Notebook).filter_by(user_id=2, index=to_move_index-1).first().id
        id_of_to_move = session.query(Notebook).filter_by(user_id=2, index=to_move_index).first().id
        rv = http(c, "patch", "/notebooks/%s" % id_of_to_move, dict(index=target_index))
        assert "OK." in rv.data
        notebook_of_target_pos = session.query(Notebook).filter_by(id=id_of_target_pos).first()
        notebook_of_previous_pos = session.query(Notebook).filter_by(id=id_of_previous_pos).first()
        notebook_of_to_move = session.query(Notebook).filter_by(id=id_of_to_move).first()
        assert notebook_of_target_pos.index == target_index + 1
        assert notebook_of_previous_pos.index == to_move_index
        assert notebook_of_to_move.index == target_index

        with c.session_transaction() as s:
            s["is_login"] = True
            s["id"] = 2
        notebooks = session.query(Notebook).filter_by(user_id=2).all()
        # simulate 20 -> 30
        to_move_index = 20
        target_index = 30
        id_of_target_pos = session.query(Notebook).filter_by(user_id=2, index=target_index).first().id
        id_of_next_pos = session.query(Notebook).filter_by(user_id=2, index=to_move_index+1).first().id
        id_of_to_move = session.query(Notebook).filter_by(user_id=2, index=to_move_index).first().id
        rv = http(c, "patch", "/notebooks/%s" % id_of_to_move, dict(index=target_index))
        assert "OK." in rv.data
        notebook_of_target_pos = session.query(Notebook).filter_by(id=id_of_target_pos).first()
        notebook_of_next_pos = session.query(Notebook).filter_by(id=id_of_next_pos).first()
        notebook_of_to_move = session.query(Notebook).filter_by(id=id_of_to_move).first()
        assert notebook_of_target_pos.index == target_index - 1
        assert notebook_of_next_pos.index == to_move_index
        assert notebook_of_to_move.index == target_index
Exemplo n.º 41
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.º 42
0
 def _handle_single_relation(self, prop, id_):
     if getattr(self, prop).id == id_:
         return None
     target_class = getattr(self.__class__, prop).property.mapper.class_
     inst = session.query(target_class).get(id_)
     setattr(self, prop, inst)
Exemplo n.º 43
0
def list_users_by_status(status):
    return session.query(AcreInfo).filter(AcreInfo.status == status).all()