Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would refactor your code to look something like this:</p> <p>View:</p> <pre><code>@login_required def view(request, extra_context=None, ...): form = EmailChangeForm(user=request.user, data=request.POST or None) if request.POST and form.is_valid(): send_email_change_request(request.user, form.cleaned_data['email'], https=request.is_secure()) return redirect(success_url) ... </code></pre> <p>Password validation goes to form:</p> <pre><code>class EmailChangeForm(Form): email = ... old_password = CharField(..., widget=Password()) def __init__(self, user, data=None): self.user = user super(EmailChangeForm, self).__init__(data=data) def clean_old_password(self): password = self.cleaned_data.get('password', None) if not self.user.check_password(password): raise ValidationError('Invalid password') </code></pre> <p>Extract logic from view:</p> <pre><code> def send_email_change_request(user, new_email, https=True): site = cache.get_model('sites', 'Site') email = new_email verification_key = generate_key(user, email) current_site = Site.objects.get_current() site_name = current_site.name domain = current_site.domain protocol = 'https' if https else 'http' # First clean all email change requests made by this user qs = EmailChangeRequest.objects.filter(user=request.user) qs.delete() # Create an email change request change_request = EmailChangeRequest( user = request.user, verification_key = verification_key, email = email ) change_request.save() # Prepare context c = { 'email': email, 'site_domain': 'dev.tolisto.de', 'site_name': 'tolisto', 'user': self.user, 'verification_key': verification_key, 'protocol': protocol, } c.update(extra_context) context = Context(c) # Send success email subject = "Subject" # I don't think that using template for # subject is good idea message = render_to_string(email_message_template_name, context_instance=context) send_mail(subject, message, None, [email]) </code></pre> <p>Don't put complicated things inside views (such as rendering and sending email).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload