Ejemplo n.º 1
0
 def update_transfer_by_id(self, transfer_id: int, data: Dict[str,
                                                              str]) -> bool:
     """
     update transfer by id
     :param transfer_id:
     :param data:
     :return:
     """
     if not transfer_id or not data:
         raise RequestDataEmpty("Transfer data is empty")
     if not self.input_validate.validate_json(data, transfer_schema):
         self.logger.error("All transfer field input must be required.")
         raise ValidateFail("Transfer update validation fail")
     try:
         self.logger.info("Update transfer info by id %s", transfer_id)
         return TransferModel.update_transfer(
             transfer_id,
             TransferModel(year=int(data["year"]),
                           month=data["month"],
                           total_jpy=int(data["total_jpy"]),
                           total_mmk=int(data["total_mmk"])))
     except SQLAlchemyError as error:
         self.logger.error(
             "Transfer update fail. id %s, error %s, custom error: %s",
             transfer_id, traceback.format_exc(), error)
         raise SQLCustomError(description="Update transfer by ID SQL ERROR")
     except SQLCustomError as error:
         self.logger.error(
             "Transfer update fail. id %s, error %s, custom error: %s",
             transfer_id, traceback.format_exc(), error)
         raise SQLCustomError(description="No record for requested address")
Ejemplo n.º 2
0
 def get_all_transfers(self,
                       page: int = 1,
                       per_page: int = 20) -> (List[Dict[str, Any]], int):
     """
     get all transfers
     :params: page
     :params: per_page
     :return:
     """
     self.logger.info("Get all transfers list")
     try:
         transfers = TransferModel.get_all_transfers(page, per_page)
         return {
             "transfers":
             [transfer.as_dict() for transfer in transfers.items],
             "total_count": transfers.total,
             "current_page": transfers.page,
             "next_page": transfers.next_num,
             "prev_page": transfers.prev_num,
             "pages": transfers.pages
         }
     except SQLAlchemyError:
         self.logger.error("Get all transfer fail. error %s",
                           traceback.format_exc())
         raise SQLCustomError(description="GET transfer SQL ERROR")
Ejemplo n.º 3
0
 def get_all_transfers(self) -> List[Dict[str, Any]]:
     """
     get all transfers
     :return:
     """
     self.logger.info("Get all transfers list")
     try:
         return [transfer.as_dict() for transfer in TransferModel.get_all_transfers()]
     except SQLAlchemyError:
         self.logger.error("Get all transfer fail. error %s", traceback.format_exc())
         raise SQLCustomError(description="GET transfer SQL ERROR")
Ejemplo n.º 4
0
    def get(self):
        user_token, error, code = validate_token(request.headers)
        if error:
            return error, code

        token = TokenModel.find_by_token(user_token)
        if not token:
            return {"message": "Token is incorrect"}, 403

        item_id = request.args.get('id')
        if not TransferModel.find_transfer(token.user_id, item_id):
            return {"message": "This link is not available"}, 400

        if ItemModel.transfer(item_id, token.user_id):
            return {"message": "Data transmission error"}, 400

        if TransferModel.delete_transfer(token.user_id, item_id):
            return {"message": "Data deletion error"}, 400

        return {"message": "Data transfer completed successfully!"}, 200
Ejemplo n.º 5
0
 def create_transfer(self, data: Dict[str, str]) -> bool:
     """
     create new transfer
     :param data: data dict includes year, month, jpy amount, mmk amount
     :return: True if creation success else False
     """
     if not data:
         raise RequestDataEmpty("Transfer data is empty")
     if not self.input_validate.validate_json(data, transfer_schema):
         self.logger.error("All scheme field input must be required.")
         raise ValidateFail("Transfer validation fail")
     try:
         return TransferModel.create_transfer(
             TransferModel(year=int(data["year"]),
                           month=data["month"],
                           total_jpy=int(data["total_jpy"]),
                           total_mmk=int(data["total_mmk"])))
     except SQLAlchemyError:
         self.logger.error("Transfer create fail. error %s",
                           traceback.format_exc())
         raise SQLCustomError("Transfer create fail")
Ejemplo n.º 6
0
 def delete_transfer_by_id(self, transfer_id: int) -> bool:
     """
     delete transfer by id
     :param transfer_id:
     :return:
     """
     try:
         self.logger.info("Delete transfer by id", transfer_id)
         return TransferModel.delete_transfer_by_id(transfer_id)
     except SQLAlchemyError:
         self.logger.error("Transfer delete fail. id %s, error %s", transfer_id, traceback.format_exc())
         raise SQLCustomError(description="Delete transfer by ID SQL ERROR")
Ejemplo n.º 7
0
 def get_transfer_by_id(self, transfer_id: int) -> Dict[str, Any]:
     """
     get users by id
     :param transfer_id:
     :return: transfer list of dict
     """
     self.logger.info("Get transfer record by id %s", transfer_id)
     try:
         transfer = TransferModel.get_transfer_by_id(transfer_id)
         return transfer.as_dict() if transfer else {}
     except SQLAlchemyError:
         self.logger.error("Get transfer record by id fail. id %s. error %s", transfer_id, traceback.format_exc())
         raise SQLCustomError(description="GET transfer by ID SQL ERROR")
Ejemplo n.º 8
0
 def get_all_transfers(self, page: int = 1) -> (List[Dict[str, Any]], int):
     """
     get all transfers
     :params: page
     :return:
     """
     self.logger.info("Get all transfers list")
     try:
         transfers = TransferModel.get_all_transfers(page)
         return [transfer.as_dict()
                 for transfer in transfers.items], transfers.total
     except SQLAlchemyError:
         self.logger.error("Get all transfer fail. error %s",
                           traceback.format_exc())
         raise SQLCustomError(description="GET transfer SQL ERROR")
Ejemplo n.º 9
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id',
                            type=int,
                            required=True,
                            help="This field cannot be left blank!")
        parser.add_argument('login',
                            type=str,
                            required=True,
                            help="This field cannot be left blank!")
        data = parser.parse_args()
        user_token, error, code = validate_token(request.headers)
        if error:
            return error, code

        token = TokenModel.find_by_token(user_token)
        if not token:
            return {"message": "Token is incorrect"}, 403

        if not ItemModel.find_by_id(data['id'], token.user_id):
            return {
                'message':
                "An item with id '{}' does not exist.".format(data['id'])
            }, 400

        if not UserModel.find_by_login(data['login']):
            return {"message": "A user with that login does not exist"}, 400

        if UserModel.find_by_login(data['login']).id == token.user_id:
            return {"message": "You can not transfer item to yourself"}, 400

        transfer_user = UserModel.find_by_login(data['login']).id
        transfer_item = TransferModel.save_to_db(transfer_user, data['id'])
        if transfer_item == "true":
            return {"message": "Item has already been transfered"}, 400

        response = "http://localhost:5000/get?id={0}".format(data['id'])

        return {"message": response}, 200