Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How to add a prefilled email to register site when the invitiation link is clicked? import hashlib import secrets from django.shortcuts import render from coombboom.settings
How to add a prefilled email to register site when the invitiation link is clicked? import hashlib import secrets from django.shortcuts import render from coombboom.settings import EMAIL_HOST_USER from . import forms from django.core.mail import send_mail from .models import Invitations # disable token by matching token in DB def disable_token(token): invitation = Invitations.objects.get(referred_token=token) invitation.referral_active = False invitation.save() # Enable token by matching email def enable_token(email): i = Invitations.objects.get(referred_email=email) # get row i.referral_active = True # set referral_active to True i.save() # commit to database # check token | check token is active def verify_token(token): token_exists = bool(Invitations.objects.filter( referred_token__icontains=token)) # check if token matches any in DB if token_exists: # check if token is active i = Invitations.objects.get(referred_token=token) return i.referral_active # true if token is active, false if not else: return False # token is inactive # check if invite exists or not def invite_exists(email): return bool(Invitations.objects.filter(referred_email__icontains=email)) # encode token from email def encode_token(email): salt = secrets.token_hex(8) + email referral_token = hashlib.sha256(salt.encode('utf-8')).hexdigest() return referral_token # calls encode_token() pushes to db and returns referral_token for use. def create_token(referer_id, recipient): referral_token = encode_token(recipient) Invitations.objects.create( referer_id=referer_id, referred_email=recipient, referred_token=referral_token) return referral_token def invitation(request): sub = forms.Invitation() if request.method == 'POST': sub = forms.Invitation(request.POST) # set referer_id and recipient/referral email referer_id = request.user.id recipient = str(sub['Email'].value()) # if email is found in database, set referral_active to True instead of creating a duplicate invite if invite_exists(recipient): enable_token(recipient) return render(request, 'invite-link-click-success.html', {'recepient': recipient}) # render success else: # if there is no duplicate, create new invite # save referer_id, referral_email and token to DB referral_token = create_token(referer_id, recipient) # start email message subject = 'Welcome' message = 'Here is your link to register: http://127.0.0.1:8000/account/register?token={}'.format( referral_token) send_mail(subject, message, EMAIL_HOST_USER, [recipient], fail_silently=False) # send email return render(request, 'invite-link-click-success.html', {'recepient': recipient}) # render success return render(request, 'invite-link-send.html', {'form': sub}) # if error?
Success Invitasion sent to: {{ recepient }}
{% load static %}Registrer Oppretting av konto
Forgotten password?Have you already created a user? Log in!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started