1

I am trying an example of using tastypie and django-registration to have an api for registering. When I execute the test_register.py test file, I get an error "'WSGIRequest' object has no attribute 'data'". I think it might have to do with the concept of bundles. Any help to get this scenario working is appreciated.

api.py

from registration.views import register
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from tastypie.http import HttpUnauthorized, HttpForbidden
from django.conf.urls.defaults import url
from tastypie.utils import trailing_slash
from registration.models import RegistrationManager

class RegisterUserResource(ModelResource):
    class Meta:
        allowed_methods = ['post']
#       authentication = Authentication()
#       authorization = Authorization()
        include_resource_uri = 'register'
        fields = ['username','password']

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/register%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('register'), name="api_register"),
        ]

    def register(self, bundle, request=None, **kwargs):
        username, password = bundle.data['username'], bundle.data['password']
        print "reached register"
        try:
            bundle.obj = RegistrationManager.create_inactive_user(username, username, password)
        except IntegrityError:
            raise BadRequest('That username already exists')
            return bundle

test_register.py

import requests
import json
from urllib2 import urlopen
import datetime
import simplejson

url = 'http://127.0.0.1:8000/apireg/registeruser/register/'
data = {'username' :'s3@gmail.com', 'password' : 'newpass'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
print json.dumps(data)
r = requests.post(url, data=json.dumps(data), headers=headers)
print r.content

urls.py

from userdetails.api import RegisterUserResource
register_userresource = RegisterUserResource()
admin.autodiscover()

urlpatterns = patterns('',
     ...
    (r'^apireg/', include(register_userresource.urls)),
)

Update

I made some changes to register method without using bundle and the basic functionality now works. This creates an inactive user.

def register(self,request, **kwargs):
        data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))

        username = data.get('username', '')
        password = data.get('password', '')
        # try:
   #     userreg = RegistrationManager
        RegistrationManager().create_inactive_user(username, username, password,'',send_email=False)

I still get an error even though this works, which I am trying to resolve. "error_message": "'NoneType' object is not callable"

Neo_32
  • 225
  • 4
  • 12

1 Answers1

1

I tried using RegistrationProfile instead. It worked. (See examples in django-registration/tests/models.py). It will be something like

from registration.models import RegistrationProfile
new_user = RegistrationProfile.objects.create_inactive_user(
                       username=username,
                       password=password,
                       email=email, site=Site.objects.get_current(),
                       send_email=True)
Sandeep
  • 306
  • 5
  • 8