How can I run a function when user registers Django

Asked

Viewed 83 times

2

I am developing a server using Django and wanted when a user registers to run a function that would create a directory with the user name. The folder with the new user name will be stored in Collections. My code is as follows:

Models py.

from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)  

class UserManager(BaseUserManager):
def create_user(self, username, first_name, last_name, email, password=None):
    """
    Creates and saves a user with the given variables and password.
    """
    if not email:
        raise ValueError('Users must have an email address')

    user = self.model(
        email=self.normalize_email(email),
        username=username,
        first_name=first_name,
        last_name=last_name,
    )

    user.set_password(password)
    user.save(using=self._db)
    return user

def create_superuser(self, username, first_name, last_name, email, password=None, is_admin=True):
    """
    Creates and saves a superuser with the given variables and password.
    """
    user = self.model(
        email=self.normalize_email(email),
        username=username,
        first_name=first_name,
        last_name=last_name,
        is_admin = is_admin,
    )

    user.set_password(password)
    user.save(using=self._db)
    return user


    class User(AbstractBaseUser):
    email = models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,
 )
username = models.CharField(max_length=255, unique=True)
first_name = models.CharField(max_length=100, unique=False)
last_name = models.CharField(max_length=100, unique=False)
is_active = models.BooleanField(default=True, unique=False)
is_admin = models.BooleanField(default=False, unique=False)
user_collection = models.CharField(max_length=500, default='NotCreated', unique=False)

objects = UserManager()

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['first_name', 'last_name', 'email']

def __str__(self):
    return self.username

def has_perm(self, perm, obj=None):
    return True

def has_module_perms(self, app_label):
    return True

@property
def is_staff(self):
    return self.is_admin

Image with Directories

inserir a descrição da imagem aqui

Is there any way to do this?

1 answer

1


Add this into the "create_user function":

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.mkdir(os.path.join(BASE_DIR, 'Collections/{}'.format(self.username)))
  • Thank you for your reply. I tried to replace it with a print() to see if it is being run but it is not working. Has any recommendation?

  • Strange, here it was right. You put the code inside the save method? When creating the user, the code should create the folder one level above the level of your app within the Collections folder. Tested by creating a user or super_user?

  • https://imgur.com/gPOIGzn <- code Console photo -> https://imgur.com/Nw3OY2d

  • I couldn’t do it after all. Yes I put inside create_user and create_superuser but I think these functions are not even being run because I put a print(self.username) and none of them is creating any output.

  • Have you referenced the class in Settings.py? If not, add the following: AUTH_USER_MODEL = 'mainAPP.Usermanager'

  • I’ve made it work

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.