1 year ago
#358558
M Umer Farooq
User Invitations based on invite links
my scenario is i have a teams model, team leads send out invite links to the team, url schema "domain/{team.id}/{team.name}/" i'm using dj_rest_auth for authentication, these invite links are sent to un-registered users. whne user clicks on url they are redirected to the signup view. now i need to add a user to the team(team id received in url) after they signup, i can't figure out how to pass the team id to user_sign_up post_save signal in allauth, django Sessionstore doesn't work as the key generated is different each time docs here, i'm new to django rest framework. i also looked at django globals, but that didn't help either. any help would be much much appreciated,
app.views:
from django.contrib.sessions.backends.db import SessionStore
def inviteduser(request, pk, name):
s = SessionStore()
s['team-id'] = pk
s.create()
print(s.session_key)
return redirect('http://localhost:8000/account/registration')
accounts.signals:
from allauth.account.signals import user_signed_up
from django.dispatch import receiver
from core.models import TeamMembers
from django.contrib.sessions.backends.db import SessionStore
@receiver(user_signed_up)
def user_signed_up(request, user, **kwargs):
s = SessionStore(session_key='gm0p279oeqvu385fy28btbyjzhak8a2j')
# if request.session['team-id']:
teamid = s['team-id']
# print(teamid)
teammember = TeamMembers(member_id=user.id, team_id=teamid)
teammember.save()
i have a custom user model and a corresponding custom serializer.
account.models:
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
"""User model."""
username = None
first_name = models.CharField(max_length=200, null=False, blank=False)
last_name = models.CharField(max_length=200, null=False, blank=False)
email = models.EmailField(_('email address'), unique=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
accounts.serializer:
from django.db import transaction
from rest_framework import serializers
from dj_rest_auth.registration.serializers import RegisterSerializer
from accounts.models import User
class CustomRegisterSerializer(RegisterSerializer):
email = serializers.EmailField(allow_blank=False, required=True)
first_name = serializers.CharField(max_length=200, allow_blank=False, required=True)
last_name = serializers.CharField(max_length=200, allow_blank=False, required=True)
# Define transaction.atomic to rollback the save operation in case of error
@transaction.atomic
def save(self, request):
user = super().save(request)
user.email = self.data.get('email')
user.first_name = self.data.get('first_name')
user.last_name = self.data.get('last_name')
user.save()
print(request.session.get('team-id'))
return user
django
django-rest-framework
django-allauth
dj-rest-auth
0 Answers
Your Answer