Search code examples
djangotastypie

Separate POST access urls for same resource


I need to create separate POST methods for user creation and user authentication

eg:http://localhost:8000/registerUser which takes email,name and password to register a user and another url

eg:http://localhost:8000/authenticateUser whcih takes the email and password to authenticate the user

Can I do this with by overriding "override_url" or the "dispatch" method ? Or anyother way


Solution

  • I think, what you are looking for is the prepend_url function, see here. You could use it like this:

    class AuthenticateUser(Resource)
    
        class Meta:
            resource_name = "authenticateUser"
    
        def prepend_urls(self):
            #add the cancel url to the resource urls
            return [
                url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/register%s$" %
                    (self._meta.resource_name, trailing_slash()),
                    self.wrap_view('register'), name="api_authenticate_register"),
            ]
    
        def register(self, request, **kwargs):
            # check request
            self.method_check(request, allowed=['post'])
            # handle your request here, register user
            return self.create_response(request,  <some method>)
    

    With this, you could call it like this:

    http://localhost:8000/authenticateUser # to authenticate
    http://localhost:8000/authenticateUser/register # to register
    

    Another option would be, to just create two resources (on inheriting from the other) and just change the resource_name in the meta class