Eu estou a desenvolver um servidor utilizando django e queria que quando um utilizador se registasse rodasse uma função que iria criar uma directory com o nome do utilizador. A pasta com o nome do utilizador novo sera guardada em Collections. O meu código é o seguinte:
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
Imagem com directories
Existe alguma forma de fazer isto?