Code Duplication    Length = 62-69 lines in 2 locations

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

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