From e8ee630b7bd65672435923630547d172a146395f Mon Sep 17 00:00:00 2001 From: Maxime Gaillard Date: Wed, 8 Jan 2014 22:59:33 +0100 Subject: [PATCH 1/2] Send an email to unactive user after one day Signed-off-by: Maxime Gaillard --- www/account/models/user.py | 11 ++++++ www/account/tasks.py | 34 ++++++++++++++++++- .../account/emails/inactive_email_message.txt | 16 +++++++++ .../account/emails/inactive_email_subject.txt | 2 ++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 www/account/templates/account/emails/inactive_email_message.txt create mode 100644 www/account/templates/account/emails/inactive_email_subject.txt diff --git a/www/account/models/user.py b/www/account/models/user.py index fbadee7..bf8e323 100644 --- a/www/account/models/user.py +++ b/www/account/models/user.py @@ -3,9 +3,11 @@ from django.conf import settings from django.db import models +from django.db.models.signals import post_save from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.utils.translation import ugettext_lazy as _ from django.utils import timezone, http +from django.utils.translation import get_language from django.template.loader import render_to_string from django.core.mail import send_mail from django.core.mail import EmailMultiAlternatives @@ -170,3 +172,12 @@ def has_identity_set(self): if self.first_name and self.last_name: return True return False + + +def add_user_language(sender, instance, created, **kwargs): + if created: + instance.browser_language = get_language() + instance.save(update_fields=['browser_language']) + +# register the signal +post_save.connect(add_user_language, sender=User) diff --git a/www/account/tasks.py b/www/account/tasks.py index fc9f937..3dee516 100644 --- a/www/account/tasks.py +++ b/www/account/tasks.py @@ -1,6 +1,16 @@ # -*- coding:Utf-8 -*- -from celery.task import task +import datetime + +from django.utils.timezone import now +from django.template.loader import render_to_string +from django.template import Context +from django.conf import settings +from django.core.mail import EmailMessage, get_connection + +from celery.task import task, periodic_task +from celery.schedules import crontab + from vosae_utils import respects_language @@ -8,3 +18,25 @@ @respects_language def user_send_activation_email(user): user.send_activation_email() + +@periodic_task(run_every=crontab(minute=0, hour='*/1')) +@respects_language +def user_send_inactive_email(): + from account.models import User + + for user in User.objects.filter(is_active=False, date_joined__gte=now() - datetime.timedelta(days=1, hours=1), date_joined__lte=now() - datetime.timedelta(days=1)): + + context = { + 'user': user, + 'site': {'name': settings.SITE_NAME, 'url': settings.SITE_URL} + } + # Send mails + connection = get_connection() + plaintext_context = Context(autoescape=False) # HTML escaping not appropriate in plaintext + subject = render_to_string('account/emails/inactive_email_subject.txt', context, plaintext_context) + subject = ''.join(subject.splitlines()) + text_body = render_to_string('account/emails/inactive_email_message.txt', context, plaintext_context) + + message = EmailMessage(subject=subject, from_email="maxime@vosae.com", to=[user.email], body=text_body, connection=connection) + message.preserve_recipients = False # Useful for Mandrill + message.send() diff --git a/www/account/templates/account/emails/inactive_email_message.txt b/www/account/templates/account/emails/inactive_email_message.txt new file mode 100644 index 0000000..8161d05 --- /dev/null +++ b/www/account/templates/account/emails/inactive_email_message.txt @@ -0,0 +1,16 @@ +{% load i18n %}{% autoescape off %} +{% if user.first_name %} +{% blocktrans with first_name=user.first_name %}Hi {{ first_name }},{% endblocktrans %} +{% else %} +{% trans "Hi," %} +{% endif %} +{% blocktrans with site=site.name %}I notice you've set up a {{ site }} account, but have yet activated your account. Please let me know if you were running into any issues, or have any questions, I would love to help! + +Regards, +-- +Maxime Gaillard +Co-founder, CEO +Vosae +{% endblocktrans %} +{% endautoescape %} + diff --git a/www/account/templates/account/emails/inactive_email_subject.txt b/www/account/templates/account/emails/inactive_email_subject.txt new file mode 100644 index 0000000..a0dbf70 --- /dev/null +++ b/www/account/templates/account/emails/inactive_email_subject.txt @@ -0,0 +1,2 @@ +{% load i18n %} +{% blocktrans with site=site.name %}Your {{ site }} account{% endblocktrans %} From 4fa7cc75deff7efda8d7fa751ae187fc349a27ed Mon Sep 17 00:00:00 2001 From: Maxime Gaillard Date: Thu, 9 Jan 2014 10:50:02 +0100 Subject: [PATCH 2/2] Send an email to new user after one week Signed-off-by: Maxime Gaillard --- www/account/tasks.py | 22 +++++++++++++++++++ .../emails/after_one_week_email_message.txt | 21 ++++++++++++++++++ .../emails/after_one_week_email_subject.txt | 2 ++ 3 files changed, 45 insertions(+) create mode 100644 www/account/templates/account/emails/after_one_week_email_message.txt create mode 100644 www/account/templates/account/emails/after_one_week_email_subject.txt diff --git a/www/account/tasks.py b/www/account/tasks.py index 3dee516..0248e9b 100644 --- a/www/account/tasks.py +++ b/www/account/tasks.py @@ -40,3 +40,25 @@ def user_send_inactive_email(): message = EmailMessage(subject=subject, from_email="maxime@vosae.com", to=[user.email], body=text_body, connection=connection) message.preserve_recipients = False # Useful for Mandrill message.send() + +@periodic_task(run_every=crontab(minute=0, hour='*/1')) +@respects_language +def user_send_after_one_week_email(): + from account.models import User + + for user in User.objects.filter(is_active=True, date_joined__gte=now() - datetime.timedelta(days=7, hours=1), date_joined__lte=now() - datetime.timedelta(days=7)): + + context = { + 'user': user, + 'site': {'name': settings.SITE_NAME, 'url': settings.SITE_URL} + } + # Send mails + connection = get_connection() + plaintext_context = Context(autoescape=False) # HTML escaping not appropriate in plaintext + subject = render_to_string('account/emails/after_one_week_email_subject.txt', context, plaintext_context) + subject = ''.join(subject.splitlines()) + text_body = render_to_string('account/emails/after_one_week_email_message.txt', context, plaintext_context) + + message = EmailMessage(subject=subject, from_email="maxime@vosae.com", to=[user.email], body=text_body, connection=connection) + message.preserve_recipients = False # Useful for Mandrill + message.send() diff --git a/www/account/templates/account/emails/after_one_week_email_message.txt b/www/account/templates/account/emails/after_one_week_email_message.txt new file mode 100644 index 0000000..8c36cad --- /dev/null +++ b/www/account/templates/account/emails/after_one_week_email_message.txt @@ -0,0 +1,21 @@ +{% load i18n %}{% autoescape off %} +{% blocktrans with site=site.name first_name=user.first_name %}Hi {{ first_name }}, + +You signed up with {{ site }} a little over a week ago. And, I was wondering how's it going? If you want to share with me your first feelings, if you are running into any issues, or have any questions, I would love to help! +If you are looking for a little guidance on a conversation, you can answer the following questions: + +- Every company is different... what kind of business are you running? +- How many colleagues do you have? +- What have you enjoyed about {{ site }}? +- What is not perfect? +- What questions can I answer for you about {{ site }}? +- Just respond, and we can get the dialog going. We have worked with many customers, and would like to know how we can help you. + + +Regards, +-- +Maxime Gaillard +Co-founder, CEO +Vosae +{% endblocktrans %} +{% endautoescape %} diff --git a/www/account/templates/account/emails/after_one_week_email_subject.txt b/www/account/templates/account/emails/after_one_week_email_subject.txt new file mode 100644 index 0000000..a0dbf70 --- /dev/null +++ b/www/account/templates/account/emails/after_one_week_email_subject.txt @@ -0,0 +1,2 @@ +{% load i18n %} +{% blocktrans with site=site.name %}Your {{ site }} account{% endblocktrans %}