def send_mail( user, code, site=None, mail_text_template_file='gdjet/registration/mail.txt', mail_html_template_file='gdjet/registration/mail.html', subject='Your Activation Code', additional_context={} ): if user.email: try: from_email, to = settings.REGISTER_FROM_EMAIL, user.email c = Context( { 'user': user, 'code': code } ) c.update(additional_context) text_content = loader.get_template( mail_text_template_file ).render(c) html_content = None if mail_html_template_file: html_content = loader.get_template( mail_html_template_file ).render(c) msg = Email( title = subject, from_email = from_email, text = text_content, html = html_content ) msg.save() msg.add_to(to, user.get_full_name()) msg.send_or_queue() return True except Exception, e: from gdjet.utils.log import log log("Error occured in email %s %s" % (e, e.message), 'gdjet.registration.email')
def send_mail_django( user, code, site=None, mail_text_template_file='gdjet/registration/mail.txt', mail_html_template_file='gdjet/registration/mail.html', subject='Your Activation Code', additional_context={} ): if user.email: try: from_email, to = settings.REGISTER_FROM_EMAIL, user.email c = Context( { 'user': user, 'code': code } ) c.update(additional_context) text_content = loader.get_template( mail_text_template_file ).render(c) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) if mail_html_template_file: html_content = loader.get_template( mail_html_template_file ).render(c) msg.attach_alternative(html_content, "text/html") msg.send() return True except Exception, e: from gdjet.utils.log import log log("Error occured in email %s %s" % (e, e.message), 'gdjet.registration.email_django')
def validation( request, template_name = "gdjet/registration/validation_start.html", template_completed_name = "gdjet/registration/validation_success.html", template_fail_name = "gdjet/registration/validation.html", template_closed_name = 'gdjet/registration/closed.html', post_validation_varname = 'reg', # historic. http_response = True, backend = None, success_url=None, extra_context=None, **kwargs ): backend = get_backend(backend) CODE=CODE_SAME # return code for non-http response. validation_code = None validation_user = None error = False error_already_active = False # gdjet does not use the direct activate approach natively # lookup if there is activation_key inside kwargs if ('activation_key' in kwargs.keys() and settings.REGISTER_OPTIONS['direct_validation']): # this might be due to django-registration like usage. validation_code=kwargs['activation_key'] else: if request.method=='GET' and 'a' in request.GET.keys(): form = backend.get_validation_form_class(request)(request.GET) if form.is_valid(): validation_code=form.cleaned_data['a'] validation_user=User.objects.get( username=form.cleaned_data['u']) else: error = True elif request.method=='POST': form = backend.get_validation_form_class(request)(request.POST, files=request.FILES) if form.is_valid(): validation_code=form.cleaned_data['a'] validation_user=User.objects.get( username=form.cleaned_data['u']) else: error = True # activate if there is a validation form. if validation_code: # first get the registration object # we can also see if it is already validated here. reg=None if not settings.REGISTER_OPTIONS['direct_validation']: try: reg = backend.get_registration_object(validation_user, validation_code) if reg.validated: # actually, keys get deleted, so... error_already_active=True except ObjectDoesNotExist: from gdjet.utils.log import log error = True # simple: this code is invalid after all. log('User tried to validate: %s, '\ 'wrong validation code: %s' % (validation_user, validation_code), 'gdjet.views.activate') except Exception, e: from gdjet.utils.log import log log('Validation got an Exception %s %s' % (e, e.message), 'gdjet.views.activate') error = True if not error_already_active or error: # now get the account activated. account = backend.activate(request, activation_key=validation_code,) if account: # success. if not http_response: return CODE_SUCCESS, RequestContext(request, {'account': account, post_validation_varname: reg }) if success_url is None: to, args, kwargs = backend.post_activation_redirect( request, account) return redirect(to, *args, **kwargs) else: return redirect(success_url) else: error = True