def test_model_save_to_db(self): card = CardModel(self.card_info['card_owner'], self.card_info['number'], self.card_info['date'], self.card_info['payment_method']) account = AccountModel.query.first() account.cards.append(card) card.save_to_db() self.assertEqual(1, CardModel.find_by_id(1).id)
def test_get_existent_card_from_non_owner_account(self): card = CardModel(self.card_info['card_owner'], self.card_info['number'], self.card_info['date'], self.card_info['payment_method']) account = AccountModel.query.first() account.cards.append(card) card.save_to_db() response = self.app.get('/api/account/2/card/1', headers=self.auth2, follow_redirects=True) self.assertEqual(409, response.status_code)
def get(self, account_id, idd): account = AccountModel.find_by_id(account_id) card = CardModel.find_by_id(idd) if card is not None and account is not None: if card in account.cards: return {'card': card.json()}, 200 return { 'message': "This account doesn't have a card with id [{}] ".format(idd) }, 409 elif card is None: return {'message': "Card with id [{}] Not found".format(idd)}, 404 return {'message': "Account with id [{}] Not found".format(idd)}, 404
def delete(self, account_id, idd): account = AccountModel.find_by_id(account_id) card = CardModel.find_by_id(idd) if card is not None and account is not None: if card in account.cards: card.delete_from_db() return {"Message": "Card deleted correctly"}, 200 return { 'message': "This account doesn't have an card with id [{}] ".format(idd) }, 409 elif card is None: return {'message': "Card with id [{}] Not found".format(idd)}, 404 return {'message': "Account with id [{}] Not found".format(idd)}, 404
def json(self): articles_json = [article.json() for article in self.articles] card = CardModel.find_by_id(self.card_id) address = AddressModel.find_by_id(self.address_id) return { "id": self.id, "id_user": self.id_user, "date": self.date, "total": self.total, "shipping": self.shipping, "taxes": self.taxes, "state": self.state, "send_type": self.send_type, "card": card.json(), "address": address.json(), "articles": articles_json, }
def post(self, account_id, idd=None): parser = reqparse.RequestParser() account = AccountModel.find_by_id(account_id) if account is None: return { 'message': "Account with id [{}] Not found".format(account_id) }, 404 if len(account.cards) == 2: return { 'message': "Account with id [{}] cannot have more cards".format( account_id) }, 403 # define the input parameters need and its type parser.add_argument('card_owner', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('date', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('payment_method', type=str, required=True, help="This field cannot be left blanck") data = parser.parse_args() card = CardModel(data['card_owner'], data['number'], data['date'], data['payment_method']) account.cards.append(card) account.save_to_db() return {"Message": "Card saved correctly"}, 200
def _prepare_ticket_pdf(self, html_name, order: OrdersModel): html = open(email_templates[html_name]) card = CardModel.find_by_id(order.card_id) address = AddressModel.find_by_id(order.address_id) self.parser = BeautifulSoup(html, 'lxml') # Añadimos la fecha del pedido self._replace_content(self, "order_date", order.date) self._replace_content(self, "order_id", "Pedido " + str(date.today().strftime("%Y")) + "-" + str(order.id) + " QRL 02154") # Añadimos los productos con su respectiva cantidad y precio products = self._get_component(self, "products") all_products = self._get_component(self, "all_products") tmp = self._get_copy_children(self,"products") # 0: Quant x Book Title / 1: Price products.contents = [] all_products.contents = [] for art in order.articles: book_title = BookModel.find_by_id(art.book_id).name tmp_products = copy.copy(products) quant, price = copy.copy(tmp[0]), copy.copy(tmp[1]) self._replace_component_content(self, quant, str(art.quant) + " x " + book_title) self._replace_component_content(self, price, str(art.price) + "€") tmp_products.append(quant) tmp_products.append(price) all_products.append(tmp_products) # Añadimos el tipo de envio con su respectivo precio sending = self._get_children(self, "send_type") send_type, price = sending[0], sending[1] # 0: 1 x Envio / 1: Price envio = "1 x " if order.send_type == 1: envio += "Envío Estándard" elif order.send_type == 2: envio += "Envío Estándard Plus" else: envio += "Envío Ultra Express" self._replace_component_content(self, send_type, envio) self._replace_component_content(self, price, str(order.shipping) + "€") # Añadimos los impuestos self._replace_content(self, "taxes", str(order.taxes) + "€") # Añadimos el precio total totals = self._get_children(self, "totals") n_articles, t_price = totals[0], totals[1] # 0: Total n articles / 1: Total price self._replace_component_content(self, n_articles, "Total "+ str(len(order.articles)) + " artículo/s") self._replace_component_content(self, t_price, str(order.total) + "€") # Añadimos tambien el precio en pesetas self._replace_content(self, "pesetas", "En pesetas : " + str(round(order.total*166.386,2)) + " ptas") # Añadimos la dirección de envio direction = self._get_children(self, "dirección") name, street, cp, telf = direction[0], direction[1], direction[2], direction[3] self._replace_component_content(self, name, address.name + " " + address.surnames) self._replace_component_content(self, street, address.street + ", " + str(address.number)) self._replace_component_content(self, cp, str(address.cp) + ", " + address.province) self._replace_component_content(self, telf, "Tfno: " + str(address.telf)) # Añadimos la tarjeta con la que se realizó el pago self._replace_content(self, "vendor", card.payment_method + " **** **** **** " + str(card.number)[-4:]) # Generamos el pdf self.pdf = BytesIO() HTML(string=str(self.parser)).write_pdf(target=self.pdf) html.close()