def post(self): parser = reqparse.RequestParser() parser.add_argument(Constants.k_user_id, type=int, help="User ID", location="form", required=True) parser.add_argument(Constants.k_user_token, type=str, help="User token", location="form", required=True) parser.add_argument(Constants.k_event_id, type=int, help="Event ID", location="headers", required=True) parser.add_argument(Constants.k_internal_id, type=int, help="Internal event ID", location="headers") parser.add_argument(ExpenseModel.k_expense_id, type=int, help="Expense ID", location="headers") parser.add_argument( ExpenseModel.k_creator_id, type=int, help="Expense creator ID", location="headers", required=True ) parser.add_argument(ExpenseModel.k_title, type=str, help="Expense title", location="headers") parser.add_argument(ExpenseModel.k_value, type=float, help="Expense value", location="headers") parser.add_argument(Constants.k_is_removed, type=str, help="Is expense removed", location="headers") parser.add_argument(ExpenseModel.k_creation_date, type=str, help="Expense creation date", location="headers") args = parser.parse_args() model = BaseResource.check_user_credentials_with_credentials( args[Constants.k_user_id], token=args[Constants.k_user_token] ) if not isinstance(model, PersonModel): # Return error description return model expense_id = args[ExpenseModel.k_expense_id] expense_model = ExpenseModel.find_expense(expense_id) expense_model.configure_with_dict(args) db.session.add(expense_model) db.session.commit() return expense_model.to_dict()
def configure_expense_with_dict(self, dict_model): if dict_model is not None and isinstance(dict_model, list): result = list(self.expenses) for expense_dict in dict_model: expense_id = expense_dict.get(self.k_expense_id) expense = ExpenseModel.find_expense(expense_id) expense.configure_with_dict(expense_dict) # Need commit immediately to get 'expense_id' db.session.add(expense) db.session.commit() if Constants.k_internal_id in expense_dict: self.internal_expense_ids[expense.expense_id] = expense_dict[Constants.k_internal_id] result.append(expense) self.expenses = result