Code Duplication    Length = 64-71 lines in 2 locations

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

@@ 349-419 (lines=71) @@
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
                self.config.WEBSITE_TITLE.__str__()
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
            # TODO - G.M - 11-06-2018 - [emailTemplateURL] correct value for logo_url  # nopep8
390
            'logo_url': get_email_logo_frontend_url(self.config),
391
            # TODO - G.M - 11-06-2018 - [emailTemplateURL] correct value for login_url  # nopep8
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-485 (lines=64) @@
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
            self.config.WEBSITE_TITLE.__str__()
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
            # TODO - G.M - 11-06-2018 - [emailTemplateURL] correct value for logo_url  # nopep8
457
            'logo_url': get_email_logo_frontend_url(self.config),
458
            # TODO - G.M - 11-06-2018 - [emailTemplateURL] correct value for login_url  # nopep8
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(