Ejemplo n.º 1
0
 def get(self):
     args = self.parser.parse_args()
     page = args.get('page', None)
     if page is None:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     if g.user.role == conf.USER_ROLE_ADMIN:
         session = AppUtils.get_session()
         try:
             items = session.query(Item).order_by(desc(Item.id)).offset(
                 page * 10).limit(10).all()
             data = []
             for item in items:
                 item_dict = AppUtils.serialize(item)
                 data.append(item_dict)
             return ResponseClass.ok_with_data(data)
         except Exception as e:
             pass
         finally:
             session.close()
     else:
         session = AppUtils.get_session()
         try:
             items = session.query(Item).filter_by(isOn=True).order_by(
                 Item.name).offset(page * 10).limit(10).all()
             data = []
             for item in items:
                 item_dict = AppUtils.serialize(item)
                 data.append(item_dict)
             return ResponseClass.ok_with_data(data)
         except Exception as e:
             pass
         finally:
             session.close()
Ejemplo n.º 2
0
 def get(self, id):
     session = AppUtils.get_session()
     try:
         user = g.user
         if user.id != id:
             # 查询他人的信息
             query_user = session.query(User).filter_by(id=id).first()
             return ResponseClass.ok_with_data(query_user.get_minimal_data(
             )) if query_user is not None else ResponseClass.warn(
                 ResponseCode.USER_NOT_EXIST)
         else:
             ResponseClass.ok_with_data(user.get_self_data())
     finally:
         session.close()
Ejemplo n.º 3
0
 def post(self):
     if g.user.role == conf.USER_ROLE_USER:
         return ResponseClass.warn(ResponseCode.NOT_ROOT)
     else:
         session = AppUtils.get_session()
         id = request.json.get('id', None)
         print(id)
         item: Item
         if id is not None:
             # 修改items
             item = session.query(Item).filter_by(id=id).first()
         else:
             item = Item()
         item.name = request.json.get('name')
         item.detail = request.json.get('detail')
         item.credits = request.json.get('credits')
         item.isOn = request.json.get('isOn')
         item.img = request.json.get('img')
         if id is None:
             session.add(item)
         try:
             session.commit()
             return ResponseClass.ok_with_data({"id": item.id})
         finally:
             session.close()
Ejemplo n.º 4
0
 def get(self, user_id):
     session = AppUtils.get_session()
     try:
         from app.database_models import User
         q_user = session.query(User).filter_by(
             id=user_id).with_for_update().first()
         if q_user is None:
             return ResponseClass.warn(ResponseCode.USER_NOT_EXIST)
         else:
             # 判断是否点过赞,点过赞则警告
             from app.database_models import UserLikes
             result = session.query(UserLikes).filter_by(
                 user_id=g.user.id,
                 like_user=user_id).with_for_update().first()
             if result is None:
                 # 点赞
                 q_user.likes += 1
                 likes = UserLikes()
                 likes.user_id = g.user.id
                 likes.like_user = user_id
                 session.add(likes)
             else:
                 q_user.likes -= 1
                 session.delete(result)
             session.commit()
             return ResponseClass.ok_with_data(q_user.likes)
     finally:
         session.close()
Ejemplo n.º 5
0
 def post(self):
     ids = request.json.get("ids", [])
     if len(ids) == 0:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     session = AppUtils.get_session()
     total_credits = g.user.credits
     try:
         need_paid = 0
         cart_item = []
         for id in ids:
             item = session.query(Cart).filter_by(id=id).first()
             if item is not None:
                 need_paid += item.item.credits
                 cart_item.append(item)
         if need_paid > total_credits:
             return ResponseClass.warn(ResponseCode.NO_ENOUGH_CREDITS)
         else:
             # cart_id -> repository
             user = session.query(User).filter_by(id=g.user.id).first()
             user.credits -= need_paid
             for cart in cart_item:
                 repo = Repository()
                 repo.user_id = g.user.id
                 repo.item_id = cart.item_id
                 session.add(repo)
                 session.delete(cart)
             session.commit()
             return ResponseClass.ok_with_data(user.credits)
     except Exception as e:
         pass
     finally:
         session.close()
 def get(self, offset: int):
     session = AppUtils.get_session()
     try:
         result = Code.get_code(session, g.user.id, offset)
         result = list(map(Code.get_public_dict, result))
         return ResponseClass.ok_with_data(result)
     finally:
         session.close()
