Esempio n. 1
0
    def post(self):
        args = get_request_params([("list_id", int, True, "json"),
                                   ("token", str, True, "json"),
                                   ("uid", int, True, "json")])

        list_id = args["list_id"]

        bl = BookList.query.get(list_id)
        if not bl:
            return {"status": 1, "msg": "书单不存在"}

        books = bl.books
        return {
            "msg":
            "获取成功",
            "status":
            0,
            "data": [{
                "book_db_id": b.book_db_id,
                "book_rate": b.book_rate,
                "book_last_number": len(json.loads(b.detail_data)),
                "book_id": b.book_id,
                "book_cover": b.book_cover,
                "book_author": json.loads(b.book_author),
                "book_title": b.book_title,
                "book_publish": b.book_publish
            } for b in books]
        }
Esempio n. 2
0
    def post(self):
        args = get_request_params([["book_id", int, True, "json"],
                                   ["token", str, True, "json"],
                                   ["uid", int, True, "json"]])
        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}
        result = db.session.execute(
            "select * from subscribes WHERE user_id=:UID and book_id=:BID",
            params={
                "UID": args["uid"],
                "BID": args["book_id"]
            })
        result = result.fetchone()

        if result:
            return {"status": 1, "msg": "已经订阅了"}
        else:
            db.session.execute(
                "insert into subscribes (user_id, book_id) VALUES (:UID, :BID)",
                params={
                    "UID": args["uid"],
                    "BID": args["book_id"]
                })

        db.session.commit()
        return {"status": 0, "msg": "订阅成功"}
Esempio n. 3
0
    def post(self):
        """
        获取书单
        :return:
        """
        args = get_request_params([("token", str, True, "json"),
                                   ("uid", int, True, "json")])

        token = args["token"]
        uid = args["uid"]

        user = User.verify_token(token)

        if (not user) or (user.id_ != uid):
            return {"status": 1, "msg": "未登录"}

        brs = BookListModel.query.filter_by(user_id=uid).all()
        data = [{
            "list_id": br.id_,
            "book_list": [b.book_id for b in br.books],
            "list_name": br.title,
            "list_content": br.description
        } for br in brs]

        return {"status": 0, "msg": "获取成功", "data": data}
Esempio n. 4
0
    def post(self):
        """
        新建书单
        :return: 
        """
        args = get_request_params([("uid", int, True, "json"),
                                   ("list_name", str, True, "json"),
                                   ("token", str, True, "json"),
                                   ("list_content", str, True, "json")])

        uid = args["uid"]
        token = args["token"]
        list_name = args["list_name"]
        description = args["list_content"]

        user = User.verify_token(token)

        if (not user) or (user.id_ != uid):
            return {"status": 1, "msg": "未登录"}

        br = BookListModel(user_id=uid,
                           title=list_name,
                           description=description)

        try:
            db.session.add(br)
            db.session.commit()

            return {"status": 0, "msg": "创建成功"}

        except Exception as e:
            return {"status": 2, "msg": "服务器飞了"}
Esempio n. 5
0
    def post(self):
        args = get_request_params([["token", str, True, "json"],
                                   ["uid", int, True, "json"]])
        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}
        s = user.subscribing

        available = [i for i in s if i.book]
        books = [i.book for i in available]
        user.subscribing = available
        db.session.add(user)
        db.session.commit()

        return {
            "status":
            0,
            "msg":
            "获取成功",
            "books": [{
                "book_last_number": book.book_last_number,
                "book_cover": book.book_cover,
                "book_id": book.book_id,
                "book_author": json.loads(book.book_author),
                "book_title": book.book_title,
                "book_db_id": book.book_db_id,
                "book_publish": book.book_publish,
                "book_rate": book.book_rate
            } for book in books]
        }
Esempio n. 6
0
    def post(self):
        args = get_request_params([("token", int, True, "json"),
                                   ("contact", str, True, "json"),
                                   ("uid", int, True, "json"),
                                   ("timestamp", str, True, "json"),
                                   ("content", str, True, "json")])

        token = args["token"]
        contact = args["contact"]
        uid = args["uid"]
        content = args["content"]

        user = User.verify_token(token)

        if (not user) or (user.id_ != uid):
            return {"status": 1, "msg": "未登录"}

        f = FeedBackModel(user_id=user.id_,
                          contact=contact,
                          date=datetime.now(),
                          content=content)

        db.session.add(f)
        db.session.commit()

        return {"status": 0, "message": "反馈成功"}
