def save(self): """ Saves the habit. """ habit_form = self.form_list[0] reminder_form = self.form_list[1] habit = Habit.objects.create( user=self.user, description=habit_form.cleaned_data.get("description"), resolution=habit_form.cleaned_data.get("resolution"), target_value=habit_form.cleaned_data.get("target_value"), reminder=reminder_form.cleaned_data.get("trigger"), reminder_hour=reminder_form.cleaned_data.get("hour") or 0, reminder_days=reminder_form.cleaned_data.get("days") or 0, start=datetime.now(), ) habit_created.send(habit) if self.send_habit_email: render_to_email( text_template="emails/onboarding/habit_created.txt", html_template="emails/onboarding/habit_created.html", to=(self.user,), subject="You set up a new habit!", context={"habit": habit}, )
def create_user(self): """ Creates and logs in a new user for the habit """ summary_form = self.form_list[2] user = User.objects.create_user(email=summary_form.cleaned_data.get("email")) user.backend = "django.contrib.auth.backends.ModelBackend" login(self.request, user) user_created.send(user) current_site = get_current_site(self.request) site_name = current_site.name domain = current_site.domain c = { "email": user.email, "domain": domain, "site_name": site_name, "uid": int_to_base36(user.pk), "user": user, "token": token_generator.make_token(user), "protocol": self.request.is_secure() and "https" or "http", } render_to_email( text_template="emails/onboarding/welcome.txt", html_template="emails/onboarding/welcome.html", to=(user,), subject="Welcome!", opt_out=False, context=c, ) return user
def password_change(request, *args, **kwargs): response = django.contrib.auth.views.password_change(request, *args, **kwargs) if request.method == "POST" and response.status_code == 302: user_changed_password.send(sender=password_change) render_to_email( text_template="emails/accounts/password_changed.txt", subject="Your password has been changed", opt_out=False, ) return response
def password_change(request, *args, **kwargs): response = django.contrib.auth.views.password_change(request, *args, **kwargs) if request.method == 'POST' and response.status_code == 302: user_changed_password.send(sender=password_change) render_to_email( text_template='emails/accounts/password_changed.txt', html_template='emails/accounts/password_changed.html', to=(request.user,), subject='Your password has been changed', opt_out=False, ) return response
def send_data_collection_email(habit, today=None): if today is None: today = datetime.date.today() if not habit.send_data_collection_emails: return if today == habit.start: # Don't ask for data from before the habit was created return if habit.resolution == 'weekday': # Don't ask for data on Sundays and Mondays if today.weekday() in [6, 0]: return if habit.resolution == 'weekendday': # Don't ask for data from Tuesday to Saturday if today.weekday() in range(1, 6): return if habit.resolution == 'week': # Don't ask for data unless today is a Monday if today.weekday() != 0: return if habit.resolution == 'month': # Don't ask for data unless today is the 1st of a new month if today.day != 1: return time_period_name = { 'day': _('yesterday'), 'weekday': _('yesterday'), 'weekendday': _('yesterday'), 'week': _('last week'), 'month': _('last month'), }[habit.resolution] return render_to_email( text_template='emails/habits/data_collection.txt', html_template='emails/habits/data_collection.html', to=(habit.user,), subject='Let us know how you did', context={ 'time_period_name': time_period_name, 'habit': habit, 'unsubscribe_url': make_auto_login_link(habit.user, redirect=reverse('account_settings')), 'record_url': make_auto_login_link(habit.user, redirect=reverse('habit_record', args=[habit.pk])), }, )
def send_reminder_email(habit): return render_to_email( text_template='emails/habits/reminder.txt', html_template='emails/habits/reminder.html', to=(habit.user,), subject=habit.description, context={ 'habit': habit, 'unsubscribe_url': make_auto_login_link( habit.user, redirect=reverse( 'habit_edit', args=[habit.pk], ) ), }, )