def order_notify_task(order_id): """ Order client notification """ logger = logging.getLogger('billing') order_model = apps.get_model('finances', 'Order') try: order = order_model.objects.get(pk=order_id) if order.status == 'new': greetings_new = '' client = order.client expired = order.expired_date # delta = arrow.get(expired) - arrow.now() with select_locale(client): if client.is_trial: greetings_new = '<p>{}</p>'.format( _('We are glad to see you among our clients!')) mail_client(subject=_('New Invoice created'), template='emails/new_order.html', data={ 'order': order, 'client': client, 'greetings_new': greetings_new, 'expired': expired, 'days': 3 }, client=order.client) logger.info('New order created {}.'.format(order)) except order_model.DoesNotExist: logger.error('Order notify task failed {}.'.format(order_id))
def test_mail_client_by_client_subject_format(mailoutbox, settings): """ Should format the mail subject with a client name """ settings.EMAIL_SUBJECT_PREFIX = 'Prefix: ' client = Client.objects.get(login='******') subject = 'test subject' mailer.mail_client(subject=subject, template='emails/registration_fail.html', data={}, client=client) mail = mailoutbox[0] assert mail.subject == 'Prefix: User One, test subject' Client.objects.filter(login='******').update(name=None) client = Client.objects.get(login='******') mailer.mail_client(subject=subject, template='emails/registration_fail.html', data={}, client=client) mail = mailoutbox[1] assert mail.subject == 'Prefix: test subject'
def mail_client_task( subject, template, data, client_id=None, email=None, lang=None, ): """ Mail to site client or email """ if email: mail_client( subject=subject, template=template, data=data, email=email, lang=lang, ) return True if client_id: try: client = Client.objects.get(pk=client_id) mail_client(subject=subject, template=template, data=data, client=client) return True except Client.DoesNotExist: return False
def send( login, subject='Registration failure', template='registration_fail', ): client = Client.objects.get(login=login) mailer.mail_client(subject=subject, template='emails/{}.html'.format(template), data={}, client=client)
def client_greeting_email(days=settings.MB_CLIENT_GREETING_EMAIL_DAYS): """ The task for sending welcome emails to trial clients """ clients = Client.objects.get_for_greeting(days) for client in clients: with select_locale(client): mail_client(subject=_('How is your Sales?'), template='emails/client_welcome.html', data={ 'client': client, }, client=client)
def test_mail_client_by_email(mailoutbox): """ Should send an email by a client email """ mailer.mail_client(subject='Text message', template='emails/registration_fail.html', data={}, email='*****@*****.**') assert len(mailoutbox) == 1 mail = mailoutbox[0] assert mail.recipients() == ['*****@*****.**'] assert 'Text message' in mail.subject assert 'Registration failure' in mail.alternatives[0][0] assert '<p style="font-family:' in mail.alternatives[0][0]
def client_disabled_email(days=settings.MB_CLIENT_DISABLED_FIRST_EMAIL_DAYS): """ The task for sending emails to disabled clients """ clients = Client.objects.get_disabled(days) for client in clients: with select_locale(client): mail_client(subject=_('we miss You!'), template='emails/client_disabled.html', data={ 'order': client.orders.get_expired(('archived', )).first(), 'client': client, }, client=client)
def _clients(locale='ru', template='registration'): """ Send emails to the clients """ for manager in settings.MANAGERS: mail_client(subject='Test client message', template='emails/{}.html'.format(template), data={ 'login': '******', 'name': 'John Doe', 'url': '/user/login', 'website': 'website', 'password': '******' }, email=manager[1], lang=locale)
def mail_comments_action_task(): """ Send a mail about uncompleted actions """ comments = Comment.objects.get_uncompleted() for comment in comments: data = { 'subject': 'Action not completed', 'template': 'emails/comment_uncompleted.html', 'data': { 'comment': comment } } manager = comment.content_object.manager if manager: data['email'] = manager.email mail_client(**data) else: mail_managers(**data)
def orders_clients_disable(): """ Expired orders clients disable """ orders = apps.get_model('finances', 'Order').objects.get_expired() for order in orders: client = order.client client.status = 'disabled' client.disabled_at = arrow.utcnow().datetime client.save() logger = logging.getLogger('billing') logger.info('Client disabled {}.'.format(client)) mail_client(subject=_('Oh No! Your Account has been suspended'), template='emails/order_client_disabled.html', data={ 'url': order.client.url, 'name': client.name, 'order': order, 'created': order.created.strftime('%d.%m.%Y') }, client=order.client)
def orders_payment_notify(): """ Order payments notification """ orders = apps.get_model('finances', 'Order').objects.get_for_payment_notification() for order in orders: greetings_new = '' client = order.client with select_locale(client): if client.is_trial: greetings_new = '<p>{}</p>'.format( _('We are glad to see you among our clients!')) mail_client(subject=_( 'Invoice is due - don’t forget to extend your access! '), template='emails/order_payment_notification.html', data={ 'order': order, 'client': client, 'greetings_new': greetings_new }, client=order.client)