Esempio n. 7
0
    def post(self):
        args = get_request_params([("book_id", int, True, "json"),
                                   ("list_id", int, True, "json"),
                                   ("token", str, True, "json"),
                                   ("uid", int, True, "json")])

        user = User.verify_token(args["token"])

        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "没有登录"}
        book_list = BookListModel.query.get(args["list_id"])
        if not book_list:
            return {"status": 2, "msg": "书单不存在"}

        if book_list.user_id != args["uid"]:
            return {"status": 3, "msg": "书单不是你的"}

        db.session.execute(
            "delete from booklist_r WHERE book_id=:BID AND list_id=:LID",
            params={
                "BID": args["book_id"],
                "LID": args["list_id"]
            })
        db.session.commit()

        return {"status": 0, "message": "删除成功"}
Esempio n. 8
0
    def post(self):
        args = get_request_params([
            ("timestamp", float, True, "json"),
            ("book_db_id", int, True, "json"),
            ("token", str, True, "json"),
            ("book_id", int, True, "json"),
            ("uid", int, True, "json"),
        ])

        timestamp = args["timestamp"]
        book_db_id = args["book_db_id"]
        token = args["token"]
        book_id = args["book_id"]
        uid = args["uid"]

        user = User.verify_token(token)
        if user is None or user.id_ != uid:
            return {
                "data": [],
                "status": 2,
                "msg": "认证失败"
            }
        try:
            the_book = Book.query.filter_by(book_id=book_id).first()
            if not the_book:
                return {
                    "status": 0,
                    "message": "搜索成功",
                    "data": None
                }
            the_detail_data = json.loads(the_book.detail_data)

            return {
                "status": 0,
                "msg": "搜索成功",
                "data": {
                    "book_rate": the_book.book_rate,
                    "book_content": the_book.book_content,
                    "book_publish": the_book.book_publish,
                    "book_last_number": the_book.book_last_number,
                    "book_key": the_book.book_key,
                    "book_db_id": the_book.book_db_id,
                    "book_title": the_book.book_title,
                    "detail_data": the_detail_data,
                    "book_author": json.loads(the_book.book_author),
                    "book_place": None if len(the_detail_data) == 0 else the_detail_data[0]["detail_place"],
                    "book_id": the_book.book_id,
                    "book_cover": the_book.book_cover,
                    "is_subscribe": 1 if uid in the_book.subscribers else 0
                }
            }

        except Exception as e:
            return {
                "data": [],
                "status": 2,
                "msg": "服务器溜了"
            }
Esempio n. 9
0
    def post(self):
        args = get_request_params([("token", str, True, "json"),
                                   ("uid", int, True, "json")])
        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}

        b = Borrowed().get()
        return {"status": 0, "msg": "获取成功", "books": b}
Esempio n. 10
0
    def post(self):
        args = get_request_params([
            ("timestamp", float, True, "json"),
            ("token", str, True, "json"),
            ("book_id", int, True, "json"),
            ("uid", int, True, "json")
        ])

        timestamp = args["timestamp"]
        token = args["token"]
        book_id = args["book_id"]
        uid = args["uid"]

        def _push_msg(message, device_id):
            app_key = 'app_key'
            master_secret = 'master_key'

            _jpush = jpush.JPush(app_key, master_secret)
            push = _jpush.create_push()
            # push.audience = jpush.audience([{"registration_id":device_id}])
            push.audience = {'registration_id': [device_id]}
            # push.audience = device_id
            android_msg = jpush.android(
                message,
                None,
                None,
                {
                    "msg": message,  # 强行套用app中notification的相关格式
                    "status": 0
                }
            )
            ios_msg = jpush.ios(
                message,
                None,
                None,
                {
                    "msg": message,  # 强行套用app中notification的相关格式
                    "status": 0
                }
            )
            push.notification = jpush.notification("hello jpush", ios_msg, android_msg, None)
            # push.options = {"time_to_live": 86400, "sendno": 12345, "apns_production":True}
            push.options = {"time_to_live": 86400, "apns_production": True}
            push.platform = jpush.platform("all")

            push.send()

        the_book = Book.query.filter_by(book_id=book_id).first()
        the_detail_data = json.loads(the_book.detail_data)
        flag = 0
        for a_book in the_detail_data:
            if a_book["is_borrowed"] == 1:
                flag = 1
        if flag == 1:
            _push_msg("有书了", uid)
