Esempio n. 1
0
    def response_change(self, request, obj):
        if 'send_test_email' not in request.POST.keys():
            return super(EmailSettingsAdmin,
                         self).response_change(request, obj)

        email_settings = EmailSettings.get_solo()

        with translation.override(
                language=BackendSettings.get_solo().language):
            subject = _('DSMR-reader email test')
            body = _('Test for your email settings.')

        try:
            dsmr_backend.services.email.send(email_settings.email_to,
                                             subject=subject,
                                             body=body)
        except Exception as error:
            self.message_user(request,
                              _('Failed to send test email: {}'.format(error)),
                              level=logging.ERROR)
            return HttpResponseRedirect('.')

        self.message_user(
            request,
            _('Email sent succesfully, please check your email inbox (and spam folder).'
              ))
        return HttpResponseRedirect('.')
Esempio n. 2
0
def run(scheduled_process):
    """ Creates a new statistics backup and sends it per email. """
    email_backup_settings = EmailBackupSettings.get_solo()

    if not email_backup_settings.interval:
        logger.debug(
            ' - Email backup interval not set, skipping backup for a day')
        return scheduled_process.delay(timezone.timedelta(days=1))

    temp_dir = tempfile.TemporaryDirectory()
    backup_file = dsmr_backup.services.backup.create_partial(
        folder=temp_dir.name, models_to_backup=(DayStatistics, HourStatistics))

    with translation.override(language=BackendSettings.get_solo().language):
        subject = _('DSMR-reader day/hour statistics backup')
        body = _(
            'This is an automated email, containing a backup of the day and hour statistics in the attachment.'
        )

    email_settings = EmailSettings.get_solo()
    dsmr_backend.services.email.send(to=email_settings.email_to,
                                     subject=subject,
                                     body=body,
                                     attachment=backup_file)

    scheduled_process.delay(
        timezone.timedelta(days=email_backup_settings.interval))
Esempio n. 3
0
def send(email_from, email_to, subject, body, attachment=None):
    """ Sends an email using the outgoing email settings. """
    email_settings = EmailSettings.get_solo()

    logger.debug('Email: Preparing to send email using mail server %s:%s',
                 email_settings.host, email_settings.port)
    email_backend = EmailBackend(host=email_settings.host,
                                 port=email_settings.port,
                                 username=email_settings.username,
                                 password=email_settings.password,
                                 use_tls=email_settings.use_tls,
                                 use_ssl=email_settings.use_ssl)

    # Force translations.
    message = mail.EmailMessage(
        subject=subject,
        body=body,
        from_email=email_from,
        to=[email_to],
        connection=email_backend,
    )

    if attachment:
        message.attach_file(attachment)

    logger.debug('Email backup: Sending an email to %s (%s)', email_to,
                 subject)
    message.send()
Esempio n. 4
0
def send(email_from: str,
         email_to: str,
         subject: str,
         body: str,
         attachment: str = None) -> None:
    """ Sends an email using the outgoing email settings. """
    email_settings = EmailSettings.get_solo()

    logger.debug('Email: Preparing to send email using mail server %s:%s',
                 email_settings.host, email_settings.port)
    email_backend = EmailBackend(host=email_settings.host,
                                 port=email_settings.port,
                                 username=email_settings.username,
                                 password=email_settings.password,
                                 use_tls=email_settings.use_tls,
                                 use_ssl=email_settings.use_ssl)

    # Prevent hanging processes, ensure there is always a timeout set.
    email_backend.timeout = email_backend.timeout if email_backend.timeout is not None else 30

    message = mail.EmailMessage(
        subject=subject,
        body=body,
        from_email=email_from,
        to=[email_to],
        connection=email_backend,
    )

    if attachment:
        message.attach_file(attachment)

    logger.debug('Email backup: Sending an email to %s (%s)', email_to,
                 subject)
    message.send()
Esempio n. 5
0
 def setUp(self):
     es = EmailSettings.get_solo()
     es.host = 'host'
     es.port = 1234
     es.username = '******'
     es.password = '******'
     es.use_tls = True
     es.save()
Esempio n. 6
0
 def render_change_form(self,
                        request,
                        context,
                        add=False,
                        change=False,
                        form_url='',
                        obj=None):
     context.update(dict(email_address=EmailSettings.get_solo().email_to))
     return super(EmailBackupSettingsAdmin,
                  self).render_change_form(request, context, add, change,
                                           form_url, obj)
Esempio n. 7
0
 def get_context_data(self, **kwargs):
     context_data = super(Configuration, self).get_context_data(**kwargs)
     # 20+ queries, we should cache this at some point.
     context_data.update(
         dict(
             api_settings=APISettings.get_solo(),
             backend_settings=BackendSettings.get_solo(),
             backup_settings=BackupSettings.get_solo(),
             consumption_settings=ConsumptionSettings.get_solo(),
             datalogger_settings=DataloggerSettings.get_solo(),
             dropbox_settings=DropboxSettings.get_solo(),
             email_settings=EmailSettings.get_solo(),
             frontend_settings=FrontendSettings.get_solo(),
             mindergas_settings=MinderGasSettings.get_solo(),
             mqtt_broker_settings=MQTTBrokerSettings.get_solo(),
             mqtt_jsondaytotals_settings=JSONDayTotalsMQTTSettings.get_solo(
             ),
             mqtt_splittopicdaytotals_settings=
             SplitTopicDayTotalsMQTTSettings.get_solo(),
             mqtt_jsoncurrentperiodtotals_settings=
             JSONCurrentPeriodTotalsMQTTSettings.get_solo(),
             mqtt_splittopiccurrentperiodtotals_settings=
             SplitTopicCurrentPeriodTotalsMQTTSettings.get_solo(),
             mqtt_jsongasconsumption_settings=JSONGasConsumptionMQTTSettings
             .get_solo(),
             mqtt_splittopicgasconsumption_settings=
             SplitTopicGasConsumptionMQTTSettings.get_solo(),
             mqtt_splittopicmeterstatistics_settings=
             SplitTopicMeterStatisticsMQTTSettings.get_solo(),
             mqtt_jsontelegram_settings=JSONTelegramMQTTSettings.get_solo(),
             mqtt_rawtelegram_settings=RawTelegramMQTTSettings.get_solo(),
             mqtt_splittopictelegram_settings=SplitTopicTelegramMQTTSettings
             .get_solo(),
             notification_settings=NotificationSetting.get_solo(),
             pvoutput_api_settings=PVOutputAPISettings.get_solo(),
             pvoutput_addstatus_settings=PVOutputAddStatusSettings.get_solo(
             ),
             retention_settings=RetentionSettings.get_solo(),
             weather_settings=WeatherSettings.get_solo(),
             influxdb_settings=InfluxdbIntegrationSettings.get_solo(),
         ))
     return context_data
Esempio n. 8
0
 def setUp(self):
     self.instance = EmailSettings().get_solo()