S
S
Sergey Kutylev2015-11-13 20:29:11
Django
Sergey Kutylev, 2015-11-13 20:29:11

How to populate a custom model in django when a new user is authenticated?

Good afternoon!
There is a model that implements the user profile. It is already filled with the data of different users, something like a phone directory. This model is related to User using one-to-one. Those. each user can have an entry in the directory. I bring part of the model.

class Person(models.Model):
    user = models.OneToOneField(User, verbose_name='Пользователь', related_name='profile', blank=True, null=True, unique=True)
    last_name = models.CharField(max_length=30, verbose_name='Фамилия')
    first_name = models.CharField(max_length=30, verbose_name='Имя')
    middle_name = models.CharField(max_length=30, verbose_name='Отчество')
    birthday = models.DateField(verbose_name='Дата рождения', blank=True, default='00.00.0000')
    email = models.EmailField(verbose_name='Email', blank=True)
    photo = models.ImageField(upload_to=get_person_image_path, verbose_name='Фотография', blank=True, default=None)

There was a need to authorize new users using google apps for business, for this I decided to use django-allauth and a slightly changed adapter to check the domain name in the mail.
class MySocialAccountAdapter(DefaultSocialAccountAdapter):
    def pre_social_login(self, request, sociallogin):
        email_domain = sociallogin.user.email.split('@')[1].lower()
        if not email_domain == 'domain.tld':
            raise ImmediateHttpResponse(HttpResponseRedirect('/forbidden/'))
        else:
            pass

Now the question itself is how it is better to make it so that when authorizing a new user, a comparison record was searched in the Person model by email or last_name and first_name, and if it was found, then the user field was filled in by the logged in user.
What is better to use to hang up a signal on a method in the model or write a new method in the adapter?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question