我在使用tastypie 0.9.12+執行記錄級授權時遇到了問題。Django Tastypie記錄級授權
我的設置看起來是這樣的:
型號
class UserProfile(models.Model):
def __unicode__(self):
return self.user.get_full_name()
user = models.OneToOneField(User)
class Sample(models.Model):
def __unicode__(self):
return '%s' % self.id
OPEN = 0
CLAIMED = 1
CLOSED = 2
MANUAL = 3
MODIFIED = 4
DELETED = 5
ERROR = 6
RESERVED = 7
STATUS_CHOICES = (
(OPEN, 'Open'),
(CLAIMED, 'Claimed'),
(CLOSED, 'Closed'),
(MANUAL, 'Manual'),
(MODIFIED, 'Modified'),
(DELETED, 'Deleted'),
(ERROR, 'Error'),
(RESERVED, 'Reserved'),
)
status = models.SmallIntegerField(max_length = 1, default = OPEN, choices = STATUS_CHOICES)
user_profile = models.ForeignKey(UserProfile, blank = True, null = True)
資源
class BaseResource(ModelResource):
# Base class with rather strict default settings
# All other Resources extend this and override any defaults to higher permissions
class Meta:
authentication = DjangoAuthentication()
authorization = ReadOnlyAuthorization()
allowed_methods = []
class SampleResource(BaseResource): # BaseResource defines a default Meta, setting allowed_methods and authentication for all other resources in the API
UserProfile = fields.ForeignKey(UserProfileResource, 'user_profile', null = True, full = True)
class Meta(BaseResource.Meta):
queryset = Sample.objects.all()
resource_name = 'sample'
allowed_methods = ['get', 'post', 'put', 'patch']
authorization = SampleAuthorization()
always_return_data = True
def dehydrate_status(self, bundle):
return Sample.STATUS_CHOICES[bundle.data['status']][1]
def hydrate_status(self, bundle):
bundle.data['status'] = Sample.__dict__[bundle.data['status'].upper()]
return bundle
授權
class SampleAuthorization(Authorization):
# Checks that the records' owner is either None or the logged in user
def authorize_user(self, bundle):
return bundle.obj.user_profile in (None, self.user_profile(bundle))
def user_profile(self, bundle):
return user_profile.objects.get(user = bundle.request.user)
def read_list(self, object_list, bundle):
print 'Read List'
return object_list.filter(Q(user_profile = self.user_profile(bundle)) | Q(user_profile = None))
def read_detail(self, object_list, bundle):
print 'Read Detail'
return self.authorize_user(bundle)
def create_list(self, object_list, bundle):
return object_list
def create_detail(self, object_list, bundle):
return self.authorize_user(bundle)
def update_list(self, object_list, bundle):
print 'Update List'
allowed = []
for obj in object_list:
if obj.user_profile in (None, self.user_profile(bundle)):
allowed.append(obj)
return allowed
def update_detail(self, object_list, bundle):
print 'Update Detail'
print bundle.obj.status, bundle.data['status']
# Compare status stored on the server against the user-set status
# If server status is >= user status
# Raise Unauthorized
if bundle.obj.status >= bundle.data['status']:
raise Unauthorized('New status must be higher than current status')
return self.authorize_user(bundle)
def delete_list(self, object_list, bundle):
raise Unauthorized('Deletion not allowed through API')
def delete_detail(self, object_list, bundle):
raise Unauthorized('Deletion not allowed through API')
我的問題是,似乎update_detail被調用兩次,不同的輸入。請求的更新嘗試更改存儲在服務器上的記錄的狀態。新狀態必須高於存儲的狀態,否則更改是未經授權的。
當運行上面的代碼,我的輸出是這樣的:
Read Detail
Update Detail
0 Claimed
Update Detail
1 1
[27/Mar/2013 09:35:23] "PATCH /api/1.0/sample/1/ HTTP/1.1" 401 0
在第一遍時,bundle.obj.status具有正確的價值,但bundle.data [「狀態」] HAS沒有被水合。在第二階段,bundle.obj.status已經被更改爲新的狀態,並且新的狀態HAS已被水化。
因爲狀態在第一次通過時未被水合,所以我無法可靠地比較它們,並且不想手動調用hydrate_status,因爲它會擾亂在後臺完成的整個水合過程。因爲第二遍的值是相同的,不管我設置了什麼狀態,它總是會引發未經授權的異常。
如果方法被Tastypie用存儲狀態值和新狀態值的不同輸入調用兩次,我該如何實現記錄級授權?