Ejemplo n.º 7
0
 def get(self):
     # 普通用户无权限
     if g.user.role == USER_ROLE_USER:
         return ResponseClass.warn(ResponseCode.NOT_ROOT)
     else:
         session = app_utils.AppUtils.get_session()
         try:
             users = session.query(User).all()
             data = [user.get_minimal_data() for user in users]
             return ResponseClass.ok_with_data(data)
         finally:
             session.close()
Ejemplo n.º 8
0
 def get(self):
     args = self.parser.parse_args()
     page_num = int(args.get('page', None))
     if page_num is None or type(page_num) != int or page_num <= 0:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     # 将page_num-1,适应计算
     page_num = page_num - 1
     session = AppUtils.get_session()
     from app.database_models import Threads
     threads = session.query(Threads).filter_by(user_id=g.user.id).offset(
         page_num * self.page_num_per_request).limit(
             self.page_num_per_request).all()
     threads = [tr.get_public_dict() for tr in threads]
     return ResponseClass.ok_with_data(threads)
Ejemplo n.º 9
0
 def post(self):
     if g.user.role == conf.USER_ROLE_USER:
         return ResponseClass.warn(ResponseCode.NOT_ROOT)
     else:
         session = AppUtils.get_session()
         try:
             item = session.query(Item).filter_by(
                 id=request.json.get("id")).first()
             if item is not None:
                 item.isOn = not item.isOn
                 session.commit()
                 return ResponseClass.ok_with_data(item.isOn)
             else:
                 return ResponseClass.warn(ResponseCode.ITEM_NOT_FOUND)
         finally:
             session.close()
Ejemplo n.º 10
0
    def post(self):
        if g.user.role == USER_ROLE_USER:
            return ResponseClass.warn(ResponseCode.NOT_ROOT)
        else:
            id = request.json.get("id", None)
            if id is None:
                return ResponseClass.warn(ResponseCode.USER_NOT_EXIST)
            session = app_utils.AppUtils.get_session()
            try:
                user = session.query(User).filter_by(id=int(id)).first()
                if user is None:
                    return ResponseClass.warn(ResponseCode.USER_NOT_EXIST)

                user.enable = not user.enable
                session.commit()
                return ResponseClass.ok_with_data(user.enable)
            finally:
                session.close()
Ejemplo n.º 11
0
 def post(self):
     username = request.json.get('username', None)
     password = request.json.get('password', None)
     if username is None or password is None:
         return jsonify(code=ResponseCode.FORMAT_ERROR)
     # 查找用户
     session = AppUtils.get_session()
     try:
         user = session.query(User).filter_by(username=username).first()
         if user is None:
             user = session.query(User).filter_by(mail=username).first()
         if user is None:
             return jsonify(code=ResponseCode.USER_NOT_EXIST)
         if not user.verify_password_only(password):
             return jsonify(code=ResponseCode.PASSWORD_ERROR)
         # 用户验证成功
         return ResponseClass.ok_with_data(user.get_self_data())
     finally:
         session.close()
 def get(self):
     args = self.parser.parse_args()
     thread_id = args.get("thread_id", None)
     page = args.get("page", None)
     if thread_id is None or page is None:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     session = AppUtils.get_session()
     try:
         from app.database_models import Comments
         comments = session.query(Comments) \
             .filter_by(threads_id=thread_id) \
             .order_by(desc(Comments.create_date)) \
             .offset(int(page) * 10) \
             .limit(10) \
             .all()
         comments = [item.get_public_dict() for item in comments]
         return ResponseClass.ok_with_data(comments)
     finally:
         session.close()
