Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email producer #425

Merged
merged 5 commits into from Sep 29, 2019
Merged

Add email producer #425

merged 5 commits into from Sep 29, 2019

Conversation

GregEremeev
Copy link
Contributor

Пример использования

class TariffSetReportEmailProducer(BaseEmailProducer):

    TARIFFS_TABLE_TEMPLATE = '''
      <table>
        <tr>
          <th>Название</th>
          <th>Название по квитанции</th>
          <th>Ед. измерения</th>
          <th>Тип помещения</th>
          <th>Значение</th>
        </tr>
        {tariffs}
      </table>            
    '''

    TARIFF_TEMPLATE = '''
      <tr>
        <td>{name}</th>
        <td>{name_by_invoice}</td>
        <td>{measure_unit}</td>
        <td>{object_type}</td>
        <td>{value}</td>
      </tr>  
    '''

    BODY_TEMPLATE = '''
      <!doctype html>
      <html lang="ru">
        <head>
          <meta charset="utf-8">
          <style>
            ul {{
              list-style-type: none;
              padding-left: 0;
            }}

            table {{
              font-family: arial, sans-serif;
              border-collapse: collapse;
              width: 100%;
            }}

            td, th {{
              border: 1px solid #dddddd;
              text-align: left;
              padding: 8px;
            }}

            tr:nth-child(even) {{
              background-color: #dddddd;
            }}
          </style>
        </head>
        <body>
          <div>
            <ul>
              <li><strong>Инициатор:</strong> {initiator}</li>
              <li>
                <strong>
                  Дата актуализации в сервисе НСИ-Тарифы:
                </strong> {actual_date}
              </li>
              <li>
                <strong>
                  Начало действия Набора тарифов:
                </strong> {begin_date}
              </li>
              <li>
                <strong>
                  Окончание действия Набора тарифов:
                </strong> {end_date}
              </li>
            </ul>
            {tariffs_table}
          </div>
        </body>
      </html>
    '''

    def __init__(
            self, emails_to: List, email_from: str,
            ts_formatter: TariffSetFormatter):
        super().__init__(emails_to=emails_to, email_from=email_from)
        self._ts_formatter = ts_formatter
        self._tariff_set = ts_formatter.tariff_set

    def _get_email_subject(self):
        building = self._tariff_set.building
        subject = (
            f'Изменение тарифов - {building.juristic_address} - '
            f'{self._ts_formatter.begin_date} - '
            f'{self._ts_formatter.end_date}')

        return subject

    def _get_html_email_body(self):
        initiator = self._tariff_set.last_author.email
        actual_date = self._tariff_set.actualization_date.strftime('%d-%m-%Y')
        begin_date = self._ts_formatter.begin_date
        end_date = self._ts_formatter.end_date

        tariffs = ''
        for tariff in self._ts_formatter.tariffs:
            tariffs += self.TARIFF_TEMPLATE.format(**tariff)
        tariffs_table = self.TARIFFS_TABLE_TEMPLATE.format(tariffs=tariffs)
        body = self.BODY_TEMPLATE.format(
            initiator=initiator, actual_date=actual_date,
            begin_date=begin_date, end_date=end_date,
            tariffs_table=tariffs_table)

        return body

Copy link
Contributor

@MaximZemskov MaximZemskov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возможно следует добавить в settings настройки связанные с email

EMAIL_USE_TLS = True
EMAIL_HOST = os.environ.get('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
EMAIL_PORT = os.environ.get('EMAIL_PORT', 587)

Так же Даниил Солодилов просил перейти с smtp.gmail.com на smtp-relay.gmail.com. Инструкция по smpt-rely тут https://support.google.com/a/answer/176600?hl=ru

email.attach_alternative(html_body, 'text/html')

if self._files:
for file_wrapper in self._files:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что за объект file_wrapper?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

там в тайпинге написано: EmailFileWrapper

self._emails_bcc = emails_bcc
self._files = files

def _get_email_subject(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Предлагаю сделать методы _get_email_subject, _get_plain_email_body, _get_html_email_body публичными т.к. подразумевается их переопределение в дочерних классах

@GregEremeev GregEremeev force-pushed the add-email-producer branch 3 times, most recently from ad79614 to b2ea778 Compare August 13, 2019 14:47
@pahaz
Copy link
Contributor

pahaz commented Sep 29, 2019

Может хоть один тест написать?

@pahaz pahaz merged commit 6c9874a into master Sep 29, 2019
@pahaz pahaz deleted the add-email-producer branch September 29, 2019 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants