Code Duplication    Length = 62-69 lines in 2 locations

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

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