Everybody is aware of that Python has a very talked-about Django framework. By default, the person mannequin there consists of an e-mail, username, and password. However there are sometimes conditions the place it’s mandatory to increase and exchange the default person mannequin.
For instance, there are conditions the place we don’t want a username, solely an e-mail. Or we want a cellphone quantity as an alternative of an e-mail, and the e-mail just isn’t essential. In most of these conditions, the default mannequin ought to be modified to a customized one which incorporates the related fields. So, the aim of the django-swap-user challenge is to interchange an current mannequin with a customized one.
As well as, authorization by one-time code is now starting to achieve reputation. So we accounted for this situation within the library as effectively. When a brand new person mannequin seems, it has just one area—e-mail or cellphone quantity—and a one-time passcode. There is no such thing as a password saved within the database, and a one-time passcode is shipped utilizing any supplier that you simply join. You get a one-time SMS, which you should use to log in.
Why did we write a complete library for this? Once we begin new initiatives, we frequently want a customized person mannequin. Beforehand, we copied the whole lot from one challenge to a different, however then we realized—why continually copy, when you’ll be able to write a library as soon as and that is it. We needed to discover a ready-made library, however all of the ready-made ones had been deserted or out-of-date. So our Python Teamlead, Artem Innokentiev, determined to jot down this wrapper himself and open-source it.
So, in case you are bored with copying a customized person mannequin from one challenge to different ones—use this package deal. It would do all of it for you!
Set up
pip set up django-swap-user
Structure
Software swap_user
is cut up into a number of apps:
to_email
– gives person withe-mail
username areato_email_otp
– gives person withe-mail
username area and OTP (One Time Password) authenticationto_phone
– gives person withcellphone
username areato_phone_otp
– gives person withcellphone
username area and OTP (One Time Password) authentication
Why such an uncommon structure?
If we go away them in a single app, they are going to all create migrations and tables, resulting in redundant tables. They are going to be handled as three customized fashions throughout the identical app, which causes confusion.
With this method (the place there’s a frequent app which incorporates inner apps), the person can select and join solely the precise person mannequin greatest suited to concrete enterprise logic.
We modeled this method after the Django REST Framework authtoken
software, referenced right here.
Using a customized person mannequin firstly of a challenge
When you find yourself beginning a challenge from scratch, that is the most effective time to combine a customized person mannequin, because you haven’t had a variety of migrations or you’ll be able to simply regenerate them. Furthermore, Django’s official docs suggest that you simply use a customized person mannequin, even in case you are totally happy with the default one. Sooner or later, it will likely be simpler to increase a customized mannequin to suit your use circumstances.
Incorporating a customized person mannequin in the midst of a challenge
Including a customized person mannequin in the midst of a challenge is a tougher means of doing issues, however it’s nonetheless attainable.
- Full all of the steps within the testing database and—ONLY IF all of them had been profitable—attempt to apply it within the manufacturing setting
- Please notice that these steps match most circumstances, however in some circumstances, it’s possible you’ll have to adapt to the state of affairs
- Create a backup of your database
- Add secure tag into your repository or save a commit hash reference
- Pray 😀
- Take away all your migrations in each app of the Django challenge
- Take away all information from
django_migrations
desk, for instance, withSQL TRUNCATE django_migrations
- Now you may have a “clear” state, so you’ll be able to change the default mannequin
- Generate new migrations for all your purposes—
python handle.py makemigrations
- Now it’s good to pretend migrate, as a result of you have already got all of the tables with information
- First, pretend the
auth
software, since you are relying on this one:python handle.py migrate --fake auth
- Set up the library, observe the directions, and apply migrations
- Then, pretend the remainder of the migrations you may have—
python handle.py migrate --fake
- Run your software!
Plans for the longer term
Sooner or later, we plan to jot down higher documentation and add implementation to the challenge. We are going to slowly add new authorization strategies, for instance, combined ones. There are circumstances the place it’s good to do authorization both by e-mail or by cellphone, and we wish to add this.
For the time being, everybody can use this challenge, and we might love to draw members of the neighborhood to contribute and assist with the event of the challenge!
And, in fact, attain out to us by way of the shape under if it’s good to develop an open-source resolution or are searching for a group of pros to construct a challenge from scratch!