Code Duplication    Length = 62-69 lines in 2 locations

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

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