Beispiel #1
0
def course_delete(userid, id):
    course = Course.query.filter_by(id=id, admin_id=userid).first()
    if not course:
        return Resp.error(message="这个课程不是你创建的,你没有权限删除它")
    else:
        db.session.delete(course)
        db.session.commit()
        return Resp.success()
Beispiel #2
0
def is_my_loved_course(userid, course_id):
    user = User.query.get(userid)
    course = Course.query.get(course_id)
    if course in user.courses:
        print("yes i loved it")
        return Resp.success(data="true")
    else:
        return Resp.success(data="false")
Beispiel #3
0
def user_love_course(userid, course_id):
    user = User.query.get(userid)
    course = Course.query.get(course_id)
    if not course in user.courses:
        user.courses.append(course)
        course.collect_num = len(course.users)
        db.session.commit()
        return Resp.success()
    else:
        return Resp.error()
Beispiel #4
0
def all_my_loved_course(userid):
    user = User.query.get(userid)
    courses = user.courses

    return Resp.success(data=[
        course.to_json() for course in courses
    ])
Beispiel #5
0
def dont_love_it_anymore(userid, course_id):
    user = User.query.get(userid)
    course = Course.query.get(course_id)
    user.courses.remove(course)
    course.collect_num = len(course.users)
    db.session.commit()
    return Resp.success()
Beispiel #6
0
def give_course_score(userid, course_id, score):
    score_all = Scores.query.filter_by(course_id=course_id).all()
    has_score = Scores.query.filter_by(user_id=userid, course_id=course_id).first()
    course = Course.query.get(course_id)

    if not has_score:
        score_new = Scores(user_id=userid, course_id=course_id, score=score)
        score_time = len(score_all)
        if score_time == 0:
            course.score = score
        else:
            course.score = (course.score + score) / (score_time + 1)
        db.session.add(score_new)
        db.session.commit()
        return Resp.success()
    else:
        return Resp.error(message="已经打过分了")
Beispiel #7
0
def complete_info(userid):
    form_data = request.get_json()
    admin = Admin.query.get(userid)
    admin.name = form_data['name']
    admin.avatar = form_data['avatar']
    admin.phone_number = form_data['phone_number']
    admin.institution = form_data['institution']
    db.session.commit()
    return Resp.success()
Beispiel #8
0
def modulesNoFollow(userid):
    all_module = Module.query.all()
    user_modules = User.query.get(userid).modules
    diff_modules = list(set(all_module).difference(set(user_modules)))

    return Resp.success(data=[{
        "id": module.id,
        "value": module.name
    } for module in diff_modules])
Beispiel #9
0
def search_course_by_name(userid,key):
    h = UserSearchHistory(
        user_id=userid,
        content=key
    )
    db.session.add(h)
    db.session.commit()
    courses = Course.query.filter(Course.name.like('%'+key+'%')).all()
    return Resp.success(data=[course.to_json() for course in courses])
Beispiel #10
0
def get_courses_form_module(module):
    if module == "1":
        courses = Course.query.order_by(desc('view_num')).all()
    elif module == "2":
        courses = Course.query.order_by(func.random()).limit(5).all()
    else:
        module = Module.query.filter_by(name=module).first()
        courses = Course.query.filter_by(module=module).all()
    print(courses)
    return Resp.success(data=[course.to_json() for course in courses])
Beispiel #11
0
def modify_user_info(userid):
    data = request.get_json()
    user = User.query.get(userid)

    user.name = data['name']
    user.sex = data['sex']
    user.age = data['age']
    user.describe = data['describe']
    if data['password'] != "":
        print("修改密码了")
        user.password = data['password']

    db.session.commit()
    return Resp.success()
Beispiel #12
0
def add_comment_reply(userid):
    course_id = request.get_json()['course_id']
    content = request.get_json()['content']
    to_user_id = request.get_json()['to']

    comment_new = Comment(course_id=course_id,
                          content=content,
                          from_user_id=userid,
                          from_user_type=2,
                          to_user_id=to_user_id,
                          to_user_type=1)

    db.session.add(comment_new)
    db.session.commit()

    return Resp.success()
