def forgotpassword(request): """ Django View. Gets called when user wants to change password he managed to forget. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User if request.POST.get("ajax", "") == "1": ret = {"result": 0} usr = None if request.POST.get("method", "") == "forgot_password": def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: usersToEmail = list( User.objects.filter( Q(username=request.POST.get("username", "")) | Q(email=request.POST.get("username", "")))) for usr in usersToEmail: from booki.account import models as account_models def generateSecretCode(): import string from random import choice return ''.join([ choice(string.letters + string.digits) for i in range(30) ]) secretcode = generateSecretCode() account_models = account_models.UserPassword() account_models.user = usr account_models.remote_useragent = request.META.get( 'HTTP_USER_AGENT', '') account_models.remote_addr = request.META.get( 'REMOTE_ADDR', '') account_models.remote_host = request.META.get( 'REMOTE_HOST', '') account_models.secretcode = secretcode # In case of an error we really should not send email to user and do rest of the procedure try: account_models.save() except: transaction.rollback() else: transaction.commit() THIS_BOOKI_SERVER = config.getConfiguration( 'THIS_BOOKI_SERVER') body = render_to_string( 'account/password_reset_email.html', dict(secretcode=secretcode, hostname=THIS_BOOKI_SERVER)) from django.core.mail import EmailMessage msg = EmailMessage(_('Reset password'), body, settings.REPORT_EMAIL_USER, [usr.email]) msg.content_subtype = 'html' try: msg.send() except: ret["result"] = 4 if len(usersToEmail) == 0: ret["result"] = 3 # Do we need rollback for this?! try: resp = HttpResponse(simplejson.dumps(ret), mimetype="text/json") except: transaction.rollback() raise else: transaction.commit() return resp # Do we need commit for this?! try: resp = render_to_response('account/forgot_password.html', {"request": request}) except: transaction.rollback() raise else: transaction.commit() return resp
def forgotpassword(request): """ Django View. Gets called when user wants to change password he managed to forget. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User if request.POST.get("ajax", "") == "1": ret = {"result": 0} usr = None if request.POST.get("method", "") == "forgot_password": def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: allOK = True try: usr = User.objects.get(username=request.POST.get("username", "")) except User.DoesNotExist: pass if not usr: try: usr = User.objects.get(email=request.POST.get("username", "")) except User.DoesNotExist: allOK = False if allOK: from booki.account import models as account_models from django.core.mail import send_mail def generateSecretCode(): import string from random import choice return ''.join([choice(string.letters + string.digits) for i in range(30)]) secretcode = generateSecretCode() account_models = account_models.UserPassword() account_models.user = usr account_models.remote_useragent = request.META.get('HTTP_USER_AGENT','') account_models.remote_addr = request.META.get('REMOTE_ADDR','') account_models.remote_host = request.META.get('REMOTE_HOST','') account_models.secretcode = secretcode try: account_models.save() except: transaction.rollback() else: transaction.commit() # body = render_to_string('account/password_reset_email.txt', dict(secretcode=secretcode)) send_mail(_('Reset password'), body, 'info@' + THIS_BOOKI_SERVER, [usr.email], fail_silently=False) else: ret["result"] = 3 return HttpResponse(simplejson.dumps(ret), mimetype="text/json") try: return render_to_response('account/forgot_password.html', {"request": request}) except: transaction.rollback() finally: transaction.commit()
def forgotpassword(request): """ Django View. Gets called when user wants to change password he managed to forget. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User if request.POST.get("ajax", "") == "1": ret = {"result": 0} usr = None if request.POST.get("method", "") == "forgot_password": def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: usersToEmail = list(User.objects.filter(Q(username=request.POST.get("username", "")) | Q(email=request.POST.get("username", "")))) for usr in usersToEmail: from booki.account import models as account_models def generateSecretCode(): import string from random import choice return ''.join([choice(string.letters + string.digits) for i in range(30)]) secretcode = generateSecretCode() account_models = account_models.UserPassword() account_models.user = usr account_models.remote_useragent = request.META.get('HTTP_USER_AGENT','') account_models.remote_addr = request.META.get('REMOTE_ADDR','') account_models.remote_host = request.META.get('REMOTE_HOST','') account_models.secretcode = secretcode # In case of an error we really should not send email to user and do rest of the procedure try: account_models.save() except: transaction.rollback() else: transaction.commit() THIS_BOOKI_SERVER = config.getConfiguration('THIS_BOOKI_SERVER') body = render_to_string('account/password_reset_email.html', dict(secretcode=secretcode, hostname=THIS_BOOKI_SERVER)) from django.core.mail import EmailMessage msg = EmailMessage(_('Reset password'), body, settings.REPORT_EMAIL_USER, [usr.email]) msg.content_subtype = 'html' try: msg.send() except: ret["result"] = 4 if len(usersToEmail) == 0: ret["result"] = 3 # Do we need rollback for this?! try: resp = HttpResponse(simplejson.dumps(ret), mimetype="text/json") except: transaction.rollback() raise else: transaction.commit() return resp # Do we need commit for this?! try: resp = render_to_response('account/forgot_password.html', {"request": request}) except: transaction.rollback() raise else: transaction.commit() return resp