2

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 !

LucieDevGirl
  • 177
  • 10
  • It's not possible to combine both `reset_password` and `reset_password_confirm` into one view because there is an intermediary step in the middle that requires the user to reset the password in the frontend. At the most, what you can do is implement the `reset_password` functionality in your `add_user` view, and have the frontend app post the data to the `reset_password_confirm` route. To add the `reset_password` functionality into your `add_user` view, refer to [here](https://github.com/sunscrapers/djoser/blob/b648b07dc2da7dab4c00c1c39af3d6ec53f58eb6/djoser/views.py#L239). – Scratch'N'Purr Jul 21 '21 at 09:48
  • Thank you for your help, yes you're right I edited my post because I just want to combine reset_password and add_user – LucieDevGirl Jul 21 '21 at 13:01
  • Ok, what does your `add_user` view and serializer look like? Because all you need to add is the sending email code piece from djoser's repo to the view/serializer after the user is created. – Scratch'N'Purr Jul 22 '21 at 00:28
  • I showed you in answer because was to big for a comment – LucieDevGirl Jul 22 '21 at 10:58

1 Answers1

0

@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

LucieDevGirl
  • 177
  • 10
  • It's probably best to use a serializer before sending the data into the object for creation because serializers can perform validation on the data prior to passing the data to the model. Since your data includes formdata, you can modify the parser class of your view. Maybe take a look at this [answer](https://stackoverflow.com/a/63398121/6245650) for some inspiration. – Scratch'N'Purr Jul 23 '21 at 00:46