Beispiel #13
0
def add_comment(userid):
    course_id = request.get_json()['course_id']
    content = request.get_json()['content']
    course = Course.query.get(course_id)

    comment_new = Comment(
        course_id=course_id,
        content=content,
        from_user_id=userid,
        from_user_type=1,
        to_user_id=course.admin_id,
        to_user_type=2
    )

    db.session.add(comment_new)
    db.session.commit()

    return Resp.success()
Beispiel #14
0
def course_edit(userid, id):
    course = Course.query.get(id)
    form_data = request.get_json()

    module_name = form_data['module']
    module_db = Module.query.filter_by(name=module_name).first()
    if module_db:
        module_id = module_db.id
    else:
        module_new = Module(name=module_name)
        db.session.add(module_new)
        db.session.commit()
        module_id = module_new.id

    course.name = form_data['name']
    course.cover = form_data['cover']
    course.module_id = module_id
    course.content = form_data['content']
    course.type = form_data['type']
    db.session.commit()
    return Resp.success()
Beispiel #15
0
def course_store(userid):
    form_data = request.get_json()
    module_name = form_data['module']
    module_db = Module.query.filter_by(name=module_name).first()
    user = Admin.query.get(userid)
    if module_db:
        module_id = module_db.id
    else:
        module_new = Module(name=module_name)
        db.session.add(module_new)
        db.session.commit()
        module_id = module_new.id

    course_new = Course(name=form_data['name'],
                        cover=form_data['cover'],
                        module_id=module_id,
                        content=form_data['content'],
                        type=form_data['type'],
                        admin_id=userid)
    db.session.add(course_new)
    user.post_num = Course.query.filter_by(admin_id=userid).count()
    db.session.commit()
    return Resp.success()
Beispiel #16
0
def all_course():
    courses = Course.query.all()
    return Resp.success(data=[course.to_json() for course in courses])
Beispiel #17
0
def cancel_modules(userid,id):
    user = User.query.get(userid)
    module = Module.query.get(id)
    user.modules.remove(module)
    db.session.commit()
    return Resp.success()
Beispiel #18
0
def modules_follow_it(userid,id):
    user = User.query.get(userid)
    module = Module.query.get(id)
    user.modules.append(module)
    db.session.commit()
    return Resp.success()
Beispiel #19
0
def my_rs_list_admin(userid):
    user = Admin.query.get(userid)
    comments = Comment.query.filter_by(from_user_id=userid,
                                       from_user_type=2).all()
    return Resp.success(data=[comment.to_json() for comment in comments])
Beispiel #20
0
def modules_i_have(userid):
    user = User.query.get(userid)
    return Resp.success(data=[{
        "id": module.id,
        "value": module.name
    } for module in user.modules])
Beispiel #21
0
def user_info(userid):
    admin = Admin.query.get(userid)
    return Resp.success(data=admin.to_json())
Beispiel #22
0
def search_history(userid):
    histories = UserSearchHistory.query.filter_by(user_id=userid).all()
    return Resp.success(data=[
        history.content for history in histories
    ])
Beispiel #23
0
def get_comments(course_id):
    comments = Comment.query.filter_by(course_id=course_id).all()
    list = [comment.to_json() for comment in comments]
    return Resp.success(data=list)
Beispiel #24
0
def user_info(userid, file_name):
    user = User.query.get(userid)
    user.avatar = file_name
    db.session.commit()
    return Resp.success()
Beispiel #25
0
def user_infos(userid):
    user = User.query.get(userid)
    return Resp.success(data=user.to_json())
Beispiel #26
0
def course_list(userid):
    list = Admin.query.get(userid).courses
    return Resp.success(data=[course.to_json() for course in list])
Beispiel #27
0
def get_comment(id):
    comment = Comment.query.get(id)
    return Resp.success(data=comment.content)
Beispiel #28
0
def my_replies_lists(userid):
    user = User.query.get(userid)
    comments = Comment.query.filter_by(to_user_id=userid, from_user_type=2).all()
    return Resp.success(data=[
        comment.to_json() for comment in comments
    ])
Beispiel #29
0
def get_course(id):
    course = Course.query.get(id)
    course.view_num = 1 if course.view_num == None else course.view_num + 1
    return Resp.success(data=course.to_json())
Beispiel #30
0
def get_course_score(userid, course_id):
    score = Scores.query.filter_by(user_id=userid, course_id=course_id).first()
    if score:
        return Resp.success(data=score.score)
    else:
        return Resp.error()