Esempio n. 11
0
    def post(self):
        args = get_request_params([("account", str, True, "json"),
                                   ("password", str, True, "json"),
                                   ("school_id", int, True, "json")])

        account = args["account"]
        pwd = args["password"]
        school_id = args["school_id"]

        # todo: check for school_id

        u = User.query.filter_by(xh=account).first()

        if not u:
            # 第一次登陆, 先尝试登录学校图书馆
            result, name = LoginService().login(account, pwd)
            if not result:
                return {"status": 1, "msg": "用户名或密码错误"}

            u = User(xh=account)
            u.password = pwd
            u.name = name
            try:
                db.session.add(u)
                db.session.commit()
                epoch = int(time.time())
                return {
                    "status": 0,
                    "msg": "注册成功",
                    "data": {
                        "timestamp": epoch,
                        "uid": u.id_,
                        "username": name,
                        "token": u.generate_token(result)
                    }
                }
            except Exception as e:
                return {"status": 2, "msg": "服务器溜了"}
            pass

        result, name = LoginService().login(account, pwd)

        if not result:
            return {"status": 1, "msg": "用户名或密码错误"}
        return {
            "status": 0,
            "msg": "登陆成功",
            "data": {
                "timestamp": int(time.time()),
                "uid": u.id_,
                "username": name,
                "token": u.generate_token(result)
            }
        }
Esempio n. 12
0
    def post(self):
        args = get_request_params([["book_id", int, True, "json"],
                                   ["token", str, True, "json"],
                                   ["uid", int, True, "json"]])
        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}
        sub = SubModel.query.filter_by(user_id=args["uid"],
                                       book_id=args["book_id"]).first()

        if sub:
            return {"status": 0, "msg": "获取成功", "is_subscribe": True}
        else:
            return {"status": 0, "msg": "获取成功", "is_subscribe": False}
Esempio n. 13
0
    def post(self):
        args = get_request_params([["book_id", int, True, "json"],
                                   ["token", str, True, "json"],
                                   ["uid", int, True, "json"]])
        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}

        sub = SubModel.query.filter_by(user_id=args["uid"],
                                       book_id=args["book_id"]).first()

        db.session.delete(sub)
        db.session.commit()

        return {"status": 0, "msg": "删除成功"}
Esempio n. 14
0
    def post(self):
        args = get_request_params([
            ("page", int, True, "json"),
            ("uid", int, True, "json"),
            ("timestamp", float, True, "json"),
            ("token", str, True, "json")
        ])

        page = args["page"]
        uid = args["uid"]
        timestamp = args["timestamp"]
        token = args["token"]

        user = User.verify_token(token)
        if user is None or user.id_ != uid:
            return {
                "data": [],
                "status": 2,
                "msg": "认证失败"
            }
        try:
            pagination = Book.query.filter_by(is_hot=True) \
                .order_by(Book.hot_id.desc()) \
                .paginate(page, per_page=20, error_out=False).items

            return {
                "status": 0,
                "msg": "搜索成功",
                "data": [{
                    "book_last_number": book.book_last_number,
                    "book_cover": book.book_cover,
                    "book_id": book.book_id,
                    "book_author": json.loads(book.book_author),
                    "book_title": book.book_title,
                    "book_db_id": book.book_db_id,
                    "book_publish": book.book_publish,
                    "book_rate": book.book_rate
                }
                    for book in pagination]
            }
        except Exception as e:
            return {
                "data": [],
                "status": 2,
                "msg": "数据库溜了"
            }
Esempio n. 15
0
    def post(self):
        args = get_request_params([["list_id", int, True, "json"],
                                   ["token", str, True, "json"],
                                   ["uid", int, True, "json"]])

        user = User.verify_token(args["token"])
        if (not user) or (user.id_ != args["uid"]):
            return {"status": 1, "msg": "未登录"}

        br = BookListModel.query.get(args["list_id"])

        if not br:
            return {"status": 1, "msg": "书单不存在"}

        if br.user_id != args["uid"]:
            return {"status": 2, "msg": "没有权限"}

        db.session.delete(br)
        db.session.commit()

        return {"status": 0, "msg": "删除成功"}
Esempio n. 16
0
    def post(self):
        args = get_request_params([("timestamp", int, True, "json"),
                                   ("book_id", int, True, "json"),
                                   ("uid", int, True, "json"),
                                   ("book_db_id", int, True, "json"),
                                   ("token", str, True, "json"),
                                   ("list_id", int, True, "json")])

        book_id = args["book_id"]
        uid = args["uid"]
        token = args["token"]
        list_id = args["list_id"]

        book_list: BookListModel = BookListModel.query.get(list_id)

        user = User.verify_token(token)

        if (not user) or (user.id_ != uid):
            return {"status": 1, "msg": "没有登录"}

        if not book_list:
            return {"status": 2, "msg": "书单不存在"}

        if book_list.user_id != uid:
            return {"status": 3, "msg": "书单不是你的"}

        book = Book.query.get(book_id)
        if not book:
            return {"status": 4, "msg": "图书不存在"}
        book_list.books.append(book)

        try:
            db.session.add(book_list)
            db.session.commit()

            return {"status": 0, "msg": "添加成功"}
        except Exception as e:
            return {"status": 5, "msg": "服务器炸了"}
