1

私はこのようなユーザーリソースを持っています

class UserResource(ModelResource):

  class Meta:
    queryset = User.objects.filter(is_active=True)
    resource_name = 'user'
    excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
    serializer = CamelCaseJSONSerializer(formats=['json'])
    list_allowed_methods = ['post']
    detail_allowed_methods = ['get', 'post', 'put', 'patch']
    authentication = ApiKeyAuthentication()
    models.signals.post_save.connect(create_api_key, sender=User)

  def obj_create(self, bundle, request=None, **kwargs):
    try:
      bundle = super(UserResource, self).obj_create(bundle, request, **kwargs)
      bundle.obj.set_password(bundle.gata.get('password'))
      bundle.obj.save()
    except IntegrityError:
      raise BadRequest('The username already exists')
    return bundle

  def apply_authorization_limits(self, request, object_list):
    return object_list.filter(user = request.user)

私はslumberを使用して、このコード行で新しいユーザーを作成しています

import slumber
>>> api = slumber.API("http://127.0.0.1:8000/api/v1")
>>> new = api.user.post({"firstName" : "fname", "lastName" : "lname", "username" : "anewuser", "password" : "123456", "email" : "email@email.com"})

エラーが発生します

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/slumber/__init__.py", line 125, in post
    resp = self._request("POST", data=s.dumps(data), params=kwargs)
  File "/Library/Python/2.7/site-packages/slumber/__init__.py", line 104, in _request
    raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
slumber.exceptions.HttpClientError: Client Error 401: http://127.0.0.1:8000/api/v1/user/

新しいユーザーを作成するときに401を取得する必要はないと思います...

APIを開いて、すべてのユーザーのリストを取得できるようにし、ログインしているユーザーだけが情報を取得できるようにします。

私は何が間違っているのですか?

それを行うためのより良い方法-ユーザーの作成に使用される新しいリソースを作成します。このリソースは、スーパーユーザーのみが呼び出すことができます

class UserSignUpResource(ModelResource):

  class Meta:
    object_class = User
    queryset = User.objects.all()
    allowed_methods = ['post']
    include_resource_uri = False
    resource_name = 'newuser'
    excludes = ['is_active', 'is_staff', 'is_superuser']
    serializer = CamelCaseJSONSerializer(formats=['json'])
    authentication = ApiKeyAuthentication()
    authorization = DjangoAuthorization()
    models.signals.post_save.connect(create_api_key, sender=User)

  def obj_create(self, bundle, request=None, **kwargs):
    try:
      bundle = super(UserSignUpResource, self).obj_create(bundle, request, **kwargs)
      bundle.obj.set_password(bundle.data.get('password'))
      bundle.obj.save()
    except IntegrityError:
      raise BadRequest('The username already exists')
    return bundle

  def apply_authorization_limits(self, request, object_list):
    return object_list.filter(id=request.user.id, is_superuser=True)

このコマンドで新しいユーザーを作成します

curl -v -X POST -d '{"username" : "username", "password" : "123456"}' -H "Authorization: ApiKey superusername:apikey" -H "Content-Type: application/json" http://127.0.0.1:8000/api/v1/newuser/
4

1 に答える 1

0

新しいリソースを作成して解決しました

class UserSignUpResource(ModelResource):

  class Meta:
    object_class = User
    queryset = User.objects.all()
    allowed_methods = ['post']
    include_resource_uri = False
    resource_name = 'newuser'
    excludes = ['is_active', 'is_staff', 'is_superuser']
    serializer = CamelCaseJSONSerializer(formats=['json'])
    authentication = ApiKeyAuthentication()
    authorization = DjangoAuthorization()
    models.signals.post_save.connect(create_api_key, sender=User)

  def obj_create(self, bundle, request=None, **kwargs):
    try:
      bundle = super(UserSignUpResource, self).obj_create(bundle, request, **kwargs)
      bundle.obj.set_password(bundle.data.get('password'))
      bundle.obj.save()
    except IntegrityError:
      raise BadRequest('The username already exists')
    return bundle

  def apply_authorization_limits(self, request, object_list):
    return object_list.filter(id=request.user.id, is_superuser=True)
于 2012-05-28T15:42:07.530 に答える