def send(self, recipients: list, subject: str, text: str = None, html: str = None): from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['To'] = ', '.join(recipients) msg['From'] = f'{self.account_smtp.SENDER} <{self.account_smtp.USER}>' msg['Reply-To'] = self.account_smtp.USER if text: msg.attach(MIMEText(text, 'plain')) if html: msg.attach(MIMEText(html, 'html')) try: mail = smtplib.SMTP_SSL(self.account_smtp.SERVER) mail.login(self.account_smtp.USER, self.account_smtp.PASSWORD) mail.sendmail(self.account_smtp.USER, recipients, msg.as_string()) mail.quit() except Exception as e: logging.critical( f'error: {str(e)}, recipients: {recipients}, subject: {subject}, text: {text}, html: {html}' ) return str(e) return None
def post(cls, request): msg = Msg(request.data['subject'], int(request.data['template']['id']), request.data['template']['variables'], request.data['recipients']) try: EmailHandler.send_msg(msg) except Exception as e: logging.critical(f'error: {str(e)}, input_data: {request.data}') return Response({'success': False}) return Response({'success': True})
def get(self): #logging.critical("request.text: " + self.request.body.decode('utf-8')) try: self.set_status(200) customer = {'customer': 'Martin', 'price': 100} json_body = json.dumps(customer) self.write(json_body) except Exception as e: logging.critical("Error on the customer server " + str(e)) self.set_status(500) self.write("Error on the customer server " + str(e))
def _add_book_to_db(self, book: Book) -> Book: logging.critical("2") db = DataBase() session = Session(db.engine) try: session.add(book) session.commit() session.refresh(book) except SQLAlchemyError: logging.debug("Error in _add_book_to_db: {error}".format( error=str(SQLAlchemyError))) self.set_status(500) finally: session.close() return book
async def _send_book_to_view(self, book: Book): logging.critical("3") try: url = config.get('request_info_about_customer', 'base_url') http_client = AsyncHTTPClient() res = await http_client.fetch(url) http_client.fetch(url) if res.code != 200: self.set_status(500) self.write("Customer information not received") # logging.critical("res.text: " + res.text) dict_res = json.loads(res.body) book.customer = dict_res['customer'] book.price = dict_res['price'] except Exception as e: logging.critical("__send_book_to_view: " + str(e)) self.set_status(500) self.write("Error send book to view")
async def _get_customer_and_price(self, book: Book) -> Book: try: logging.critical("1") url = config.get('request_info_about_customer', 'base_url') http_client = AsyncHTTPClient() res = await http_client.fetch(url) if res.code != 200: self.set_status(500) self.write("Customer information not received") # logging.critical("res.text: " + res.text) dict_res = json.loads(res.body) book.customer = dict_res['customer'] book.price = dict_res['price'] except Exception as e: logging.critical("_get_customer_and_price: " + str(e)) self.set_status(500) self.write( "Error processing information about the customer and price") return book
async def post(self): logging.critical("0") try: self.set_status(200) # logging.critical("request.url: " + self.request.uri) # logging.critical("request.text: " + self.request.body.decode('utf-8')) data = json_decode(self.request.body) book = Book() book.author = data["author"] book.title_of_book = data["title"] book = await self._get_customer_and_price(book) self.loop = asyncio.get_event_loop() book = await self.loop.run_in_executor( self.application.executor, partial(self._add_book_to_db, book)) await self._send_book_to_view(book) self.write(book.title_of_book) except Exception as e: logging.critical("AddBook: " + str(e)) self.set_status(500) self.write("Error on the book server")