how to send reset password email at the creation of my user in django API?

Viewed 360

I build an API with django and I'm automating the welcome email at creation of one user with Sendinblue. I have no problem at this step. (so we are at the url /add_user/)

I have a second url to send a link (uid + token) to a user (thanks to his email). For that I use an external package : djoser. So I use the url of djoser /auth/users/reset_password/ and /auth/users/reset_password_confirm/

My dream would be to combine /reset_password/ and /add_user/ in the single url /add_user/ My problem is that the logic in /reset_password/ and /reset_password_confirm/ was written by djoser so I can't handle it. Or I don't know how !


Here is what I tried to override djoser views :

in urls.py:

   path('custom_djoser/', ChangeCredentialsView.as_view(), name='customdjoser'),

in views.py:

class ChangeCredentialsView(UserViewSet):
    def post(self, *args, **kwargs):
        print("hello world")
        return JsonResponse({"helloworld":"success"})

got the following error :

The actions argument must be provided when calling .as_view() on a ViewSet. For example .as_view({'get': 'list'})

So I removed the .as_view() in urls.py and got this error :

Forbidden (CSRF token missing or incorrect.): /custom_djoser/

For me its incomprehensible because I don't use csrf token, but I tried with @csrf_exempt in a dispatch function inside my class. I tried to define class ChangeCredentialsView(UserViewSet, APIView): as my other class with authorization token. Nothing to do, everytime error with csrf token. I may explore a really painful and stupid way to do what I want to do, please tell me if there is an easier way to do this !

1 Answers

@Scratch'N'Purr I answer to you here because of the lack of space in comments :

currently my add_user view looks like this (the most important part because its huge)

class AddUserView(generics.CreateAPIView, APIView):
    def post(self, request):
               newUser = User(
                username    = request.POST.get('username'),
                password    = request.POST.get('password'),
                email       = request.POST.get('email'),
                image       = request.FILES['image'],
                full_name   = request.POST.get('full_name'),
                user_type   = request.POST.get('usertype'),
                contact_number = request.POST.get('phone')
            )
            newUser.save()
 
      **** then lines of code to send email with Sendinblue ****

Usually I use request.data to get the request body but here there is a file so I had to use formdata. and I have to confess I don't know a lot about serializers and don't use it

Related