Code Duplication    Length = 62-69 lines in 2 locations

backend/tracim_backend/lib/mail_notifier/notifier.py 2 locations

@@ 353-421 (lines=69) @@
350
                message
351
            )
352
353
    def notify_created_account(
354
            self,
355
            user: User,
356
            password: str,
357
    ) -> None:
358
        """
359
        Send created account email to given user.
360
361
        :param password: choosed password
362
        :param user: user to notify
363
        """
364
        # TODO BS 20160712: Cyclic import
365
        logger.debug(self, 'user: {}'.format(user.user_id))
366
        logger.info(self, 'Sending asynchronous email to 1 user ({0})'.format(
367
            user.email,
368
        ))
369
370
        async_email_sender = EmailSender(
371
            self.config,
372
            self._smtp_config,
373
            self.config.EMAIL_NOTIFICATION_ACTIVATED
374
        )
375
376
        subject = \
377
            self.config.EMAIL_NOTIFICATION_CREATED_ACCOUNT_SUBJECT \
378
            .replace(
379
                EST.WEBSITE_TITLE,
380
                str(self.config.WEBSITE_TITLE)
381
            )
382
        message = MIMEMultipart('alternative')
383
        message['Subject'] = subject
384
        message['From'] = self._get_sender()
385
        message['To'] = formataddr((user.get_display_name(), user.email))
386
387
        text_template_file_path = self.config.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT  # nopep8
388
        html_template_file_path = self.config.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML  # nopep8
389
390
        context = {
391
            'user': user,
392
            'password': password,
393
            'logo_url': get_email_logo_frontend_url(self.config),
394
            'login_url': get_login_frontend_url(self.config),
395
        }
396
        translator = Translator(self.config, default_lang=user.lang)
397
        body_text = self._render_template(
398
            mako_template_filepath=text_template_file_path,
399
            context=context,
400
            translator=translator
401
        )
402
403
        body_html = self._render_template(
404
            mako_template_filepath=html_template_file_path,
405
            context=context,
406
            translator=translator
407
        )
408
409
        part1 = MIMEText(body_text, 'plain', 'utf-8')
410
        part2 = MIMEText(body_html, 'html', 'utf-8')
411
412
        # Attach parts into message container.
413
        # According to RFC 2046, the last part of a multipart message,
414
        # in this case the HTML message, is best and preferred.
415
        message.attach(part1)
416
        message.attach(part2)
417
418
        send_email_through(
419
            config=self.config,
420
            sendmail_callable=async_email_sender.send_mail,
421
            message=message
422
        )
423
424
    def notify_reset_password(
@@ 424-485 (lines=62) @@
421
            message=message
422
        )
423
424
    def notify_reset_password(
425
            self,
426
            user: User,
427
            reset_password_token: str,
428
    ) -> None:
429
        """
430
        Reset password link for user
431
        :param user: user to notify
432
        :param reset_password_token: token for resetting password
433
        """
434
        logger.debug(self, 'user: {}'.format(user.user_id))
435
        logger.info(self, 'Sending asynchronous email to 1 user ({0})'.format(
436
            user.email,
437
        ))
438
        translator = Translator(self.config, default_lang=user.lang)
439
        async_email_sender = EmailSender(
440
            self.config,
441
            self._smtp_config,
442
            self.config.EMAIL_NOTIFICATION_ACTIVATED
443
        )
444
        subject = self.config.EMAIL_NOTIFICATION_RESET_PASSWORD_SUBJECT.replace(
445
            EST.WEBSITE_TITLE,
446
            str(self.config.WEBSITE_TITLE)
447
        )
448
        message = MIMEMultipart('alternative')
449
        message['Subject'] = subject
450
        message['From'] = self._get_sender()
451
        message['To'] = formataddr((user.get_display_name(), user.email))
452
453
        text_template_file_path = self.config.EMAIL_NOTIFICATION_RESET_PASSWORD_TEMPLATE_TEXT  # nopep8
454
        html_template_file_path = self.config.EMAIL_NOTIFICATION_RESET_PASSWORD_TEMPLATE_HTML  # nopep8
455
        # TODO - G.M - 2018-08-17 - Generate token
456
        context = {
457
            'user': user,
458
            'logo_url': get_email_logo_frontend_url(self.config),
459
            'reset_password_url': get_reset_password_frontend_url(self.config, token=reset_password_token),  # nopep8
460
        }
461
        body_text = self._render_template(
462
            mako_template_filepath=text_template_file_path,
463
            context=context,
464
            translator=translator,
465
        )
466
467
        body_html = self._render_template(
468
            mako_template_filepath=html_template_file_path,
469
            context=context,
470
            translator=translator,
471
        )
472
473
        part1 = MIMEText(body_text, 'plain', 'utf-8')
474
        part2 = MIMEText(body_html, 'html', 'utf-8')
475
476
        # Attach parts into message container.
477
        # According to RFC 2046, the last part of a multipart message,
478
        # in this case the HTML message, is best and preferred.
479
        message.attach(part1)
480
        message.attach(part2)
481
482
        send_email_through(
483
            config=self.config,
484
            sendmail_callable=async_email_sender.send_mail,
485
            message=message
486
        )
487
488
    def _render_template(