def view_settings(request): old_email = request.user.email # this line has to be at the top to work. old_wants_newsletter = request.user.profile.wants_newsletter user = request.user up = user.profile user_form = UserForm(request.POST or None, instance=user) profile_form = ProfileForm(request.POST or None, instance=up) if profile_form.is_valid() and user_form.is_valid(): user_cd = user_form.cleaned_data profile_cd = profile_form.cleaned_data new_email = user_cd['email'] changed_email = old_email != new_email if changed_email: # Email was changed. up.activation_key = sha1_activation_key(user.username) up.key_expires = now() + timedelta(5) up.email_confirmed = False # Unsubscribe the old address in mailchimp (we'll # resubscribe it when they confirm it later). update_mailchimp.delay(old_email, 'unsubscribed') # Send the email. email = emails['email_changed_successfully'] send_mail( email['subject'], email['body'] % (user.username, up.activation_key), email['from'], [new_email], ) msg = message_dict['email_changed_successfully'] messages.add_message(request, msg['level'], msg['message']) logout(request) else: # if the email wasn't changed, simply inform of success. msg = message_dict['settings_changed_successfully'] messages.add_message(request, msg['level'], msg['message']) new_wants_newsletter = profile_cd['wants_newsletter'] if old_wants_newsletter != new_wants_newsletter: if new_wants_newsletter is True and not changed_email: # They just subscribed. If they didn't *also* update their # email address, subscribe them. subscribe_to_mailchimp.delay(new_email) elif new_wants_newsletter is False: # They just unsubscribed update_mailchimp.delay(new_email, 'unsubscribed') # New email address and changes above are saved here. profile_form.save() user_form.save() return HttpResponseRedirect(reverse('view_settings')) return render(request, 'profile/settings.html', { 'profile_form': profile_form, 'user_form': user_form, 'private': True })
def confirm_email(request, activation_key): """Confirms email addresses for a user and sends an email to the admins. Checks if a hash in a confirmation link is valid, and if so sets the user's email address as valid. If they are subscribed to the newsletter, ensures that mailchimp is updated. """ ups = UserProfile.objects.filter(activation_key=activation_key) if not len(ups): return render( request, "register/confirm.html", { "invalid": True, "private": True }, ) confirmed_accounts_count = 0 expired_key_count = 0 for up in ups: if up.email_confirmed: confirmed_accounts_count += 1 if up.key_expires < now(): expired_key_count += 1 if confirmed_accounts_count == len(ups): # All the accounts were already confirmed. return render( request, "register/confirm.html", { "already_confirmed": True, "private": True }, ) if expired_key_count > 0: return render( request, "register/confirm.html", { "expired": True, "private": True }, ) # Tests pass; Save the profile for up in ups: if up.wants_newsletter: subscribe_to_mailchimp.delay(up.user.email) up.email_confirmed = True up.save() return render(request, "register/confirm.html", { "success": True, "private": True })
def confirm_email(request, activation_key): """Confirms email addresses for a user and sends an email to the admins. Checks if a hash in a confirmation link is valid, and if so sets the user's email address as valid. If they are subscribed to the newsletter, ensures that mailchimp is updated. """ ups = UserProfile.objects.filter(activation_key=activation_key) if not len(ups): return render(request, 'register/confirm.html', { 'invalid': True, 'private': True }) confirmed_accounts_count = 0 expired_key_count = 0 for up in ups: if up.email_confirmed: confirmed_accounts_count += 1 if up.key_expires < now(): expired_key_count += 1 if confirmed_accounts_count == len(ups): # All the accounts were already confirmed. return render(request, 'register/confirm.html', { 'already_confirmed': True, 'private': True }) if expired_key_count > 0: return render(request, 'register/confirm.html', { 'expired': True, 'private': True }) # Tests pass; Save the profile for up in ups: if up.wants_newsletter: subscribe_to_mailchimp.delay(up.user.email) up.email_confirmed = True up.save() return render(request, 'register/confirm.html', { 'success': True, 'private': True })
def view_settings(request): old_email = request.user.email # this line has to be at the top to work. old_wants_newsletter = request.user.profile.wants_newsletter user = request.user up = user.profile user_form = UserForm(request.POST or None, instance=user) profile_form = ProfileForm(request.POST or None, instance=up) if profile_form.is_valid() and user_form.is_valid(): user_cd = user_form.cleaned_data profile_cd = profile_form.cleaned_data new_email = user_cd["email"] changed_email = old_email != new_email if changed_email: # Email was changed. up.activation_key = sha1_activation_key(user.username) up.key_expires = now() + timedelta(5) up.email_confirmed = False # Unsubscribe the old address in mailchimp (we'll # resubscribe it when they confirm it later). update_mailchimp.delay(old_email, "unsubscribed") # Send an email to the new and old addresses. New for verification; # old for notification of the change. email = emails["email_changed_successfully"] send_mail( email["subject"], email["body"] % (user.username, up.activation_key), email["from"], [new_email], ) email = emails["notify_old_address"] send_mail( email["subject"], email["body"] % (user.username, old_email, new_email), email["from"], [old_email], ) msg = message_dict["email_changed_successfully"] messages.add_message(request, msg["level"], msg["message"]) logout(request) else: # if the email wasn't changed, simply inform of success. msg = message_dict["settings_changed_successfully"] messages.add_message(request, msg["level"], msg["message"]) new_wants_newsletter = profile_cd["wants_newsletter"] if old_wants_newsletter != new_wants_newsletter: if new_wants_newsletter is True and not changed_email: # They just subscribed. If they didn't *also* update their # email address, subscribe them. subscribe_to_mailchimp.delay(new_email) elif new_wants_newsletter is False: # They just unsubscribed update_mailchimp.delay(new_email, "unsubscribed") # New email address and changes above are saved here. profile_form.save() user_form.save() return HttpResponseRedirect(reverse("view_settings")) return render( request, "profile/settings.html", { "profile_form": profile_form, "user_form": user_form, "private": True, }, )
def view_settings(request): old_email = request.user.email # this line has to be at the top to work. old_wants_newsletter = request.user.profile.wants_newsletter user = request.user up = user.profile user_form = UserForm(request.POST or None, instance=user) profile_form = ProfileForm(request.POST or None, instance=up) if profile_form.is_valid() and user_form.is_valid(): user_cd = user_form.cleaned_data profile_cd = profile_form.cleaned_data new_email = user_cd['email'] changed_email = old_email != new_email if changed_email: # Email was changed. # Build the activation key for the new account salt = hashlib.sha1(str(random.random())).hexdigest()[:5] up.activation_key = hashlib.sha1(salt + user.username).hexdigest() up.key_expires = now() + timedelta(5) up.email_confirmed = False # Unsubscribe the old address in mailchimp (we'll # resubscribe it when they confirm it later). update_mailchimp.delay(old_email, 'unsubscribed') # Send the email. email = emails['email_changed_successfully'] send_mail( email['subject'], email['body'] % (user.username, up.activation_key), email['from'], [new_email], ) msg = message_dict['email_changed_successfully'] messages.add_message(request, msg['level'], msg['message']) logout(request) else: # if the email wasn't changed, simply inform of success. msg = message_dict['settings_changed_successfully'] messages.add_message(request, msg['level'], msg['message']) new_wants_newsletter = profile_cd['wants_newsletter'] if old_wants_newsletter != new_wants_newsletter: if new_wants_newsletter is True and not changed_email: # They just subscribed. If they didn't *also* update their # email address, subscribe them. subscribe_to_mailchimp.delay(new_email) elif new_wants_newsletter is False: # They just unsubscribed update_mailchimp.delay(new_email, 'unsubscribed') # New email address and changes above are saved here. profile_form.save() user_form.save() return HttpResponseRedirect(reverse('view_settings')) return render(request, 'profile/settings.html', { 'profile_form': profile_form, 'user_form': user_form, 'private': True })