カスタムURLを使用してリソースの削除を実装したい。ですから、デフォルトのリソースTastypie urlにDELETEを許可するのではなく、新しいものを定義しました。削除は機能しますが、私は自分のコードに何か不足していると確信しているので、まだエラーが発生しています。削除を実行する関数はcancel_ride_request
です。Tastypie:カスタムURLにDELETEリソースを実装する
class DemandResource(ModelResource):
"""
Handles ride requests resources. In particular:
- Offers information about the logged user's ride requests
- Allows new ride requests creation
"""
user = fields.ForeignKey(UserResource, 'passenger')
origin = fields.ForeignKey(NodeResource, 'origin', full=True)
destination = fields.ForeignKey(NodeResource, 'destination', full=True)
potential_drivers = fields.ListField(readonly=True)
class Meta:
queryset = api.models.Demand.objects.all()
resource_name = _Helpers.demand_resource_name
list_allowed_methods = ['get']
detail_allowed_methods = ['get', 'put', 'patch']
authentication = BasicAuthentication()
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/register%s" % (self._meta.resource_name, trailing_slash()),
self.wrap_view('register_ride_request'), name="api_ask_ride"),
url(r"^(?P<resource_name>%s)/(?P<pk>.*?)/cancel%s" % (self._meta.resource_name, trailing_slash()),
self.wrap_view('cancel_ride_request'), name="api_cancel_ride_request"),
]
@classmethod
def dehydrate_potential_drivers(cls, bundle):
return _Helpers.serialise_passengerships_passenger(bundle.obj.passengership_set.select_related().all())
def hydrate(self, bundle):
bundle.data['user'] = bundle.request.user
#extract orign and destination ID
bundle.data['origin'] = api.models.Node.objects.get(id=bundle.data['origin']['id'])
bundle.data['destination'] = api.models.Node.objects.get(id=bundle.data['destination']['id'])
bundle.data['arrival_time'] = datetime.strptime(bundle.data['arrival_time'], _Helpers.date_time_format)
tz = pytz.timezone('Europe/Brussels') #TODO get the user time zone
bundle.data['arrival_time'] = tz.localize(bundle.data['arrival_time'])
bundle.data['arrival_time_tolerance_early'] = timedelta(minutes=int(bundle.data['arrival_time_tolerance_early']))
bundle.data['arrival_time_tolerance_late'] = timedelta(minutes=int(bundle.data['arrival_time_tolerance_late']))
return bundle
def register_ride_request(self, request, **kwargs):
self.method_check(request, ['post', ])
self.is_authenticated(request)
data = json.loads(request.body)
bundle = self.build_bundle(data=data, request=request)
bundle = self.hydrate(bundle)
demand = api.models.Demand(passenger=bundle.request.user,
origin=bundle.data['origin'],
destination=bundle.data['destination'],
arrival_time=bundle.data['arrival_time'],
arrival_time_tolerance_early=bundle.data['arrival_time_tolerance_early'],
arrival_time_tolerance_late=bundle.data['arrival_time_tolerance_late'])
demand.save()
return HttpResponse(status=201)
"""
Handling demand deletion, making sure the request type is a DELETE and the user is authenticated
"""
def cancel_ride_request(self, request, **kwargs):
self.method_check(request, ['delete', ])
self.is_authenticated(request)
return api.models.Demand.objects.filter(pk=kwargs['pk']).delete()
"""
Makes sure that only the owner of a demand is able to delete it
"""
def delete_detail(self, object_list, bundle):
return bundle.obj.passenger == bundle.request.user
また、私は機能register_ride_request
とリソースの作成を実施してきた方法が最適ではないと思います。それは動作しますが、私は手動でHTTPResponse
コードを返さなければならないのが奇妙です。それを行う良い方法はありませんか? 1つの投稿で2つの質問に感謝して申し訳ありませんが、私はそれらが関連していると感じています。