Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

password reset helpers

  • Loading branch information...
commit d733bd693236eb88a5b5a931266efd80bd62c4e3 1 parent d22d3b7
@jesperp jesperp authored
Showing with 27 additions and 0 deletions.
  1. +27 −0 profilebase/models.py
View
27 profilebase/models.py
@@ -3,12 +3,15 @@
import random
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
+from django.core.cache import cache
+from django.core.mail import send_mail
from django.db import models
from django.db.models import Q
from django.db.models.base import ModelBase
from django.db.models.fields import Field
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
+from django.template.loader import render_to_string
from django.utils.encoding import smart_str
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
@@ -142,6 +145,30 @@ def authenticate(cls, login, password):
return profile
@classmethod
+ def make_password_reset_key(cls, code):
+ return 'profilebase.reset.%s.%s' % (cls.__name__.lower(), code)
+
+ @classmethod
+ def get_profile_by_code(cls, code):
+ cache_key = cls.make_password_reset_key(code)
+ profile_id = cache.get(cache_key)
+ try:
+ return cls._default_manager.get(pk=profile_id)
+ except cls.DoesNotExist:
+ return None
+
+ def send_password_reset(self):
+ import uuid
+ code = uuid.uuid4().hex
+ cache_key = self.make_password_reset_key(code)
+ timeout = 60 * 60 * 24 * settings.PASSWORD_RESET_TIMEOUT_DAYS
+ cache.set(cache_key, self.pk, timeout)
+ ctx = { 'code': code, 'profile': self }
+ text = render_to_string('profilebase/password_reset_email.txt', ctx)
+ send_mail('Reset password', text, settings.DEFAULT_FROM_EMAIL,
+ [self.email], fail_silently=False)
+
+ @classmethod
def login_form(cls, **kwargs):
from .forms import LoginForm
return LoginForm(cls.authenticate, **kwargs)
Please sign in to comment.
Something went wrong with that request. Please try again.