Esempio n. 17
0
    def post(self):
        args = get_request_params([
            ("timestamp", float, True, "json"),
            ("token", str, True, "json"),
            ("content", str, True, "json"),
            ("uid", int, True, "json"),
            ("type", int, True, "json"),
            ("page", int, False, "json")
        ])

        timestamp = args["timestamp"]
        token = args["token"]
        content = args["content"]
        uid = args["uid"]
        type = args["type"]

        user = User.verify_token(token)
        if user is None or user.id_ != uid:
            return {
                "data": [],
                "status": 1,
                "msg": "认证失败"
            }

        try:
            clear_content = content
            if type == 0:
                books_of_db = Book.query.filter(Book.book_title.like('%' + clear_content + '%')).paginate(
                    page=args["page"], per_page=20, error_out=False).items
            elif type == 1:
                books_of_db = Book.query.filter(Book.book_author.like('%' + clear_content + '%')).paginate(
                    page=args["page"], per_page=20, error_out=False).items
            else:
                books_of_db = Book.query.filter(Book.book_publish.like('%' + clear_content + '%')).paginate(
                    page=args["page"], per_page=20, error_out=False).items

        except Exception as e:
            return {
                "data": [],
                "status": 2,
                "msg": "数据库溜了"
            }

        if books_of_db:
            return {
                "status": 0,
                "msg": "搜索成功",
                "data": [{
                    "book_cover": book.book_cover,
                    "book_id": book.book_id,
                    "book_rate": book.book_rate,
                    "book_title": book.book_title,
                    "book_author": json.loads(book.book_author),
                    "book_last_number": book.book_last_number,
                    "book_db_id": book.book_db_id,
                    "book_publish": book.book_publish
                } for book in books_of_db]
            }

        else:
            ncu_search = NcuSearch()
            douban = Douban()
            data = []

            try:
                for book_info in ncu_search.get(content, args["page"]):
                    if book_info["book_key"]:
                        b = douban.search_by_isbn(book_info["book_key"])
                        if not b:
                            continue
                        book_info.update(b)

                        b = Book.query.filter_by(book_key=book_info["book_key"]).first()
                        if b:
                            continue
                        new_book = Book(book_author=book_info["book_author"])
                        new_book.book_cover = book_info["book_cover"]
                        new_book.book_rate = book_info["book_rate"]
                        new_book.book_content = book_info["book_content"]
                        new_book.book_publish = book_info["book_publish"]
                        new_book.book_last_number = len(
                            list(filter(lambda x: not x["is_borrowed"], book_info["data"])))
                        new_book.book_key = book_info["book_key"]
                        new_book.book_db_id = book_info["book_db_id"]
                        new_book.book_title = book_info["book_title"]
                        new_book.detail_data = json.dumps(book_info["data"])

                        db.session.add(new_book)
                        db.session.commit()

                        mydict = {
                            "book_cover": book_info["book_cover"],
                            "book_id": new_book.book_id,
                            "book_rate": book_info["book_rate"],
                            "book_title": book_info["book_title"],
                            "book_author": json.loads(book_info["book_author"]),
                            "book_last_number": new_book.book_last_number,
                            "book_db_id": book_info["book_db_id"],
                            "book_publish": book_info["book_publish"]
                        }
                        data.append(mydict)

                    else:
                        b = douban.search_by_isbn(book_info["book_title"])
                        if not b:
                            continue
                        book_info.update(b)

                        b = Book.query.filter_by(book_db_id=book_info["book_db_id"]).first()
                        if b:
                            continue
                        new_book = Book(book_author=book_info["book_author"])
                        new_book.book_cover = book_info["book_cover"]
                        new_book.book_rate = book_info["book_rate"]
                        new_book.book_content = book_info["book_content"]
                        new_book.book_publish = book_info["book_publish"]
                        new_book.book_last_number = len(
                            list(filter(lambda x: not x["is_borrowed"], book_info["data"])))
                        new_book.book_key = book_info["book_key"]
                        new_book.book_db_id = book_info["book_db_id"]
                        new_book.book_title = book_info["book_title"]
                        new_book.detail_data = json.dumps(book_info["data"])

                        db.session.add(new_book)
                        db.session.commit()

                        mydict = {
                            "book_cover": book_info["book_cover"],
                            "book_id": new_book.book_id,
                            "book_rate": book_info["book_rate"],
                            "book_title": book_info["book_title"],
                            "book_author": json.loads(book_info["book_author"]),
                            "book_last_number": new_book.book_last_number,
                            "book_db_id": book_info["book_db_id"],
                            "book_publish": book_info["book_publish"]
                        }
                        data.append(mydict)
                return {
                    "status": 0,
                    "msg": "搜索成功",
                    "data": data
                }
            except Exception as e:
                print(e)
                return {
                    "data": [],
                    "status": 3,
                    "msg": "服务器溜了"
                }