Ejemplo n.º 13
0
 def get(self):
     args = self.parser.parse_args()
     page = args.get('page', None)
     if page is None:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     session = AppUtils.get_session()
     try:
         repository_items = session.query(Repository).filter_by(
             user_id=g.user.id).order_by(
                 conf.database.desc(Repository.create_date)).offset(
                     page * 10).limit(10).all()
         data = []
         for repository_item in repository_items:
             item_data = AppUtils.serialize(repository_item.item)
             item_data['repo_id'] = repository_item.id
             data.append(item_data)
         return ResponseClass.ok_with_data(data)
     except Exception as e:
         pass
     finally:
         session.close()
Ejemplo n.º 14
0
 def post(self):
     code = request.json.get('code', None)
     new_password = request.json.get('new_password', None)
     if code is None or new_password is None or not User.password_illigal(
             new_password):
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     else:
         user_id = cache.get(code)
         if user_id is None:
             return ResponseClass.warn(ResponseCode.SERVER_FORBIDDEN)
         session = AppUtils.get_session()
         try:
             user = session.query(User).filter_by(id=user_id).first()
             if user is None:
                 return ResponseClass.warn(ResponseCode.USER_NOT_EXIST)
             user.hash_password(new_password)
             session.commit()
             cache.delete(code)
             cache.delete(user_id)
             return ResponseClass.ok_with_data(user.get_self_data())
         finally:
             session.close()
Ejemplo n.º 15
0
 def post(self):
     url = request.json.get("url", None)
     url = AppUtils.get_local_path(url)
     print("url:", url)
     session = AppUtils.get_session()
     from app.database_models import Code
     code = session.query(Code).filter_by(local_path=url).first()
     if code is None:
         return ResponseClass.warn(ResponseCode.FILE_NOT_EXIST)
     session.close()
     # 先存入CodeResult
     from app.database_models import CodeResult
     code_result = CodeResult()
     code_result.code_id = code.id
     code_result.user_id = g.user.id
     from app_config import code_manager
     session = AppUtils.add_to_sql(code_result)
     block = CodeBlock(user_id=code_result.user_id, task_id=code_result.id)
     session.close()
     return_value = code_manager.add_task(block)
     if return_value:
         return ResponseClass.ok_with_data({"code_id": code_result.code_id})
     else:
         return ResponseClass.warn(ResponseCode.SUBMIT_ERROR)
Ejemplo n.º 16
0
 def get(self):
     user_id = g.user.id
     args = self.parser.parse_args()
     code_id = args.get('code_id', None)
     if code_id is None:
         return ResponseClass.warn(ResponseCode.FORMAT_ERROR)
     else:
         from app_config import SQLSession
         session = SQLSession()
         try:
             from app.database_models import CodeResult
             result = session.query(CodeResult).filter_by(
                 user_id=user_id, code_id=code_id).first()
             if result is None:
                 return ResponseClass.warn(ResponseCode.FILE_NOT_EXIST)
             return ResponseClass.ok_with_data({
                 "status": result.status,
                 "result": result.result
             })
         except Exception as e:
             print(e)
             return ResponseClass.warn(ResponseCode.SERVER_ERROR)
         finally:
             session.close()
Ejemplo n.º 17
0
 def get(self):
     token = g.user.generate_auth_token()
     return ResponseClass.ok_with_data({'token': token})
Ejemplo n.º 18
0
 def get(self):
     token = get_upload_token()
     if token == "":
         return ResponseClass.warn(ResponseCode.SERVER_ERROR)
     else:
         return ResponseClass.ok_with_data(token)
Ejemplo n.º 19
0
 def get(self):
     return ResponseClass.ok_with_data({'credits': g.user.credits})