Code Duplication    Length = 66-69 lines in 2 locations

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

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