From d0148cf6dc1489c96db15708d22f09c9f19a1f3b Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 24 Apr 2012 22:20:10 -0400 Subject: [PATCH] [finish #28524505] add change email form --- frontend/forms.py | 7 +++ .../templates/registration/email_changed.txt | 6 +++ .../registration/user_change_form.html | 16 +++++-- frontend/views.py | 47 ++++++++++++------- 4 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 frontend/templates/registration/email_changed.txt diff --git a/frontend/forms.py b/frontend/forms.py index d2b4650b..5d848330 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -97,6 +97,13 @@ class ProfileForm(forms.ModelForm): 'tagline': forms.Textarea(attrs={'rows': 5, 'onKeyUp': "counter(this, 140)", 'onBlur': "counter(this, 140)"}), } +class UserEmail(forms.Form): + email = forms.EmailField( + label=_("new email address"), + max_length=100, + error_messages={'required': 'Please enter an email address.'}, + ) + class UserData(forms.Form): username = forms.RegexField( label=_("New Username"), diff --git a/frontend/templates/registration/email_changed.txt b/frontend/templates/registration/email_changed.txt new file mode 100644 index 00000000..8d82d1f4 --- /dev/null +++ b/frontend/templates/registration/email_changed.txt @@ -0,0 +1,6 @@ +The email address for your unglue.it accound has been changed from {{oldemail}} to {{request.user.email}}. + +If there's any reason that this is in error, please contact us at support@gluejar.com + +Sincerely, +The unglue.it Team diff --git a/frontend/templates/registration/user_change_form.html b/frontend/templates/registration/user_change_form.html index 5cc88412..7a068b70 100644 --- a/frontend/templates/registration/user_change_form.html +++ b/frontend/templates/registration/user_change_form.html @@ -1,16 +1,26 @@ {% extends "registration/registration_base.html" %} {% block title %}Change User Data{% endblock %} {% block doccontent %} -

Changing Your Username

+

Changing Your Username

If you change your username, the web address for your profile page will change as well.

- +

Your current username: {{ user.username }}

{% csrf_token %} {{ form.as_p }} - +
+
+
+

Changing Your Email

+

Your current email: {{ user.email }}

+
+ {% csrf_token %} + {{ emailform.as_p }} + +
+

Want to change your password instead?

{% endblock %} diff --git a/frontend/views.py b/frontend/views.py index 7a05d926..ea1bc81a 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -12,7 +12,6 @@ from re import sub from itertools import islice from decimal import Decimal as D from xml.etree import ElementTree as ET - import requests import oauth2 as oauth from django import forms @@ -20,7 +19,6 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.core.exceptions import ObjectDoesNotExist -from django.core.mail import send_mail from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.comments import Comment @@ -29,6 +27,7 @@ from django.forms import Select from django.forms.models import modelformset_factory from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponse, HttpResponseNotFound +from django.template.loader import render_to_string from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST from django.views.generic.edit import FormView @@ -38,11 +37,12 @@ from django.shortcuts import render, render_to_response, get_object_or_404 from django.utils.http import urlencode from django.utils.translation import ugettext_lazy as _ from regluit.core import tasks +from regluit.core.tasks import send_mail_task from regluit.core import models, bookloader, librarything from regluit.core import userlists from regluit.core.search import gluejar_search from regluit.core.goodreads import GoodreadsClient -from regluit.frontend.forms import UserData, ProfileForm, CampaignPledgeForm, GoodreadsShelfLoadingForm +from regluit.frontend.forms import UserData, UserEmail, ProfileForm, CampaignPledgeForm, GoodreadsShelfLoadingForm from regluit.frontend.forms import RightsHolderForm, UserClaimForm, LibraryThingForm, OpenCampaignForm from regluit.frontend.forms import ManageCampaignForm, DonateForm, CampaignAdminForm, EmailShareForm, FeedbackForm from regluit.frontend.forms import EbookForm, CustomPremiumForm, EditManagersForm @@ -1089,20 +1089,35 @@ def supporter(request, supporter_username, template_name): return render(request, template_name, context) def edit_user(request): - form=UserData() if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('auth_login')) + return HttpResponseRedirect(reverse('auth_login')) + form=UserData() + emailform = UserEmail({'email':request.user.email}) oldusername=request.user.username + oldemail= request.user.email if request.method == 'POST': - # surely there's a better way to add data to the POST data? - postcopy=request.POST.copy() - postcopy['oldusername']=oldusername - form = UserData(postcopy) - if form.is_valid(): # All validation rules pass, go and change the username - request.user.username=form.cleaned_data['username'] - request.user.save() - return HttpResponseRedirect(reverse('home')) # Redirect after POST - return render(request,'registration/user_change_form.html', {'form': form},) + if 'change_username' in request.POST.keys(): + # surely there's a better way to add data to the POST data? + postcopy=request.POST.copy() + postcopy['oldusername']=oldusername + form = UserData(postcopy) + if form.is_valid(): # All validation rules pass, go and change the username + request.user.username=form.cleaned_data['username'] + request.user.save() + return HttpResponseRedirect(reverse('home')) # Redirect after POST + elif 'change_email' in request.POST.keys(): + emailform = UserEmail(request.POST) + if emailform.is_valid(): + request.user.email=emailform.cleaned_data['email'] + request.user.save() + send_mail_task.delay( + 'unglue.it email changed', + render_to_string('registration/email_changed.txt',{'oldemail':oldemail,'request':request}), + None, + [request.user.email,oldemail] + ) + return HttpResponseRedirect(reverse('home')) # Redirect after POST + return render(request,'registration/user_change_form.html', {'form': form,'emailform': emailform}) def search(request): @@ -1601,7 +1616,7 @@ def emailshare(request): message = form.cleaned_data['message'] sender = form.cleaned_data['sender'] recipient = form.cleaned_data['recipient'] - send_mail(subject, message, sender, [recipient]) + send_mail_task.delay(subject, message, sender, [recipient]) try: next = form.cleaned_data['next'] except: @@ -1666,7 +1681,7 @@ def feedback(request): else: ungluer = request.user.username message = "<<>>\n"+message - send_mail(subject, message, sender, [recipient]) + send_mail_task.delay(subject, message, sender, [recipient]) return render(request, "thanks.html", {"page":page})