Here is what the email looks like:
# Hello ,
## Please click the link below to accept sponsorship of None:
### New User Comments:
### [Accept Sponsorship](http://127.0.0.1:8000/accounts/accept_sponsorship/MTM4/bvnkb3-1ae6d7e98bbd650d58a55fca2a4e3b87/)
These variables should be after Hello {alias_name_url }, where None is {new_alias_name}, and blank under user comments {user_comments}
views.py
from .forms import CustomUserCreationForm
from .models import CustomUser
from .tokens import account_activation_token
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import send_mail, EmailMessage
from django.http import HttpResponseBadRequest
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils.encoding import force_bytes, force_str
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.views.generic.edit import CreateView
import re, random, qrcode, requests
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy("login")
template_name = "signup.html"
model = CustomUser
def form_valid(self, form):
request_url = form.data['url']
alias_name_url = str(re.sub(r'/accounts/signup/', '', request_url)).lower()
an = CustomUser.objects.filter(alias_name=alias_name_url).values('alias_name').exists()
status = CustomUser.objects.filter(alias_name=alias_name_url).values('status').filter(status='fr').exists()
if an == True and status == True:
new_user =form.save()
user_comments = form.data['user_comments']
user_id = CustomUser.objects.filter(username=form.cleaned_data['username']).values('id')[0]['id']
user_email = CustomUser.objects.filter(username=form.cleaned_data['username']).values('email')[0]['email']
sponsor_id = CustomUser.objects.filter(alias_name=alias_name_url).values('id')[0]['id']
sponsor_email = CustomUser.objects.filter(id=sponsor_id).values('email')[0]['email']
sponsor_org_id = CustomUser.objects.filter(alias_name=alias_name_url).values('org_id')[0]['org_id']
user_upline = CustomUser.objects.filter(alias_name=alias_name_url).values('downline')[0]['downline']
user_downline = str(user_upline) + "," + str(user_id)
new_alias_name = str(form.cleaned_data['first_name'] + '_' + form.cleaned_data['last_name'][0] + str(
random.randrange(0000, 9999))).lower()
if CustomUser.objects.filter(alias_name=new_alias_name).values('alias_name').exists() == True:
while CustomUser.objects.filter(alias_name=new_alias_name).values('alias_name').exists() == True:
new_alias_name = str(
form.cleaned_data['first_name'] + '_' + form.cleaned_data['last_name'][0] + str(
random.randrange(0000, 9999))).lower()
if CustomUser.objects.filter(alias_name=new_alias_name).values('alias_name').exists() == False:
new_alias_name = new_alias_name
break
else:
new_alias_name = new_alias_name
alias_email = new_alias_name + 'xxxxxxxxxxxx'
CustomUser.objects.filter(username=form.cleaned_data['username']).update(is_active=False)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(sponsor=sponsor_id)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(org_id=sponsor_org_id)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(upline=user_upline)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(downline=user_downline)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(alias_name=new_alias_name)
CustomUser.objects.filter(username=form.cleaned_data['username']).update(alias_email=alias_email)
current_site = get_current_site(self.request)
subject = 'Activate your account'
message = render_to_string('account_activation_email.html', {
'user': new_user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
'token': account_activation_token.make_token(new_user),
})
send_mail(subject, message, 'xxxxxxxxxxxxxxxxxxxx', [user_email], html_message=message)
current_site = get_current_site(self.request)
subject = 'Accept new user'
message = render_to_string('accept_sponsorship_email.html', {
'user': new_user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
'token': account_activation_token.make_token(new_user),
'user_comments': user_comments,
})
send_mail(subject, message, 'xxxxxxxxxx', [sponsor_email], html_message=message,)
return redirect('account_activation_sent')
elif an == True and status == False:
return render(self.request, 'is_fish.html')
else:
return render(self.request, 'bad_alias.html')
def account_activation_sent(request):
return render(request, 'account_activation_sent.html')
def activate(request, uidb64, token):
try:
uid = force_str(urlsafe_base64_decode(uidb64))
user = CustomUser.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, CustomUser.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return redirect('account_activation_complete')
else:
return HttpResponseBadRequest('Activation link is invalid!')
def account_activation_complete(request):
return render(request, 'account_activation_complete.html')
##################################################################################################
def accept_sponsorship(request, uidb64, token):
try:
uid = force_str(urlsafe_base64_decode(uidb64))
user = CustomUser.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, CustomUser.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.sponsor_accept = True
user.save()
return redirect('accept_sponsorship_complete')
else:
return HttpResponseBadRequest('Accept Sponsorship link is invalid!')
def accept_sponsorship_complete(request):
return render(request, 'accept_sponsorship_complete.html')